5

I am trying to read a JPG image in Python.

So far i have:

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

print "'%s'"% (ima)

It reads 16 bytes and displays the string in 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 it read 16 bytes. Why can I not read more than 16 bytes of the jpeg image?

Gewthen
  • 408
  • 4
  • 12
andrepcg
  • 1,301
  • 7
  • 26
  • 45

2 Answers2

11

Two issues here:

  1. Set read mode to binary. This way file.read function won't try to convert '\r\n' sequences.

  2. You're trying to print NULL-terminated string to the console. print function finds first zero character in your string and terminates. Use binascii.hexlify to convert it to the hex:


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

print "%s" % (binascii.hexlify(ima))
yurymik
  • 2,194
  • 20
  • 14
  • 4
    Rather than that *horribly* named function in binascii, `repr()` might be more useful here. – Greg Hewgill Jan 12 '11 at 01:24
  • @yurymik does the `(16)` argument mean it reads in 16 bytes, or that it reads it in units of 16 bits? – AllTradesJack Jul 31 '14 at 00:53
  • 1
    @joshsvoss: file.read([size]) Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). https://docs.python.org/2/library/stdtypes.html – yurymik Aug 06 '14 at 18:58
  • use f.read() to read the complete file , no need to find the size of the file or loop over f.readline() to see the output of everyline – kkk Jan 12 '16 at 09:20
5

You probably need to set the open mode to binary:

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

See this similar question for a more thorough description.

Community
  • 1
  • 1
brettkelly
  • 27,655
  • 8
  • 56
  • 72