Imagenes en python 2.5

Amm-Python python en ammsoft.com
Mar Ene 22 17:49:26 CET 2008


Así es como lo hacemos nosotros.
No se si te servirá, utilizamos Python 2.4, wxpython 2.6.2, Oracle y la
imagen está en un campo tipo BLOB.

import wx
import cStringIO, Image

def PILToWX(image):
    """convert a PIL image to a wxImage
    """
    
    if (image.mode != 'RGB'):
        image = image.convert('RGB')
    imageData = image.tostring('raw', 'RGB')
    imageWx = wx.EmptyImage(image.size[0], image.size[1])
    imageWx.SetData(imageData)
    return imageWx
    
def WXToPIL(image, mode = 'RGBA'):
    """convert a wxImage to a PIL RGBA image
    """
    
    imageData = image.GetData()
    size = (image.GetWidth(), image.GetHeight())
    imagePIL = Image.fromstring('RGB', size, imageData)
    if mode != 'RGB':
        imagePIL = imagePIL.convert(mode)
    return imagePIL

def resizeTo(image, width=None, height=None, retainAspect=True):
    """Resize to a given size. Omitting either height or width
        will cause it to be sized along with the other dimension to
        keep the aspect ration. If retainAspect is set to False
        the size is left alone
    """
    if width and not height and isinstance(width, tuple):
        width, height =  width

    # If both are passed in then we know the size
    if width and height: retainAspect = False

    if width and retainAspect:
        ratio = float(width)/float(image.size[0])
        height = round(ratio * image.size[1])
    elif height and retainAspect:
        ratio = float(height)/float(image.size[1])
        width = round(ratio * image.size[0])

    if not width: width = image.size[0]
    if not height: height = image.size[1]
    return image.resize((int(width), int(height)),
                                    Image.ANTIALIAS)

def MostrarFotografia(self, buff, foto, ancho=200, largo=250):
    """Muestra la imagen contenida en la variable buff 
    en el StaticBitmap foto
    """
    
    if buff != None:
        try:
            im = buff
            try:
                bmr = cStringIO.StringIO(im.read())
            except:
                bmr = cStringIO.StringIO(im)
            
            img = wx.ImageFromStream(bmr)
            image = WXToPIL(img)
            image = resizeTo(image,ancho,largo)
            bmp = PILToWX(image).ConvertToBitmap()
        except (Exception),e:
            bmp = wx.Image('temas/foto.png') #imagen vacía
            image = WXToPIL(bmp)
            image = resizeTo(image,ancho,largo)
            bmp = PILToWX(image).ConvertToBitmap()
    else:
        bmp = wx.Image('temas/foto.png')
        image = WXToPIL(bmp)
        image = resizeTo(image,ancho,largo)
        bmp = PILToWX(image).ConvertToBitmap()
        
    foto.SetBitmap(bmp)
    foto.Refresh()
    foto.Show(True)

> -----Mensaje original-----
> De: python-es-bounces en aditel.org
> [mailto:python-es-bounces en aditel.org] En nombre de cesar diaz
> Enviado el: dimarts, 22 / gener / 2008 16:30
> Para: python-es en aditel.org
> Asunto: [Python-es] Imagenes en python 2.5
> 
> 
> Hola. Esta consulta la he enviado varias veces pero al
> parecer nadie me entiende. Trataré de exlicar un poco lo que 
> quiero. Estoy haciendo una aplicacion de registro academico, 
> y en este momento la tengo parada por un pequeño detalle. No 
> he podido saber como hago para mostrar la foto de un 
> estudiante, la cual se encuentra guardada en una base de 
> datos,  dentro de un wxStaticBitMap.
> 
> Si alguien me entiende y me puede colaborar con esta duda se
> lo agradeceria mucho. GRacias por su comprension. Apenas 
> estoy empezando en este cuento de la programacion, disculpen 
> mi ignorancia.
> 
> 
> 
> 
>        
> ______________________________________________
> Web Revelación Yahoo! 2007:
> Premio Favorita del Público. 
> http://es.promotions.yahoo.com/revelacion2007/favoritos/
> 

_______________________________________________
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