def on_button_clicked(self, widget, operation): if operation == 'load-new-image': dialog = Gtk.FileChooserDialog( _('Select one images to upload to Picasa Web'), self, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) dialog.set_default_response(Gtk.ResponseType.OK) filter = Gtk.FileFilter() filter.set_name(_('Imagenes')) filter.add_mime_type('image/png') filter.add_mime_type('image/jpeg') filter.add_mime_type('image/gif') filter.add_mime_type('image/x-ms-bmp') filter.add_mime_type('image/x-icon') filter.add_mime_type('image/tiff') filter.add_mime_type('image/x-photoshop') filter.add_mime_type('x-portable-pixmap') filter.add_pattern('*.png') filter.add_pattern('*.jpg') filter.add_pattern('*.gif') filter.add_pattern('*.bmp') filter.add_pattern('*.ico') filter.add_pattern('*.tiff') filter.add_pattern('*.psd') filter.add_pattern('*.ppm') dialog.add_filter(filter) preview = Gtk.Image() dialog.set_preview_widget(preview) dialog.connect('update-preview', self.update_preview_cb, preview) response = dialog.run() if response == Gtk.ResponseType.OK: self.filename = dialog.get_filename() f = open(self.filename, 'rb') data = f.read() f.close() pbl = GdkPixbuf.PixbufLoader() pbl.write(data) pbuf = pbl.get_pixbuf() pbl.close() if pbuf is not None: self.aimage2.set_from_pixbuf(pbuf) dialog.destroy() elif operation == 'rotate-left': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.rotate_pixbuf(pixbuf, 90) self.aimage2.set_from_pixbuf(pixbuf) elif operation == 'rotate-right': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.rotate_pixbuf(pixbuf, 270) self.aimage2.set_from_pixbuf(pixbuf) elif operation == 'flip-horizontal': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.flip_pixbuf(pixbuf, True) self.aimage2.set_from_pixbuf(pixbuf) elif operation == 'flip-vertical': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.flip_pixbuf(pixbuf, False) self.aimage2.set_from_pixbuf(pixbuf) elif operation == 'grayscale': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.grayscale_pixbuf(pixbuf) self.aimage2.set_from_pixbuf(pixbuf) elif operation == 'instagram-sunset': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: image = utils.pixbuf2image(pixbuf) filter = filters.CFilterSunset() new_image = filter.applyFilter(image) pixbuf = utils.image2pixbuf(new_image) self.aimage2.set_from_pixbuf(pixbuf) elif operation == 'instagram-colorful': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: image = utils.pixbuf2image(pixbuf) filter = filters.CFilterColorful() new_image = filter.applyFilter(image) pixbuf = utils.image2pixbuf(new_image) self.aimage2.set_from_pixbuf(pixbuf) elif operation == 'instagram-groovy': pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: image = utils.pixbuf2image(pixbuf) filter = filters.CFilterGroovy() new_image = filter.applyFilter(image) pixbuf = utils.image2pixbuf(new_image) self.aimage2.set_from_pixbuf(pixbuf) else: pass
def on_button_clicked(self, widget, operation): if operation == "load-new-image": dialog = Gtk.FileChooserDialog( _("Select one images to upload to Picasa Web"), self, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK), ) dialog.set_default_response(Gtk.ResponseType.OK) filter = Gtk.FileFilter() filter.set_name(_("Imagenes")) filter.add_mime_type("image/png") filter.add_mime_type("image/jpeg") filter.add_mime_type("image/gif") filter.add_mime_type("image/x-ms-bmp") filter.add_mime_type("image/x-icon") filter.add_mime_type("image/tiff") filter.add_mime_type("image/x-photoshop") filter.add_mime_type("x-portable-pixmap") filter.add_pattern("*.png") filter.add_pattern("*.jpg") filter.add_pattern("*.gif") filter.add_pattern("*.bmp") filter.add_pattern("*.ico") filter.add_pattern("*.tiff") filter.add_pattern("*.psd") filter.add_pattern("*.ppm") dialog.add_filter(filter) preview = Gtk.Image() dialog.set_preview_widget(preview) dialog.connect("update-preview", self.update_preview_cb, preview) response = dialog.run() if response == Gtk.ResponseType.OK: self.filename = dialog.get_filename() f = open(self.filename, "rb") data = f.read() f.close() pbl = GdkPixbuf.PixbufLoader() pbl.write(data) pbuf = pbl.get_pixbuf() pbl.close() if pbuf is not None: self.aimage2.set_from_pixbuf(pbuf) dialog.destroy() elif operation == "rotate-left": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.rotate_pixbuf(pixbuf, 90) self.aimage2.set_from_pixbuf(pixbuf) elif operation == "rotate-right": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.rotate_pixbuf(pixbuf, 270) self.aimage2.set_from_pixbuf(pixbuf) elif operation == "flip-horizontal": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.flip_pixbuf(pixbuf, True) self.aimage2.set_from_pixbuf(pixbuf) elif operation == "flip-vertical": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.flip_pixbuf(pixbuf, False) self.aimage2.set_from_pixbuf(pixbuf) elif operation == "grayscale": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: pixbuf = picasamod.grayscale_pixbuf(pixbuf) self.aimage2.set_from_pixbuf(pixbuf) elif operation == "instagram-sunset": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: image = utils.pixbuf2image(pixbuf) filter = filters.CFilterSunset() new_image = filter.applyFilter(image) pixbuf = utils.image2pixbuf(new_image) self.aimage2.set_from_pixbuf(pixbuf) elif operation == "instagram-colorful": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: image = utils.pixbuf2image(pixbuf) filter = filters.CFilterColorful() new_image = filter.applyFilter(image) pixbuf = utils.image2pixbuf(new_image) self.aimage2.set_from_pixbuf(pixbuf) elif operation == "instagram-groovy": pixbuf = self.aimage2.get_pixbuf() if pixbuf is not None: image = utils.pixbuf2image(pixbuf) filter = filters.CFilterGroovy() new_image = filter.applyFilter(image) pixbuf = utils.image2pixbuf(new_image) self.aimage2.set_from_pixbuf(pixbuf) else: pass
def get_pixbuf(self, maximo): """ Devuelve una GtkImage de la foto del empleado o de la foto por defecto si no tiene. Si maximo != None reescala la imagen para que ninguna de sus dos dimensiones supere esa cantidad """ if self.data: impil = foto.to_pil() else: from PIL import Image impil = Image.open(os.path.join("..", "imagenes", "users.png")) ancho, alto = impil.size escala = (float(maximo) / max(ancho, alto)) impil = impil.resize((int(ancho * escala), int(alto * escala)), resample = 1) pixbuf = utils.image2pixbuf(impil) return pixbuf def _set_data(self, value): self._SO_set_data(value.encode('base64')) def _get_data(self): return self._SO_get_data().decode('base64') def save_to_temp(self): """ Guarda la imagen en un temporal y devuelve la ruta a la misma. """ # OJO: 1.- No chequea que no haya imagen almacenada. # 2.- No chequea que no exista una imagen con el mismo # nombre o que no pueda escribir a disco por falta de # espacio, etc. from tempfile import gettempdir from random import randint dir = gettempdir() nombre = "%d" % randint(10**9, 10**10 - 1) ruta = os.path.join(dir, nombre) f = open(ruta, "wb") f.write(self.data) f.close() return ruta def to_pil(self): """ Devuelve una imagen PIL a partir del BLOB guardado. """ from PIL import Image path_tmp_im = self.save_to_temp() im = Image.open(path_tmp_im) return im def store_from_file(self, ruta): """ Guarda la imagen especificada por la ruta en la base de datos. """ self.data = "" f = open(ruta, "rb") binchunk = f.read() while binchunk: self.data += binchunk binchunk = f.read() f.close() def store_from_pil(self, imagen): """ Guarda la imagen del objeto PIL en la base de datos. """ import Image # PLAN raise NotImplementedError