encode-image.py 678 B

1234567891011121314151617181920212223242526
  1. from PIL import Image
  2. image_path = 'ice-base.png'
  3. pixel_data = None
  4. compressdict = {}
  5. with Image.open(image_path) as image:
  6. pixel_data = image.load()
  7. char_count = 1
  8. rawdata = ''
  9. bitpack_string = ''
  10. # bitpack spec - 4-bit values encoded to hex
  11. width, height = image.size
  12. for y in range(height):
  13. for x in range(0, width-4, 4):
  14. px = int(pixel_data[(x, y)])
  15. bitpack_string += hex(pixel_data[(x, y)] << 12 | pixel_data[(x+1, y)] << 8 | pixel_data[(x+2, y)] << 4 | pixel_data[(x+3, y)]).replace('0x','') + " "
  16. rawdata += str(px)
  17. print(bitpack_string)
  18. # print("\nvs\n")
  19. # print(rawdata)