add converter for black and white images
This commit is contained in:
parent
6f876abc94
commit
3f71536be1
|
@ -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<<temp_byte_pos
|
||||
else:
|
||||
temp_byte+=1<<(7-temp_byte_pos)
|
||||
|
||||
temp_byte_pos+=1
|
||||
if temp_byte_pos>=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("};")
|
||||
|
||||
|
||||
|
||||
|
||||
|
44
src/main.cpp
44
src/main.cpp
|
@ -6,6 +6,7 @@
|
|||
#include "EPD.h"
|
||||
#include "GUI_Paint.h"
|
||||
#include <stdlib.h>
|
||||
#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<EPD_4IN2_HEIGHT;y=y+2) {
|
||||
Paint_DrawLine(0, y, EPD_4IN2_WIDTH, y, BLACK, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
|
||||
}
|
||||
EPD_4IN2_4GrayDisplay(BlackImage);
|
||||
EPD_4IN2_4GrayDisplay(GSImage);
|
||||
DEV_Delay_ms(5000);
|
||||
}
|
||||
|
||||
|
@ -82,8 +110,8 @@ void setup()
|
|||
|
||||
printf("Goto Sleep...\r\n");
|
||||
EPD_4IN2_Sleep();
|
||||
free(BlackImage);
|
||||
BlackImage = NULL;
|
||||
free(GSImage);
|
||||
GSImage = NULL;
|
||||
}
|
||||
|
||||
/* The main loop -------------------------------------------------------------*/
|
||||
|
|
Loading…
Reference in New Issue