import urllib.request import urllib.parse import time from PIL import Image, ImageDraw IP = "192.168.68.128" def send_raw_frame(payload, x=0, y=0, w=320, h=240, fmt=None): params = {"x": x, "y": y, "w": w, "h": h} if fmt: params["format"] = fmt q = urllib.parse.urlencode(params) url = f"http://{IP}/api/screen/raw?{q}" req = urllib.request.Request( url, data=payload, headers={"Content-Type": "application/octet-stream"}, method="POST" ) try: with urllib.request.urlopen(req, timeout=5.0) as resp: return resp.read().decode("utf-8") except Exception as e: return f"Error: {e}" # 1. Test Color Frame (RGB565) print("Generating raw color frame...") img_color = Image.new("RGB", (320, 240)) draw = ImageDraw.Draw(img_color) for x in range(320): for y in range(240): r = (x * 255 // 319) g = (y * 255 // 239) b = 128 img_color.putpixel((x, y), (r, g, b)) draw.text((20, 20), "RAW COLOR STREAM (RGB565)", fill=(255, 255, 255)) # Convert to RGB565 BE bytes color_payload = bytearray(320 * 240 * 2) for idx, (r, g, b) in enumerate(img_color.getdata()): val = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3) color_payload[idx * 2] = (val >> 8) & 0xFF color_payload[idx * 2 + 1] = val & 0xFF print("Streaming raw color frame...") res = send_raw_frame(bytes(color_payload), fmt="rgb565") print("Response:", res) time.sleep(3.0) # 2. Test Monochrome Frame (1-bit) print("\nGenerating raw monochrome frame...") img_mono = Image.new("1", (320, 240), 0) draw_mono = ImageDraw.Draw(img_mono) draw_mono.rectangle([10, 10, 310, 230], outline=1, width=3) draw_mono.text((30, 100), "RAW MONOCHROME STREAM (1-BIT)", fill=1) # Convert PIL 1-bit to MHMSB bytearray mono_payload = img_mono.tobytes() print(f"Monochrome payload size: {len(mono_payload)} bytes") print("Streaming raw monochrome frame...") res_mono = send_raw_frame(mono_payload, fmt="mono") print("Response:", res_mono)