From 3f71536be1a17ec26e8498f536ce23d58367d8d1 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 9 Nov 2022 23:59:16 +0100 Subject: [PATCH] add converter for black and white images --- imageconverter/img2array.py | 84 +++++++++++++++++++++++++++++++++++++ src/main.cpp | 44 +++++++++++++++---- 2 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 imageconverter/img2array.py diff --git a/imageconverter/img2array.py b/imageconverter/img2array.py new file mode 100644 index 0000000..226df6f --- /dev/null +++ b/imageconverter/img2array.py @@ -0,0 +1,84 @@ +from PIL import Image +import math +import argparse +import os.path + +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('-n', '--bytesperline') +parser.add_argument('-b', '--lsbfirst', action='store_true') +parser.add_argument('-i', '--invert', action='store_true') + +args = parser.parse_args() + +lsbfirst=args.lsbfirst +invert=args.invert + +bytesperline=16 +if args.bytesperline is not None: + bytesperline=args.bytesperline #for output formatting + + +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 + + +array=[] #array with every element a byte + +thresh=128 #threshold brightness + +def calculateDistance(x1,y1,x2,y2): + dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) + return dist + +temp_byte=0 +temp_byte_pos=0 +for y in range(im.size[1]): + for x in range(im.size[0]): + c = pix[x,y] #get pixel + r=c[0] + g=c[1] + b=c[2] + if invert: + r=255-r + g=255-g + b=255-b + + if ((r+g+b)/3 < thresh ): #black + if lsbfirst: + temp_byte+=1<=8: #finished assemblying byte + array.append(temp_byte) + temp_byte_pos=0 #reset + temp_byte=0 + + +if os.path.isfile(args.filename+'.txt'): + print("Outputfile "+args.filename+".txt exists") + exit() + +with open(args.filename+'.txt', 'w') as f: + f.write("const unsigned char gImage_4in2[] = {") + f.write('\r\n') + counter=0 + for a in array: #for every byte + f.write("0X{:02X}".format(a)+',') #Example output: 0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF, + counter+=1 + if counter>=bytesperline: + f.write('\r\n') + counter=0 + f.write("};") + + + + + diff --git a/src/main.cpp b/src/main.cpp index 39b681d..9b422a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "EPD.h" #include "GUI_Paint.h" #include +#include "ImageData.h" void setup() { @@ -20,17 +21,44 @@ void setup() EPD_4IN2_Clear(); //flashed black 2 times. long, short + + //Create a new image cache + UBYTE *BWImage; + /* you have to edit the startup_stm32fxxx.s file and set a big enough heap size */ + UWORD BWImagesize = ((EPD_4IN2_WIDTH % 8 == 0) ? (EPD_4IN2_WIDTH / 8 ) : (EPD_4IN2_WIDTH / 8 + 1)) * EPD_4IN2_HEIGHT; + if ((BWImage = (UBYTE *)malloc(BWImagesize)) == NULL) { + printf("Failed to apply for black memory...\r\n"); + while (1); + } + printf("Paint_NewImage\r\n"); + Paint_NewImage(BWImage, EPD_4IN2_WIDTH, EPD_4IN2_HEIGHT, 0, WHITE); + + + printf("show image for array\r\n"); + Paint_SelectImage(BWImage); + Paint_Clear(WHITE); + Paint_DrawBitMap(gImage_4in2); //to convert image use: python3 img2array.py -i image.png + EPD_4IN2_Display(BWImage); + free(BWImage); + BWImage=NULL; + + DEV_Delay_ms(10000); + + + + + // ########## Grayscale Serial.println("e-Paper init 4gray"); DEV_Delay_ms(1000); EPD_4IN2_Init_4Gray(); - UBYTE *BlackImage; - UWORD Imagesize = ((EPD_4IN2_WIDTH % 8 == 0) ? (EPD_4IN2_WIDTH / 4 ) : (EPD_4IN2_WIDTH / 4 + 1)) * EPD_4IN2_HEIGHT; - if ((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL) { + UBYTE *GSImage; + UWORD GSImagesize = ((EPD_4IN2_WIDTH % 8 == 0) ? (EPD_4IN2_WIDTH / 4 ) : (EPD_4IN2_WIDTH / 4 + 1)) * EPD_4IN2_HEIGHT; + if ((GSImage = (UBYTE *)malloc(GSImagesize)) == NULL) { printf("Failed to apply for black memory...\r\n"); while (1); } - Paint_NewImage(BlackImage, EPD_4IN2_WIDTH, EPD_4IN2_HEIGHT, 0, WHITE); - Paint_SelectImage(BlackImage); + Paint_NewImage(GSImage, EPD_4IN2_WIDTH, EPD_4IN2_HEIGHT, 0, WHITE); + Paint_SelectImage(GSImage); Paint_SetScale(4); @@ -73,7 +101,7 @@ void setup() for (uint16_t y=i%2;y