Commit 8de08fed authored by Russ Hughes's avatar Russ Hughes

add depth conversion and usage

parent 55ca1dcd
#!/usr/local/bin/python3.6 #!python3
''' '''
Convert image file to python module for use with blit_bitmap. Convert image file to python module for use with blit_bitmap.
Usage imgtobitmap image_file bits-per-pixel >image.py Usage imgtobitmap image_file bits_per_pixel >image.py
''' '''
import sys import sys
from PIL import Image from PIL import Image
from itertools import groupby from itertools import groupby
import argparse
bits = int(sys.argv[2]) # output bits per pixel (1-8) def main():
img = Image.open(sys.argv[1])
img = img.convert("P", palette=Image.ADAPTIVE) # Make sure we are in 8-bit RGB
palette = img.getpalette() # Make copy of palette colors
# For all the colors in the palette parser = argparse.ArgumentParser(
colors = [] prog='imgtobitmap',
for color in range(1<<bits): description='Convert image file to python module for use with bitmap method.')
parser.add_argument('image_file',
help='Name of file containing image to convert')
parser.add_argument('bits_per_pixel',
type=int,
choices=range(1,9),
default=1,
metavar='bits_per_pixel',
help='The number of bits to use per pixel (1..8)')
args = parser.parse_args()
bits = args.bits_per_pixel
img = Image.open(args.image_file)
img = img.convert("P", palette=Image.ADAPTIVE, colors=2**bits)
palette = img.getpalette() # Make copy of palette colors
# For all the colors in the palette
colors = []
for color in range(1<<bits):
# get rgb values and convert to 565 # get rgb values and convert to 565
color565 = ( color565 = (
...@@ -31,13 +50,13 @@ for color in range(1<<bits): ...@@ -31,13 +50,13 @@ for color in range(1<<bits):
colors.append(f'{color:04x}') colors.append(f'{color:04x}')
image_bitstring = '' image_bitstring = ''
bit_index = 0 bit_index = 0
max_colors = 1<<bits max_colors = 1<<bits
# Run through the image and create a string with the ascii binary representation # Run through the image and create a string with the ascii binary representation
# of the color of each pixel. # of the color of each pixel.
for y in range(img.height): for y in range(img.height):
for x in range(img.width): for x in range(img.width):
pixel = img.getpixel((x, y)) pixel = img.getpixel((x, y))
color = pixel color = pixel
...@@ -46,29 +65,29 @@ for y in range(img.height): ...@@ -46,29 +65,29 @@ for y in range(img.height):
bstring += '1' if (color & (1 << bit-1)) else '0' bstring += '1' if (color & (1 << bit-1)) else '0'
image_bitstring += bstring image_bitstring += bstring
bitmap_bits = len(image_bitstring) bitmap_bits = len(image_bitstring)
# Create python source with image parameters # Create python source with image parameters
print(f'HEIGHT = {img.height}') print(f'HEIGHT = {img.height}')
print(f'WIDTH = {img.width}') print(f'WIDTH = {img.width}')
print(f'COLORS = {max_colors}') print(f'COLORS = {max_colors}')
print(f'BITS = {bitmap_bits}') print(f'BITS = {bitmap_bits}')
print(f'BPP = {bits}') print(f'BPP = {bits}')
print('PALETTE = [', sep='', end='') print('PALETTE = [', sep='', end='')
for color,rgb in enumerate(colors): for color,rgb in enumerate(colors):
if color: if color:
print(',', sep='', end='') print(',', sep='', end='')
print(f'0x{rgb}', sep='', end='') print(f'0x{rgb}', sep='', end='')
print("]") print("]")
# Run though image bit string 8 bits at a time # Run though image bit string 8 bits at a time
# and create python array source for memoryview # and create python array source for memoryview
print("_bitmap =\\", sep='') print("_bitmap =\\", sep='')
print("b'", sep='', end='') print("b'", sep='', end='')
for i in range(0, bitmap_bits, 8): for i in range(0, bitmap_bits, 8):
if i and i % (16*8) ==0: if i and i % (16*8) ==0:
print("'\\\nb'", end='', sep='') print("'\\\nb'", end='', sep='')
...@@ -77,4 +96,7 @@ for i in range(0, bitmap_bits, 8): ...@@ -77,4 +96,7 @@ for i in range(0, bitmap_bits, 8):
color = int(value,2) color = int(value,2)
print(f'\\x{color:02x}', sep='', end='') print(f'\\x{color:02x}', sep='', end='')
print("'\nBITMAP = memoryview(_bitmap)") print("'\nBITMAP = memoryview(_bitmap)")
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment