python 3.x - Why does printing a png file in Python3 result in extra characters in IDAT chunk? -


so i've been testing out reading uncompressed png files following code in python3:

f = open(r'img1.png', 'rb') pixel = f.read() print(pixel) 

however results give strange additional characters besides hex pairs expect in idat chunk:

b'\x89png\r\n\x1a\n\x00\x00\x00\rihdr\x00\x00\x00\x02\x00\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9as\x00\x00\x00\x19idat\x08\x1d\x01\x0e\x00\xf1\xff\x00\x00\x00\x00\x00\xff\xff\x01\x00\xff\xff\x13\x90\x90\x1b\xe4 \x0510o\xffc \x00\x00\x00\x00iend\xaeb`\x82' [finished in 0.1s]

any idea is? under assumption in idat when data uncompressed pixel data in hex pairs. i've searched both stackoverflow/online looked through documentation png without luck.

here link image using test (it's 4 pixels): img1.png

fyi i'm running tests via archlinux if helps.

i suspect senshin's comment on spot.

when printing binary data, python prints printable ascii characters such, , remainder bytes \xhh h hexadecimal digit.

hence subsequence \xe4\x0510o\xffc\x00 sequence of following 8 bytes:

\xe4 \x05 1     ----> ascii character '1', equivalent \x31 0     ----> etc o \xff c \x00 

oc course, in case we'd prefer bytes printed hexadecimal, because bytes intended understood binary data, not text characters. python cannot guess that.

if want print bytes hexadecimal, can see recipes here. aware, however, in cases ascii output prefer (the 'png' sequence @ start, , chunks identifiers...), there no silver bullet.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -