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.