Last active
July 15, 2021 15:56
-
-
Save TamojitSaha/73adad5ff55f50adf6e7217fe48f623a to your computer and use it in GitHub Desktop.
bmp2array.py - A python script for genarting image arrays from 1-bit bitmaps for DPEG0290RW series Epaper display. This display has unique scannning method. The bits should be written vertically, but the data should be present horizontally in the image array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Test image 1: https://imgur.com/ZL9xyBy | |
Test image 2: https://imgur.com/MtHCRrb | |
Test image 3: https://imgur.com/J4ULXO0 | |
Test image 4: https://imgur.com/jHyfTJa | |
@author: Tamojit Saha <https://github.com/TamojitSaha> | |
''' | |
from PIL import Image | |
import numpy as np | |
import sys | |
np.set_printoptions(threshold=sys.maxsize) | |
def add_padding(hexString: str, hexLen: int)->str: | |
if hexLen > len(hexString): | |
padded_str = '0x'+'0'* (hexLen - len(hexString))+hexString[2:] | |
return padded_str | |
else: print("Invalid entry") | |
def bmp2array(filename: str)->None: | |
im = Image.open(filename) | |
ar = (~np.array(im).astype(bool)).astype(int) | |
h,w = ar.shape | |
ar1 = ar.copy() | |
# print(ar) | |
# print("\n") | |
# print(ar.shape) | |
final = [0]*(int(h/8)*w) | |
d = [] | |
for k in range(0,w): | |
for i in range(0,h): | |
d.append(ar1[:][i][k]) | |
# print(d) | |
a = np.asarray_chkfinite(d) | |
c = np.array_split(a,h/8) | |
factor = len(c) | |
# print(c) | |
if len(c)>1: | |
for j in range(factor): | |
hexData = hex(int("".join(str(m) for m in c[j]),2))#base:2 | |
if(len(hexData)==3) : | |
data = add_padding(hexData,4) | |
final[k+(w*j)] = data | |
else: final[k+(w*j)] = hexData | |
else: | |
c0 = hex(int("".join(str(i) for i in c[0]),2)) | |
final[k]=c0 | |
d.clear() | |
# print(final, len(final)) | |
hexString = "const unsigned char gImage_"+filename.split('.')[0]+"["+str(int(h/8)*w)+"] = {\r" | |
for i in range (0, len(final)): | |
hexString += str(final[i])+", " | |
if i%16 == 0: | |
hexString +='\r' | |
hexString = hexString[:-2] | |
hexString +='\r};' | |
# print(hexString) | |
m_fileName = filename.split('.')[0]+'_py.h' | |
outfile = open(m_fileName,"w") | |
outfile.write(hexString) | |
outfile.close() | |
print(f"File generated: {m_fileName}") | |
if __name__ == "__main__": | |
image_name = "56x34.bmp" | |
bmp2array(image_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment