Home > database >  Code to convert Image into hex code files based on pixel (250x122px)
Code to convert Image into hex code files based on pixel (250x122px)

Time:07-20

I'm writing a code to convert an image file of a specified size currently using 250px width and 122px height to hex code file.

Python Code I wrote:

from PIL import Image

imgb = Image.open("George_Sample_bw.jpg")
imgr = Image.open("George_Sample_rw.jpg")
#width, height = img.size
#print(width, "  ", height)

pxb = imgb.load()
pxr= imgr.load()
fb = open("bfile.txt", "w")
fr= open("rfile.txt", "w")
for i in range(122):
    b = 0b00000000
    r = 0b00000000
    for j in range(250):
        if(not(pxr[j, i][0]>150 and pxr[j,i][1]<80 and pxr[j,i][2]<80)):
            #print("Red")
            r|=1<<(j % 8)
        if (pxb[j,i][0]>180 and pxb[j,i][1]>180 and pxb[j,i][2]>180):
            #print(" Black")
            b|=1<<(j % 8)
        if(j%8==0):
            fb.write("0x{:02x}".format(b))
            fr.write("0x{:02x}".format(r))
            b = 0b00000000
            r = 0b00000000
            fb.write(', ')
            fr.write(', ')
    fb.write('\n')
    fr.write('\n')

The purpose of this code is to read each pixel 1st row all pixel, 2nd row all pixel and so on, converting each value to:

Case 1: bfile 1 for white and 0 for Black/Red pixel

Case 2: rifle 1 for white/black and 0 for red pixel (Only Red consider dark)

Adding all values to the binary of 8 bit, then output for the corresponding file in 0x{:02x} hex format.

Image using this for bfile:

enter image description here

For rifle:

enter image description here

original:

enter image description here

File output of both images from python code:

https://drive.google.com/file/d/1CcedU79l3N4DG0OfArL_pDKoX484AJo5/view?usp=sharing

https://drive.google.com/file/d/1aanKNAuro3do1nQ4MBewAsyGbOfNkyIE/view?usp=sharing

When I use the converted hex code in my hardware I don't satisfactory results:

enter image description here

But when I use this online converter with same images:

https://www.digole.com/tools/PicturetoC_Hex_converter.php

The converted hex file gives almost perfect results:

enter image description here

So I think problem is not with the hardware it just the hex files, as I want to do same through code not manually, even just with single colored image to two hex files (bfile and rifle). What can I change in my python code to get right results as online converter? Once I get correct result for two separate images, I'll use similar for just one original image.

CodePudding user response:

from PIL import Image

imgb = Image.open("image_bw.jpg")
imgr = Image.open("image_rw.jpg")

width, height = imgb.size
pxb = imgb.load()
pxr= imgr.load()
fb = open("bfile.h", "w")
fb.write("uint8_t bfile[4001] = {\n")
fr= open("rfile.h", "w")
fr.write("uint8_t rfile[4001] = {\n")
flag=0
for i in range(height):
    b = 0b00000000
    r = 0b00000000
    for j in range(width):
        if (j% 8== 0 and j>0):
            fb.write("0x{:02x}".format(b))
            fr.write("0x{:02x}".format(r))
            fb.write(', ')
            fr.write(', ')
            b = 0b00000000
            r = 0b00000000
        if (pxr[j, i][0] > 190 and pxr[j, i][1] > 190 and pxr[j, i][2] > 190):#White Pixel
            r|=1<<(7-(j % 8))
        if ((pxb[j, i][0] > 190 and pxb[j, i][1] > 190 and pxb[j, i][2] > 190) ):##White Pixel
            b|=1<<(7-(j % 8))
    fb.write("0x{:02x}".format(b))
    fr.write("0x{:02x}".format(r))
    fb.write(', ')
    fr.write(', ')
    fb.write('\n')
    fr.write('\n')
fb.write("};")
fr.write("};")
fb.close()
fr.close()
  • Related