Poll dev portal for new image
This commit is contained in:
55
main.py
55
main.py
@@ -1,34 +1,49 @@
|
|||||||
|
import io
|
||||||
|
import time
|
||||||
|
import requests
|
||||||
from escpos.printer import Usb
|
from escpos.printer import Usb
|
||||||
from PIL import Image, ImageEnhance
|
from PIL import Image, ImageEnhance
|
||||||
|
|
||||||
VENDOR_ID = 0x0416
|
VENDOR_ID = 0x0416
|
||||||
PRODUCT_ID = 0x5011
|
PRODUCT_ID = 0x5011
|
||||||
|
IMAGE_URL = "https://static.spaceport.dns.t0.vc/drawing.png"
|
||||||
# Initialize printer
|
|
||||||
p = Usb(VENDOR_ID, PRODUCT_ID, interface=0, in_ep=0x81, out_ep=0x03)
|
|
||||||
|
|
||||||
# Load and convert image
|
|
||||||
PRINTER_WIDTH = 384 # Set to your printer's pixel width (common: 384 or 576)
|
PRINTER_WIDTH = 384 # Set to your printer's pixel width (common: 384 or 576)
|
||||||
|
|
||||||
PRINTER_WIDTH = 384 # Adjust to your printer
|
|
||||||
|
|
||||||
img = Image.open('robbie.jpg')
|
def main():
|
||||||
|
# Initialize printer
|
||||||
|
p = Usb(VENDOR_ID, PRODUCT_ID, interface=0, in_ep=0x81, out_ep=0x03)
|
||||||
|
last_image_content = None
|
||||||
|
|
||||||
# Resize first
|
while True:
|
||||||
wpercent = (PRINTER_WIDTH / float(img.size[0]))
|
try:
|
||||||
hsize = int((float(img.size[1]) * float(wpercent)))
|
response = requests.get(IMAGE_URL, timeout=5)
|
||||||
img = img.resize((PRINTER_WIDTH, hsize), Image.LANCZOS)
|
response.raise_for_status()
|
||||||
|
|
||||||
# Optional: Enhance contrast
|
if response.content != last_image_content:
|
||||||
img = ImageEnhance.Contrast(img).enhance(2.0)
|
print("New image detected, printing...")
|
||||||
|
last_image_content = response.content
|
||||||
|
|
||||||
# Optional: Sharpen
|
img = Image.open(io.BytesIO(response.content))
|
||||||
# img = ImageEnhance.Sharpness(img).enhance(2.0)
|
|
||||||
|
|
||||||
# Convert with dithering
|
# Resize first
|
||||||
img = img.convert('1', dither=Image.FLOYDSTEINBERG)
|
wpercent = (PRINTER_WIDTH / float(img.size[0]))
|
||||||
|
hsize = int((float(img.size[1]) * float(wpercent)))
|
||||||
|
img = img.resize((PRINTER_WIDTH, hsize), Image.LANCZOS)
|
||||||
|
|
||||||
# Print image
|
# Convert with dithering
|
||||||
p.image(img)
|
img = img.convert('1', dither=Image.FLOYDSTEINBERG)
|
||||||
p.cut()
|
|
||||||
|
# Print image
|
||||||
|
p.image(img)
|
||||||
|
p.cut()
|
||||||
|
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
print(f"Error downloading image: {e}")
|
||||||
|
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user