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
  • 7,605
  • 11
  • 49
  • 64

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
  • 534
  • 2
  • 12
  • 30
Matt Alcock
  • 11,601
  • 13
  • 44
  • 59
0
>>> from binascii import crc32
>>> with open(filename, "rb") as f:
...     data = f.read()
...
>>> crc32(data)
361260080
Janne Karila
  • 22,762
  • 5
  • 50
  • 92
  • 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