94 lines
2.6 KiB
Python
94 lines
2.6 KiB
Python
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
|
|
parser.add_argument('-f', '--format', choices=['firmware','nodered','nodered_int'], default='firmware', help='Output format') # option that takes a value
|
|
parser.add_argument('-v', '--verbose', action='store_true') # on/off flag
|
|
|
|
args = parser.parse_args()
|
|
print(args.filename, args.output, args.format,args.verbose)
|
|
|
|
|
|
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
|
|
print(pix[10,10]) # Get the RGBA Value of the a pixel of an image
|
|
|
|
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
|
|
output_pre="msg.payload=\"\\\n"
|
|
output_col_preL=""
|
|
output_col_writeCol_flag=False
|
|
output_col_preR=""
|
|
output_col_write_binary_flag=True
|
|
output_col_post="\\\n"
|
|
output_post="\";\nreturn msg;"
|
|
elif args.format=='nodered_int': #Nodered Int
|
|
output_pre="msg.payload=\"\\\n"
|
|
output_col_preL=""
|
|
output_col_writeCol_flag=False
|
|
output_col_preR=""
|
|
output_col_write_int_flag=True
|
|
output_col_post=","
|
|
output_post="\";\nreturn msg;"
|
|
|
|
|
|
|
|
|
|
|
|
def calculateDistance(x1,y1,x2,y2):
|
|
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
|
|
return dist
|
|
|
|
with open('result.txt', 'w') as f:
|
|
f.write(output_pre)
|
|
for x in range(im.size[0]):
|
|
f.write(output_col_preL)
|
|
if output_col_writeCol_flag:
|
|
f.write(str(x))
|
|
f.write(output_col_preR)
|
|
|
|
columnValue=0
|
|
for y in reversed(range(im.size[1])):
|
|
c = pix[x,y] #get pixel
|
|
if (c[0]>127):
|
|
if output_col_write_binary_flag:
|
|
f.write("1")
|
|
columnValue+=pow(2,y)
|
|
else:
|
|
if output_col_write_binary_flag:
|
|
f.write("0")
|
|
|
|
if output_col_write_int_flag:
|
|
f.write(str(columnValue))
|
|
|
|
|
|
f.write(output_col_post)
|
|
|
|
f.write(output_post)
|
|
|
|
|
|
|