123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #!/usr/bin/env python3
- from PIL import Image
- import sys
- from pathlib import Path
- OLED_HEIGHT = 32
- OLED_WIDTH = 128
- OLED_PAGE_HEIGHT = 8
- if len(sys.argv) < 2:
- print("No image path provided.")
- sys.exit()
- img_path = sys.argv[1]
- try:
- im = Image.open(img_path)
- except OSError:
- raise Exception("Oops! The image could not be opened.")
- img_width = im.size[0]
- img_height = im.size[1]
- if img_width > OLED_WIDTH or img_height > OLED_HEIGHT:
- print(f'Your image is {img_width} pixels wide and {img_height} pixels high, but...')
- raise Exception(f"OLED display only {OLED_WIDTH} pixels wide and {OLED_HEIGHT} pixels high!")
- # 2値化
- im = im.convert("L")
- out = im.convert("1")
- img_name = "ssd1306img"
- pixels = list(out.getdata())
- pixels = [0 if x == 255 else 1 for x in pixels]
- buffer = []
- for i in range(img_height // OLED_PAGE_HEIGHT):
- start_index = i*img_width*OLED_PAGE_HEIGHT
- for j in range(img_width):
- out_byte = 0
- for k in range(OLED_PAGE_HEIGHT):
- out_byte |= pixels[k*img_width + start_index + j] << k
- buffer.append(out_byte)
- with open(f'{img_name}.bin', 'wb') as f:
- f.write(bytearray(buffer))
- print(f"Successfully created binary file: {img_name}.bin")
- print(f"Image dimensions: {img_width}x{img_height}")
|