3

Context:

  • I am using python 2.6.5

Goal:

  • Read a binary image file and represent it in-memory. Then run a checksum on it. Deliver the binary representation to be stored as a blob in mysql.

Comments:

  • I have read this SO thread.
  • I have looked at the struct module.
  • I also have bumped into the io module.
  • With all the available options, I am not certain which is the best solution. The BytesIO data structure seems to be suitable for my needs. Which one do you think will meet my requirements ?
Community
  • 1
  • 1
canadadry
  • 8,115
  • 12
  • 51
  • 68

2 Answers2

4

I'd recommend using PIL (Python Image Library)

http://effbot.org/imagingbook/pil-index.htm

Save it down to a string and then write to the db. Then you can use the string butter interface to PIL to read it back out.

Jonathan Root
  • 535
  • 2
  • 14
  • 31
Matt Alcock
  • 12,399
  • 14
  • 45
  • 61
0
>>> from binascii import crc32
>>> with open(filename, "rb") as f:
...     data = f.read()
...
>>> crc32(data)
361260080
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • This calculates a checksum of the file, not the checksum of image data. . This means it will generate a different checksum for 2 .pngs with identical image but different metadata. Might be relevant – Kos Aug 11 '13 at 14:57