01 studio k230设备已连接wifi,怎么实现下载网络图片(https开头),有没有参考案例

Viewed 30

问题描述


之前参考了https_client.py 下载老是有问题,图片600k+ 下载下来只有100k+

1 Answers

你好,可以使用这个为参考,不过这里的图片比较小。

# simple_image_display.py
import network
import time
import requests
from media.display import *
from media.media import *

# Configuration
SSID = "CMCC-9DGZ"
PASSWORD = "xel123456"


IMAGE_URL = "https://www.kendryte.com/img/k230d.17aa362a.jpg"
IMAGE_FILE = "/data/display_image.jpeg"

DISPLAY_WIDTH = ALIGN_UP(1920, 16)
DISPLAY_HEIGHT = 1080

def connect_wifi():
    """Connect to WiFi"""
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    
    if not wlan.isconnected():
        print("Connecting to WiFi...")
        wlan.connect(SSID, PASSWORD)
        
        timeout = 10
        start = time.time()
        
        while not wlan.isconnected():
            if time.time() - start > timeout:
                raise Exception("WiFi timeout")
            time.sleep(0.5)
            print(".", end="")
    
    print("\nConnected! IP:", wlan.ifconfig()[0])

def download_and_show():
    """Download image and show on display"""
    
    # Connect to WiFi
    connect_wifi()
    
    # Download image
    print("Downloading image...")
    try:
        response = requests.get(IMAGE_URL)
        if response.status_code == 200:
            # Save image
            with open(IMAGE_FILE, "wb") as f:
                f.write(response.content)
            print(f"Downloaded {len(response.content)} bytes")
            
            # Initialize display
            Display.init(Display.LT9611, 
                        width=DISPLAY_WIDTH, 
                        height=DISPLAY_HEIGHT,
                        to_ide = True)
            MediaManager.init()

            # Load and display image
            img = image.Image(IMAGE_FILE)
            img_show = img.to_rgb888()
            print(img_show)

            # Show image
            Display.show_image(img_show)
            print("Image displayed! Press Ctrl+C to exit")

            # Keep displaying
            while True:
                time.sleep(1)
        else:
            print(f"Download failed: HTTP {response.status_code}")
            
    except Exception as e:
        print(f"Error: {e}")
    finally:
        if 'response' in locals():
            response.close()
        # Cleanup on exit
        MediaManager.deinit()
        Display.deinit()

if __name__ == "__main__":
    download_and_show()