flipdot/imagegenerator/generate.py

131 lines
3.9 KiB
Python
Raw Normal View History

2023-09-20 17:53:32 +00:00
from PIL import Image
import math
import argparse
parser = argparse.ArgumentParser(
prog = 'Image Edit Script',
description = 'Manipulate or extract information from an image file',
epilog = '')
parser.add_argument('filename') # positional argument
parser.add_argument('-o', '--output') # option that takes a value
2023-12-31 13:01:44 +00:00
parser.add_argument('-f', '--format', choices=['firmware','nodered','nodered_int'], default='firmware', help='Output format') # option that takes a value
2024-12-03 16:21:37 +00:00
parser.add_argument('-n', '--variablename', action='store_true') # use outputfilename as variable name (for firmats nodered and nodered_int
2023-09-20 17:53:32 +00:00
parser.add_argument('-v', '--verbose', action='store_true') # on/off flag
2023-12-31 14:29:28 +00:00
parser.add_argument('-i', '--invert', action='store_true')
2024-02-04 21:12:23 +00:00
parser.add_argument('-y', '--ymirror', action='store_true')
parser.add_argument('-x', '--xmirror', action='store_true')
2023-09-20 17:53:32 +00:00
args = parser.parse_args()
2024-02-04 21:12:23 +00:00
print(args.filename, args.output, args.format,args.verbose,args.invert,args.xmirror,args.ymirror)
2023-09-20 17:53:32 +00:00
2024-02-04 15:46:53 +00:00
outputfilename="result.txt"
if args.output is not None:
outputfilename=args.output
2023-09-20 17:53:32 +00:00
im = Image.open(args.filename) # Can be many different formats.
pix = im.load()
print(im.size) # Get the width and hight of the image for iterating over
2024-02-04 15:46:53 +00:00
#print(pix[10,10]) # Get the RGBA Value of the a pixel of an image
2023-09-20 17:53:32 +00:00
2024-12-03 16:21:37 +00:00
variablename="msg.payload"
if (args.variablename):
variablename=outputfilename.split('.')[0]
print("Using variablename="+str(variablename))
2023-12-31 13:01:44 +00:00
output_pre=""
output_col_preL=""
output_col_writeCol_flag=False
output_col_preR=""
output_col_write_binary_flag=False
output_col_write_int_flag=False
output_col_post=""
output_post=""
if args.format=='firmware':
output_pre=""
output_col_preL="backBuffer["
output_col_writeCol_flag=True
output_col_preR="]=0b"
output_col_write_binary_flag=True
output_col_post=";\n"
elif args.format=='nodered': #Nodered Binary
2024-12-03 16:21:37 +00:00
#output_pre="msg.payload=\"\\\n"
output_pre=variablename+"=\"\\\n"
2023-12-31 13:01:44 +00:00
output_col_preL=""
output_col_writeCol_flag=False
output_col_preR=""
output_col_write_binary_flag=True
output_col_post="\\\n"
if (args.variablename):
output_post="\";"
else:
output_post="\";\nreturn msg;"
2023-12-31 13:01:44 +00:00
elif args.format=='nodered_int': #Nodered Int
2024-12-03 16:21:37 +00:00
output_pre=variablename+"=\"\\\n"
2023-12-31 13:01:44 +00:00
output_col_preL=""
output_col_writeCol_flag=False
output_col_preR=""
output_col_write_int_flag=True
output_col_post=","
if (args.variablename):
output_post="\";"
else:
output_post="\";\nreturn msg;"
2023-12-31 13:01:44 +00:00
2024-02-04 21:12:23 +00:00
xrange = range(im.size[0])
yrange = range(im.size[1])
2023-09-20 17:53:32 +00:00
def calculateDistance(x1,y1,x2,y2):
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
return dist
2024-02-04 15:46:53 +00:00
with open(outputfilename, 'w') as f:
2023-12-31 13:01:44 +00:00
f.write(output_pre)
2024-02-04 21:12:23 +00:00
_xrange=xrange
if args.xmirror:
_xrange=reversed(xrange)
for ix,x in enumerate(_xrange):
if args.verbose:
print("ix="+str(ix)+" x="+str(x))
2023-12-31 13:01:44 +00:00
f.write(output_col_preL)
if output_col_writeCol_flag:
f.write(str(x))
f.write(output_col_preR)
columnValue=0
2024-02-04 21:12:23 +00:00
_yrange=yrange
if args.ymirror:
_yrange=reversed(yrange)
for iy,y in enumerate(_yrange):
if args.verbose:
print("iy="+str(iy)+" y="+str(y))
2023-09-20 17:53:32 +00:00
c = pix[x,y] #get pixel
2024-02-04 21:12:23 +00:00
if args.verbose:
print("get pixel "+str(x)+","+str(y)+": color="+str(c))
2023-12-31 14:29:28 +00:00
if args.invert:
c=255-c
2023-09-20 17:53:32 +00:00
if (c[0]>127):
2023-12-31 13:01:44 +00:00
if output_col_write_binary_flag:
f.write("1")
2024-02-04 21:12:23 +00:00
columnValue+=pow(2,iy)
2023-09-20 17:53:32 +00:00
else:
2023-12-31 13:01:44 +00:00
if output_col_write_binary_flag:
f.write("0")
if output_col_write_int_flag:
f.write(str(columnValue))
2023-09-20 17:53:32 +00:00
2023-12-31 13:01:44 +00:00
f.write(output_col_post)
2023-09-20 17:53:32 +00:00
2023-12-31 13:01:44 +00:00
f.write(output_post)
2023-09-20 17:53:32 +00:00