Why can't I read more than 16 bytes of a JPEG file in Python?

I am trying to read a jpg image in Python.

So far, I:

f = open("test.jpg")
ima = f.read(16)

print "'%s'"% (ima)

It reads 16 bytes and displays a string in the console, but it looks like I cannot display more than 32 bytes. Why?

When it tries to read 32 or more bytes, the output will be the same as when reading 16 bytes. Why can't I read more than 16 bytes of jpeg image?

+5
source share
2 answers

Two questions here:

  • Set the reading mode to binary. Thus, the function file.readwill not try to convert sequences \ r \ n.

  • NULL . print . binascii.hexlify, :


f = open("test.jpg", "rb")
ima = f.read(16)

print "%s" % (binascii.hexlify(ima))
+11

, , :

f = open("test.jpg", "rb") # 'rb' here means "read mode, binary"

. .

+5

All Articles