Re: StringIO MySQL data blob Image problem

2007-09-06 Thread Tim Golden
dimitri pater wrote:
> Hi,
> the following code works when inserting images in reportlab tables:
> 
> (result4 is a query result)
> a=0
> for i in result4:
>cfoto = StringIO()
>cfoto.write(result4[a][9].tostring())
>dfoto = cfoto.getvalue()
>fileFoto = open(str(a)+'temp.jpg','wb')
>fileFoto.write(dfoto)
>fileFoto.close()
>foto = Image(str(a)+'temp.jpg')
>a+=1
> 
>   Do stuff here (insert the Image)
> 
> The problem with this code is that I need to create a unique file
> (str(a)+'temp.jpg'), I tried to use a single temp.jpg but it kept
> using the data from the first record. Tried flush(), truncate(0), but
> it didn't work. (My mistake probably ;-)
> But the images show in the PDF so that's fine for now.

You've obviously already worked this one out, which is
great. But if I might just comment on the code a little
bit, in a spirit of helpful criticism:

* It's not clear (to me) why you're using a StringIO
at all. I'm not familiar with MySQL in particular, but
in general, database interface modules will return a Python
string or possibly a buffer object from a Blob field.
In other words, is there anything to stop you simply
writing "result4[a][9]" directly into a file?


# ... stuff leading up to:
blob = result4[a][9]
ofile = open ("temp.jpg", "wb")
ofile.write (blob) # (or, possibly, str (blob) if it's a buffer)
ofile.close ()


* Assuming you're using any recent version of Python,
you can save the extra counter by iterating over
enumerate (result4) which return a 0-based index
and the indexed item as a tuple. In fact, now I look
at it, you're doing the work twice. You're iterating
over result4 but doing nothing with the "i" which
is the result of the iteration. You might do better
with something like this:


#
# I've used "a", "i" to match your code,
# but more descriptive names are good, such
# as n_result, result or whatever fits your mind.
#
for a, i in enumerate (result4):
   blob = i[9]
   ofile = open ("%d-temp.jpg" % a, "wb")
   ofile.write (blob)
   ofile.close ()



* Finally, the tempfile module is a good one to use for
temporary files. (Although the xx-temp.jpg solution you
used may be perfectly acceptable for your needs).

In the above, I've avoided quite a few good-practice
issues, such as try-except blocks when writing a file,
or with-blocks in Python 2.5+. The code I've sketched
out is merely a rough-and-ready illustration of a
particular point, not a manual of best practice.

I suspect you may be newish to Python, if not to programming
in general. My comments above aren't intended to be
nitpicks to show up your ignorance, but hopefully hints
which might help you look at alternatives or consider
alternative idioms within Python itself.

Good luck with the PDFs!

TJG
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: StringIO MySQL data blob Image problem

2007-09-05 Thread dimitri pater
ah, sorry
a+=1 should be after 'Do stuff here'  of course...


On 9/5/07, dimitri pater <[EMAIL PROTECTED]> wrote:
> Hi,
> the following code works when inserting images in reportlab tables:
>
> (result4 is a query result)
> a=0
> for i in result4:
>cfoto = StringIO()
>cfoto.write(result4[a][9].tostring())
>dfoto = cfoto.getvalue()
>fileFoto = open(str(a)+'temp.jpg','wb')
>fileFoto.write(dfoto)
>fileFoto.close()
>foto = Image(str(a)+'temp.jpg')
>a+=1
>
>   Do stuff here (insert the Image)
>
> The problem with this code is that I need to create a unique file
> (str(a)+'temp.jpg'), I tried to use a single temp.jpg but it kept
> using the data from the first record. Tried flush(), truncate(0), but
> it didn't work. (My mistake probably ;-)
> But the images show in the PDF so that's fine for now.
>
> On 9/5/07, dimitri pater <[EMAIL PROTECTED]> wrote:
> > -- Forwarded message --
> > From: dimitri pater <[EMAIL PROTECTED]>
> > Date: Sep 5, 2007 9:13 PM
> > Subject: Re: StringIO MySQL data blob Image problem
> > To: Tim Golden <[EMAIL PROTECTED]>
> >
> >
> > > 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.
> > >
> > Dear Tim,
> > you are right of course, I have been trying to put the StringIO in a temp 
> > file:
> > cfoto=StringIO
> > cfoto.write(result[0][1].tostring())
> > dfoto=cfoto.getvalue()
> > fileFoto=open('temp.jpg','wb')
> > fileFoto.write(dfoto)
> >
> > and indeed, the blob from MySQL is saved as an image!
> > however,
> > foto= Image('temp.jpg')
> > and inserting foto into a table results in:
> > x = struct.unpack('B', image.read(1))
> > error: unpack str size does not match format
> > oh, well... still needs some work
> > BTW: I used 'local' images before (I mean they did not originate from
> > a DB), that worked well in Reportlab's tables.
> > Thanks, I am getting there (I think)
> >
> >
> > --
> > ---
> > You can't have everything. Where would you put it? -- Steven Wright
> > ---
> > please visit www.serpia.org
> >
>
>
> --
> ---
> You can't have everything. Where would you put it? -- Steven Wright
> ---
> please visit www.serpia.org
>


-- 
---
You can't have everything. Where would you put it? -- Steven Wright
---
please visit www.serpia.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: StringIO MySQL data blob Image problem

2007-09-05 Thread dimitri pater
Hi,
the following code works when inserting images in reportlab tables:

(result4 is a query result)
a=0
for i in result4:
   cfoto = StringIO()
   cfoto.write(result4[a][9].tostring())
   dfoto = cfoto.getvalue()
   fileFoto = open(str(a)+'temp.jpg','wb')
   fileFoto.write(dfoto)
   fileFoto.close()
   foto = Image(str(a)+'temp.jpg')
   a+=1

  Do stuff here (insert the Image)

The problem with this code is that I need to create a unique file
(str(a)+'temp.jpg'), I tried to use a single temp.jpg but it kept
using the data from the first record. Tried flush(), truncate(0), but
it didn't work. (My mistake probably ;-)
But the images show in the PDF so that's fine for now.

On 9/5/07, dimitri pater <[EMAIL PROTECTED]> wrote:
> -- Forwarded message --
> From: dimitri pater <[EMAIL PROTECTED]>
> Date: Sep 5, 2007 9:13 PM
> Subject: Re: StringIO MySQL data blob Image problem
> To: Tim Golden <[EMAIL PROTECTED]>
>
>
> > 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.
> >
> Dear Tim,
> you are right of course, I have been trying to put the StringIO in a temp 
> file:
> cfoto=StringIO
> cfoto.write(result[0][1].tostring())
> dfoto=cfoto.getvalue()
> fileFoto=open('temp.jpg','wb')
> fileFoto.write(dfoto)
>
> and indeed, the blob from MySQL is saved as an image!
> however,
> foto= Image('temp.jpg')
> and inserting foto into a table results in:
> x = struct.unpack('B', image.read(1))
> error: unpack str size does not match format
> oh, well... still needs some work
> BTW: I used 'local' images before (I mean they did not originate from
> a DB), that worked well in Reportlab's tables.
> Thanks, I am getting there (I think)
>
>
> --
> ---
> You can't have everything. Where would you put it? -- Steven Wright
> ---
> please visit www.serpia.org
>


-- 
---
You can't have everything. Where would you put it? -- Steven Wright
---
please visit www.serpia.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: StringIO MySQL data blob Image problem

2007-09-05 Thread Tim Golden

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"[EMAIL 
PROTECTED]'Í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
-- 
http://mail.python.org/mailman/listinfo/python-list