working image to bitmap converter for 2 bit grayscale
This commit is contained in:
parent
3f71536be1
commit
a87fe36ac2
|
@ -3,6 +3,28 @@ import math
|
||||||
import argparse
|
import argparse
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
|
||||||
|
def swapBits(number, nbits):
|
||||||
|
if nbits==2:
|
||||||
|
bitstring = '{0:02b}'.format(number)
|
||||||
|
if nbits==3:
|
||||||
|
bitstring = '{0:03b}'.format(number)
|
||||||
|
if nbits==4:
|
||||||
|
bitstring = '{0:04b}'.format(number)
|
||||||
|
if nbits==5:
|
||||||
|
bitstring = '{0:05b}'.format(number)
|
||||||
|
if nbits==6:
|
||||||
|
bitstring = '{0:06b}'.format(number)
|
||||||
|
if nbits==7:
|
||||||
|
bitstring = '{0:07b}'.format(number)
|
||||||
|
if nbits==8:
|
||||||
|
bitstring = '{0:08b}'.format(number)
|
||||||
|
|
||||||
|
bitstring=bitstring[::-1]
|
||||||
|
|
||||||
|
return int(bitstring, 2)
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog = 'Image Edit Script',
|
prog = 'Image Edit Script',
|
||||||
description = 'Manipulate or extract information from an image file',
|
description = 'Manipulate or extract information from an image file',
|
||||||
|
@ -10,17 +32,29 @@ parser = argparse.ArgumentParser(
|
||||||
|
|
||||||
parser.add_argument('filename') # positional argument
|
parser.add_argument('filename') # positional argument
|
||||||
parser.add_argument('-n', '--bytesperline')
|
parser.add_argument('-n', '--bytesperline')
|
||||||
parser.add_argument('-b', '--lsbfirst', action='store_true')
|
parser.add_argument('-d', '--bitdepth')
|
||||||
|
parser.add_argument('-b', '--lsbfirst', action='store_true') #lsb first for the whole byte
|
||||||
|
parser.add_argument('-c', '--colorlsbfirst', action='store_true') #lsb first for a single color
|
||||||
parser.add_argument('-i', '--invert', action='store_true')
|
parser.add_argument('-i', '--invert', action='store_true')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
lsbfirst=args.lsbfirst
|
lsbfirst=args.lsbfirst
|
||||||
|
colorlsbfirst=args.colorlsbfirst
|
||||||
invert=args.invert
|
invert=args.invert
|
||||||
|
|
||||||
|
|
||||||
|
bitdepth=1
|
||||||
|
if args.bitdepth is not None:
|
||||||
|
_bd=int(int(args.bitdepth))
|
||||||
|
if _bd!=1 and _bd!=2 and _bd!=4 and _bd!=8:
|
||||||
|
print("bitdepth needs to be 1, 2, 4, or 8")
|
||||||
|
exit()
|
||||||
|
bitdepth=_bd
|
||||||
|
|
||||||
bytesperline=16
|
bytesperline=16
|
||||||
if args.bytesperline is not None:
|
if args.bytesperline is not None:
|
||||||
bytesperline=args.bytesperline #for output formatting
|
bytesperline=int(args.bytesperline) #for output formatting
|
||||||
|
|
||||||
|
|
||||||
im = Image.open(args.filename) # Can be many different formats.
|
im = Image.open(args.filename) # Can be many different formats.
|
||||||
|
@ -30,7 +64,6 @@ print(im.size) # Get the width and hight of the image for iterating over
|
||||||
|
|
||||||
array=[] #array with every element a byte
|
array=[] #array with every element a byte
|
||||||
|
|
||||||
thresh=128 #threshold brightness
|
|
||||||
|
|
||||||
def calculateDistance(x1,y1,x2,y2):
|
def calculateDistance(x1,y1,x2,y2):
|
||||||
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
|
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
|
||||||
|
@ -44,24 +77,41 @@ for y in range(im.size[1]):
|
||||||
r=c[0]
|
r=c[0]
|
||||||
g=c[1]
|
g=c[1]
|
||||||
b=c[2]
|
b=c[2]
|
||||||
|
|
||||||
|
_a=int((r+g+b)/3 )
|
||||||
|
brightness=int(_a)
|
||||||
|
|
||||||
|
brightnessreduced=brightness>>(8-bitdepth) #reduce to bitdepth
|
||||||
|
|
||||||
|
brightnessreduced_byte=brightnessreduced<<(8-bitdepth) #scale back to byte
|
||||||
|
pix[x,y]=(brightnessreduced_byte,brightnessreduced_byte,brightnessreduced_byte)
|
||||||
|
|
||||||
|
|
||||||
if invert:
|
if invert:
|
||||||
r=255-r
|
brightnessreduced=pow(2,bitdepth)-1-brightnessreduced #only invert txt output
|
||||||
g=255-g
|
|
||||||
b=255-b
|
|
||||||
|
|
||||||
if ((r+g+b)/3 < thresh ): #black
|
if colorlsbfirst:
|
||||||
if lsbfirst:
|
brightnessreduced=swapBits(brightnessreduced,bitdepth)
|
||||||
temp_byte+=1<<temp_byte_pos
|
|
||||||
else:
|
|
||||||
temp_byte+=1<<(7-temp_byte_pos)
|
|
||||||
|
|
||||||
temp_byte_pos+=1
|
|
||||||
|
temp_byte+=brightnessreduced<<temp_byte_pos #add to current byte
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
temp_byte_pos+=bitdepth
|
||||||
|
#print("temp byte "+str(temp_byte)+" pos="+str(temp_byte_pos)+" brightnessreduced="+str(brightnessreduced))
|
||||||
if temp_byte_pos>=8: #finished assemblying byte
|
if temp_byte_pos>=8: #finished assemblying byte
|
||||||
|
if lsbfirst: #swap bit order
|
||||||
|
temp_byte=swapBits(temp_byte,8)
|
||||||
array.append(temp_byte)
|
array.append(temp_byte)
|
||||||
|
#print("appending "+str(temp_byte))
|
||||||
temp_byte_pos=0 #reset
|
temp_byte_pos=0 #reset
|
||||||
temp_byte=0
|
temp_byte=0
|
||||||
|
|
||||||
|
|
||||||
|
im.save(args.filename+'_result'+'.png')
|
||||||
|
|
||||||
|
|
||||||
if os.path.isfile(args.filename+'.txt'):
|
if os.path.isfile(args.filename+'.txt'):
|
||||||
print("Outputfile "+args.filename+".txt exists")
|
print("Outputfile "+args.filename+".txt exists")
|
||||||
exit()
|
exit()
|
||||||
|
@ -71,14 +121,10 @@ with open(args.filename+'.txt', 'w') as f:
|
||||||
f.write('\r\n')
|
f.write('\r\n')
|
||||||
counter=0
|
counter=0
|
||||||
for a in array: #for every byte
|
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,
|
hexstring="0X{:02X}".format(a)
|
||||||
|
f.write(hexstring+',') #Example output: 0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
|
||||||
counter+=1
|
counter+=1
|
||||||
if counter>=bytesperline:
|
if counter>=bytesperline:
|
||||||
f.write('\r\n')
|
f.write('\r\n')
|
||||||
counter=0
|
counter=0
|
||||||
f.write("};")
|
f.write("};")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 8.1 KiB |
6001
src/ImageData.cpp
6001
src/ImageData.cpp
File diff suppressed because it is too large
Load Diff
14
src/main.cpp
14
src/main.cpp
|
@ -21,10 +21,10 @@ void setup()
|
||||||
EPD_4IN2_Clear(); //flashed black 2 times. long, short
|
EPD_4IN2_Clear(); //flashed black 2 times. long, short
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
//Create a new image cache
|
//Create a new image cache
|
||||||
UBYTE *BWImage;
|
UBYTE *BWImage;
|
||||||
/* you have to edit the startup_stm32fxxx.s file and set a big enough heap size */
|
// 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;
|
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) {
|
if ((BWImage = (UBYTE *)malloc(BWImagesize)) == NULL) {
|
||||||
printf("Failed to apply for black memory...\r\n");
|
printf("Failed to apply for black memory...\r\n");
|
||||||
|
@ -34,15 +34,18 @@ void setup()
|
||||||
Paint_NewImage(BWImage, EPD_4IN2_WIDTH, EPD_4IN2_HEIGHT, 0, WHITE);
|
Paint_NewImage(BWImage, EPD_4IN2_WIDTH, EPD_4IN2_HEIGHT, 0, WHITE);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
printf("show image for array\r\n");
|
printf("show image for array\r\n");
|
||||||
Paint_SelectImage(BWImage);
|
Paint_SelectImage(BWImage);
|
||||||
Paint_Clear(WHITE);
|
Paint_Clear(WHITE);
|
||||||
Paint_DrawBitMap(gImage_4in2); //to convert image use: python3 img2array.py -i image.png
|
Paint_DrawBitMap(gImage_4in2); //to convert image use: python3 img2array.py -i image.png
|
||||||
EPD_4IN2_Display(BWImage);
|
EPD_4IN2_Display(BWImage);
|
||||||
|
|
||||||
free(BWImage);
|
free(BWImage);
|
||||||
BWImage=NULL;
|
BWImage=NULL;
|
||||||
|
|
||||||
DEV_Delay_ms(10000);
|
DEV_Delay_ms(10000);
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -61,6 +64,11 @@ void setup()
|
||||||
Paint_SelectImage(GSImage);
|
Paint_SelectImage(GSImage);
|
||||||
Paint_SetScale(4);
|
Paint_SetScale(4);
|
||||||
|
|
||||||
|
Paint_DrawBitMap(gImage_4in2);
|
||||||
|
EPD_4IN2_4GrayDisplay(GSImage);
|
||||||
|
|
||||||
|
DEV_Delay_ms(10000);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Paint_Clear(WHITE);
|
Paint_Clear(WHITE);
|
||||||
|
@ -90,6 +98,7 @@ void setup()
|
||||||
Paint_DrawString_CN(220, 120, "微雪电子", &Font24CN, GRAY4, GRAY1);
|
Paint_DrawString_CN(220, 120, "微雪电子", &Font24CN, GRAY4, GRAY1);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
Serial.println("e-Paper Start Lines Test");
|
Serial.println("e-Paper Start Lines Test");
|
||||||
DEV_Delay_ms(1000);
|
DEV_Delay_ms(1000);
|
||||||
for (uint16_t i=0;i<100;i++) {
|
for (uint16_t i=0;i<100;i++) {
|
||||||
|
@ -104,6 +113,7 @@ void setup()
|
||||||
EPD_4IN2_4GrayDisplay(GSImage);
|
EPD_4IN2_4GrayDisplay(GSImage);
|
||||||
DEV_Delay_ms(5000);
|
DEV_Delay_ms(5000);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
EPD_4IN2_Init_Fast();
|
EPD_4IN2_Init_Fast();
|
||||||
EPD_4IN2_Clear();
|
EPD_4IN2_Clear();
|
||||||
|
|
Loading…
Reference in New Issue