Manejo de imágenes en Python

Aquí una implementación del modulo Python "Image" de la librería "PIL", para el manejo de imágenes.





  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-


  3. import Image


  4. class Imagen(object):

  5.     def __init__(self):
  6.         return
  7.         
  8.     def proporcion(sizeancho=0alto=0):
  9.         hsize[0], size[1]
  10.         if ancho:
  11.             x100to = (100.0 h) * ancho
  12.         if alto:
  13.             x100to = (100.0 w) * alto
  14.         h1 = (100.0) * x100to
  15.         w1 = (100.0) * x100to
  16.         return (int(h1), int(w1))
  17.   

  18.     def ImgResize(selfimganchoalto=None):
  19.         """img es una instancia del modulo Image. Si no se especifica un
  20.         alto, se cambia el tamano proporcionalmente al ancho especificado.
  21.         imageobj -> imageobje
  22.         """
  23.         size = (anchoalto)
  24.         if not alto:
  25.             size img.size
  26.             prop float(size[0]) / ancho
  27.             alto float(size[1]) /  prop
  28.             size = (int(ancho), int(alto))
  29.         return img.resize(sizeImage.ANTIALIAS)

  30.     
  31.     def ImgGetThumbnail(imgsizeresample=0):
  32.         """Crea un thumbnail de la imagen indicada.
  33.         """
  34.         return img.thumbnail(sizeresample)

  35.     
  36.     def ImgResizeProportion(selfimgintproportion):
  37.         """resize el imagen objeto de acuerdo a la proporcion indicada
  38.         Imageobj -> Imageobje
  39.         """
  40.         anchoalto img.size
  41.         ancho int((ancho 100) * intproportion)
  42.         alto int((alto 100) * intproportion)
  43.         return self.ImgResize(imganchoalto)
  44.     

  45.     def ImgResizeProportionFromPath(selfpathintproportion):
  46.         """Ver ImgResizeProportion.
  47.         """
  48.         img self.ImgOpen(path)
  49.         return self.ImgResizeProportion(imgintproportion)

  50.     
  51.     def ImgResizeFromPath(selfpathanchoalto=None):
  52.         """Imageobj -> Imageobj, ver ImgResize
  53.         """
  54.         img self.ImgOpen(path)
  55.         return self.ImgResize(imganchoalto)

  56.     
  57.     def ImgOpen(selfpath):
  58.         """strpath -> Imageobj.
  59.         """
  60.         return Image.open(path)

  61.     
  62.     def ImgSave(selfimgoutpath):
  63.         """Crea una imagen fisica en el outpath, a partir del Image obj.
  64.         """
  65.         return img.save(outpath)

  66.     
  67.     def ImgNew(selfsizecolor=0):
  68.         """Crea un nuevo objeto Image.
  69.         """
  70.         return Image.new(sizecolor)