Guardar y recuperar imagenes en BBDD
Guimi
listas en guimi.net
Mie Dic 26 13:47:13 CET 2007
Hola,
la pista de Arnau ha sido la definitiva, gracias a todos.
Para el archivo dejo como ha quedado todo mi código perfectamente funcional:
----------------------------------------------------
# Guardar imagenes en un campo BLOB de MySQL
imagen = Image.open('ejemplo.bmp')
imagenStr = imagen.tostring()
imagenMode = imagen.mode
imagenSize = imagen.size
miCursor.execute("insert into prueba (bin, mode, width, height) values (
%s, \""+imagenMode+"\", "+str(imagenSize[0])+", "+str(imagenSize[1])+" )
", imagenStr)
(...)
# Recuperar imagenes de un campo BLOB de MySQL
#+ (Suponemos que tenemos toda la fila de datos en "datos"
imagenStr = datos[0]
imagenMode = datos[1]
imagenWidth = datos[2]
imagenHeight = datos[3]
imagenSize = (imagenWidth, imagenHeight)
imagen = Image.fromstring(imagenMode, imagenSize, imagenStr)
# Usamos una funcion para convertir Image a Pixbuf
imagenPB = self.Image_to_GdkPixbuf(imagen)
imagenGTK = gtk.Image()
imagenGTK.set_from_pixbuf(imagenPB)
cajaScroll.add(imagenGTK)
(...)
# La funcion que hace la "magia" es original de Sebastian Wilhelmi
#+ (seppi at seppi.de)
#+ Tomada de http://www.daa.com.au/pipermail/pygtk/2003-June/005268.html
def Image_to_GdkPixbuf(self, image):
#import pygtk
#pygtk.require("2.0")
#import gtk
#import StringIO
#import Image
file = StringIO.StringIO ()
image.save (file, 'ppm')
contents = file.getvalue()
file.close ()
loader = gtk.gdk.PixbufLoader ('pnm')
loader.write (contents, len (contents))
pixbuf = loader.get_pixbuf ()
loader.close ()
return pixbuf
----------------------------------------------------
Saludos
Güimi
http://guimi.net
------------ próxima parte ------------
_______________________________________________
Lista de correo Python-es
http://listas.aditel.org/listinfo/python-es
FAQ: http://listas.aditel.org/faqpyes
Más información sobre la lista de distribución Python-es