iqlou.thought
iqlou.thought

Reputation: 3

Image to Hexidecimal Data Conversion in Python

Using Python I am trying to convert each pixel of a B/W image to hex and store it in a '.txt' file. I have to use C for my application so its easier to read from a text file then do the entire conversion in C code. I have tweaked some example code from online, but I am still not very experienced with using Python.

When I run my python script it only produces two hex values instead of eight. At first I thought this was from the image being B/W, but an RGB image should contain 0x000000 for black and 0xFFFFFF for white.

Python Script

from binascii import hexlify
import re

hexValNew = ''
placeHolder ='0'

file = open('Frame2.txt', 'w') #Create txt file for data

with open('Frame2.png', 'rb') as f:
    binVal = f.read(1)
    while len(binVal) != 0:
        hexVal = hex(ord(binVal))
        hexValNew = hexVal[2:4] #Remove "0x" from hex() process
        hexValString = str(hexValNew)

        if len(hexValString) == 1:
            hexValString = placeHolder + hexValString
        print hexVal

        #print(hexValString) #Test Line
        file.write(hexValString) #Write adjusted hex value to txt file
        binVal = f.read(1)

file.close() #Close txt file

Section of ./a.out

ff 00 62 26 e0 c4 a2 d7 c2 90 00 00 49 45 4e 44 ae 42 60 82

From what I understand, the value should be have eight digits returned and not just two.

Upvotes: 0

Views: 1180

Answers (3)

nickglazer
nickglazer

Reputation: 145

You cannot open a PNG and treat the data as raw image data. For example, the first 8 bytes of a PNG are known as the PNG signature, which are always the same for every PNG.

You should use a library such as Pillow or OpenCV to open the image, get the image data, and write it out in raw to a file. It would be easier to make a raw file rather than a file of hex strings that you will have to parse again in your C program.

Upvotes: 0

fmw42
fmw42

Reputation: 53164

In ImageMagick on Unix, you can do the following:

convert rose.png:[5x5] txt: | tail -n +2 | sed 's/[ ][ ]*/ /g' | awk '{print $1,$3}'
0,0: #414042
1,0: #634040
2,0: #B53D30
3,0: #A54845
4,0: #998886
0,1: #6A6674
1,1: #C0494A
2,1: #E4312B
3,1: #DB403F
4,1: #98936E
0,2: #77796F
1,2: #CA948B
2,2: #A94D43
3,2: #603D31
4,2: #405937


The format is column,row: hex.

You can redirect that to a file if you want. I am not a Windows users. But there are likely Windows equivalents of the Unix commands. If not, then you can parse the output from the txt:. See https://imagemagick.org/Usage/files/#txt

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207818

I would suggest you don't bother writing any Python at all, but instead just use ImageMagick in Terminal to convert any PNG, JPEG or other format file to NetPBM format see Wikipedia description.

Look specifically at PGM format starting with P2 (ASCII) or P5 (binary) format. It is very simple to read with C. So to convert a PNG image to PGM P2 (ASCII) format, in Terminal use:

magick input.png -depth 8 -colorspace gray -compress none result.pgm

and look at your file with any text editor.

To convert a JPEG file to PGM P5 (binary) format, use:

magick input.jpg -depth 8 -colorspace gray result.pgm

Upvotes: 1

Related Questions