import requests from PIL import Image, ImageDraw, ImageFont dither = 0 apip = "192.168.178.192" mac_file = "macs.txt" # eine MAC pro Zeile # 1. Bild generieren (wie im Wiki-Beispiel1) image = Image.new('P', (296, 128)) palette = [ 255, 255, 255, # weiß -> Index 0 0, 0, 0, # schwarz -> Index 1 255, 0, 0 # rot -> Index 2 ] image.putpalette(palette) draw = ImageDraw.Draw(image) line1, line2 = "OpenEPaperLink", "Demo image" # Fonts laden – fallback auf Default, wenn arial.ttf fehlt try: font_line1 = ImageFont.truetype("arial.ttf", size=36) font_line2 = ImageFont.truetype("arial.ttf", size=16) except OSError: font_line1 = ImageFont.load_default() font_line2 = ImageFont.load_default() bbox1 = draw.textbbox((0, 0), line1, font=font_line1) bbox2 = draw.textbbox((0, 0), line2, font=font_line2) pos1 = ((image.width - (bbox1[2] - bbox1[0])) // 2, 20) pos2 = ((image.width - (bbox2[2] - bbox2[0])) // 2, 80) # Text schreiben (Palette‑Index 2 = rot, 1 = schwarz, 0 = weiß) draw.text(pos1, line1, fill=2, font=font_line1) # rot draw.text(pos2, line2, fill=1, font=font_line2) # schwarz # 2. RAW‑Daten erzeugen: zwei Bit‑Planes width, height = image.size pixels = list(image.getdata()) # Schwarze Bit‑Plane: Bit = 1, wenn Pixel schwarz (Index 1), sonst 0 black_bytes = bytearray() for y in range(height): row_base = y * width for xb in range((width + 7) // 8): byte = 0 for bit in range(8): x = xb * 8 + bit if x < width and pixels[row_base + x] == 1: byte |= 0x80 >> bit black_bytes.append(byte) # Rote Bit‑Plane: Bit = 1, wenn Pixel rot (Index 2), sonst 0 red_bytes = bytearray() for y in range(height): row_base = y * width for xb in range((width + 7) // 8): byte = 0 for bit in range(8): x = xb * 8 + bit if x < width and pixels[row_base + x] == 2: byte |= 0x80 >> bit red_bytes.append(byte) # 3. RAW‑Datei speichern – Reihenfolge: erst Schwarz/Weiß‑Plane, dann Rot‑Plane raw_path = "output.raw" with open(raw_path, "wb") as f: f.write(black_bytes) f.write(red_bytes) # 4. MAC‑Adressen aus Datei laden with open(mac_file, "r", encoding="utf-8") as f: macs = [line.strip() for line in f if line.strip()] # 5. Upload für jede MAC url = f"http://{apip}/imgupload" for mac in macs: payload = {"dither": dither, "mac": mac} with open(raw_path, "rb") as file: response = requests.post(url, data=payload, files={"file": file}) if response.status_code == 200: print(f"RAW‑Bild erfolgreich an {mac} gesendet!") else: print(f"Upload fehlgeschlagen bei {mac}: Status {response.status_code}")