Skip to content

Instantly share code, notes, and snippets.

@dinkopehar
Created July 21, 2020 09:00
Show Gist options
  • Save dinkopehar/8e610e514785357ce0b2df57e32f85ef to your computer and use it in GitHub Desktop.
Save dinkopehar/8e610e514785357ce0b2df57e32f85ef to your computer and use it in GitHub Desktop.
Custom script to download cat images and print them using ESCPOS mode
import shutil
from escpos.printer import Usb
import requests
from PIL import Image
def download_cat_image(status_code=200):
"""Download cat image based on status_code."""
print(f"Downloading: {status_code} code cat")
response = requests.get(f'https://http.cat/{status_code}', stream=True)
with open(f'images/{status_code}.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
def resize_image(status_code='200'):
print(f"Resizing {status_code} cat")
im = Image.open(f'images/{status_code}.jpg')
im.thumbnail((512, 512), Image.ANTIALIAS)
im.save(f'images/{status_code}-thumbnail.jpg', 'JPEG')
def print_cat_code(status_code=200):
p = Usb(0x0519, 0x0001, 0)
p.image(f'images/{status_code}-thumbnail.jpg')
p.text('---------------------' * 2)
p.cut('PART')
if __name__ == '__main__':
STATUS_CODES = [100, 200, 204, 300, 400, 404, 500, 505]
for status in STATUS_CODES:
download_cat_image(status)
resize_image(str(status))
break
for status in STATUS_CODES:
print_cat_code(status)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment