简体   繁体   English

将图像转换为基于像素(250x122px)的十六进制代码文件的代码

[英]Code to convert Image into hex code files based on pixel (250x122px)

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.我正在编写代码以将当前使用 250px 宽度和 122px 高度的指定大小的图像文件转换为十六进制代码文件。

Python Code I wrote:我写的 Python 代码:

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:此代码的目的是读取每个像素的第 1 行所有像素,第 2 行所有像素,依此类推,将每个值转换为:

Case 1: bfile 1 for white and 0 for Black/Red pixel案例 1: bfile 1 用于白色,0 用于黑色/红色像素

Case 2: rifle 1 for white/black and 0 for red pixel (Only Red consider dark)案例 2:步枪1 代表白色/黑色,0 代表红色像素(只有红色认为暗)

Adding all values to the binary of 8 bit, then output for the corresponding file in 0x{:02x} hex format.将所有值添加到 8 位二进制文​​件中,然后以0x{:02x}十六进制格式输出对应文件。

Image using this for bfile:将其用于 bfile 的图像:

在此处输入图像描述

For rifle:对于步枪:

在此处输入图像描述

original:原来的:

在此处输入图像描述

File output of both images from python code:来自 python 代码的两个图像的文件输出:

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

https://drive.google.com/file/d/1aanKNAuro3do1nQ4MBewAsyGbOfNkyIE/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:当我在硬件中使用转换后的十六进制代码时,结果并不令人满意:

在此处输入图像描述

But when I use this online converter with same images:但是当我使用这个具有相同图像的在线转换器时:

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

The converted hex file gives almost perfect results:转换后的 hex 文件给出了几乎完美的结果:

在此处输入图像描述

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).所以我认为问题不在于硬件而只是十六进制文件,因为我想通过代码而不是手动执行相同的操作,即使只是将单色图像转换为两个十六进制文件(bfile 和步枪)。 What can I change in my python code to get right results as online converter?作为在线转换器,我可以在我的 python 代码中进行哪些更改以获得正确的结果? Once I get correct result for two separate images, I'll use similar for just one original image.一旦我为两个单独的图像获得正确的结果,我将只对一个原始图像使用相似的结果。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM