StringIO MySQL data blob Image problem

Tim Golden mail at timgolden.me.uk
Wed Sep 5 09:10:03 EDT 2007


dimitri pater wrote:
> I am trying to insert an image, which is stored as a blob in MySQL,
> into a table using Reportlab.

[... snip stuff involving StringIO and getvalue / tostring etc. ...]

> This is also the string I see in the table, in stead of the actual image.

[.. snip more stuff ...]

> that returns:
> ÿØÿàúlbo¤qÁ5¼–Ò\¸•£ˆˆ‡Y|Aø—­,ñé–ú…"ìâm3Z¸ŒÁfêñ""NÔ,­¡¾ÚÀIæÃt"Äb5Î@xqô'ÍkÕÁå¼sàßd˜ª²«ÍÉ1ØœÏ
> ‡^ÖJ*™C(r)ë{:tâ¥_‡Çâ–´°joÙÃ
> ¿C(c)¯äÜ[)¯gN«ÃæXßi etc... etc...
> and return an UnicodeDecodeError when I try to insert this into the table

Well, I'm mystified. Not by your results: that exactly what I
expected to get, but because you're doing everything *except*
manipulating an image and putting it into a PDF via ReportLab.

Just in case you're under any other impression, there's no such
thing as "a graphical image" in a database or in computer memory.
There's just a bunch of bytes which -- given the right libraries
-- can be represented as an image. Given other libraries, such
as the ones which print them to a text console, they can be
represented as a stream of apparent gibberish. You need to
use the former; you're using the latter.

It's a while since I did anything with ReportLab but I seem to
remember that you can pass it an image filename and, (possibly
as long as you have the Python Imaging Library installed),
it will insert it into the PDF you're building as a flowable.

Assuming I'm right, get hold of the bytes in your MySQL query,
save them as "temp.jpg" or whatever, and then pass that filename
along to ReportLab. Feel free to come back and ask if those
instructions aren't clear enough.

TJG



More information about the Python-list mailing list