def crop_image(self, action): dialog = gtk.Dialog(_("Crop Image"), self.window, gtk.DIALOG_MODAL, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT)) dialog.add_button(_("Crop"), gtk.RESPONSE_ACCEPT) image = gtk.DrawingArea() # Create a pixbuf that fits in a window of 400x400 image_width = self.currimg_pixbuf_original.get_width() image_height = self.currimg_pixbuf_original.get_height() if image_width-400 > image_height-400: if image_width > 400: image_height = int(400/float(image_width)*image_height) image_width = 400 else: if image_height > 400: image_width = int(400/float(image_height)*image_width) image_height = 400 if self.currimg_pixbuf_original.get_has_alpha() == False: crop_pixbuf = self.currimg_pixbuf_original.scale_simple(image_width, image_height, self.zoom_quality) else: colormap = self.imageview.get_colormap() light_grey = colormap.alloc_color('#666666', True, True) dark_grey = colormap.alloc_color('#999999', True, True) crop_pixbuf = self.currimg_pixbuf_original.composite_color_simple(image_width, image_height, self.zoom_quality, 255, 8, light_grey.pixel, dark_grey.pixel) image.set_size_request(image_width, image_height) hbox = gtk.HBox() hbox.pack_start(gtk.Label(), expand=True) hbox.pack_start(image, expand=False) hbox.pack_start(gtk.Label(), expand=True) dialog.vbox.pack_start(hbox, False, False, 0) dialog.set_resizable(False) dialog.vbox.show_all() image.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.BUTTON_RELEASE_MASK) image.connect("expose-event", self.crop_image_expose_cb, crop_pixbuf, image_width, image_height) image.connect("motion_notify_event", self.crop_image_mouse_moved, image) image.connect("button_press_event", self.crop_image_button_press, image) image.connect("button_release_event", self.crop_image_button_release) image.realize() self.drawing_crop_rectangle = False self.crop_rectangle = None self.rect = None response = dialog.run() if response == gtk.RESPONSE_ACCEPT: dialog.destroy() if self.rect != None: # Convert the rectangle coordinates of the current image # to coordinates of pixbuf_original if self.rect[0] < 0: self.rect[2] = self.rect[2] + self.rect[0] self.rect[0] = 0 if self.rect[1] < 0: self.rect[3] = self.rect[3] + self.rect[1] self.rect[1] = 0 coords = [0,0,0,0] coords[0] = int(float(self.rect[0])/image_width*self.currimg_pixbuf_original.get_width()) coords[1] = int(float(self.rect[1])/image_height*self.currimg_pixbuf_original.get_height()) coords[2] = int(float(self.rect[2])/image_width*self.currimg_pixbuf_original.get_width()) coords[3] = int(float(self.rect[3])/image_height*self.currimg_pixbuf_original.get_height()) if coords[0] + coords[2] > self.currimg_pixbuf_original.get_width(): coords[2] = self.currimg_pixbuf_original.get_width() - coords[0] if coords[1] + coords[3] > self.currimg_pixbuf_original.get_height(): coords[3] = self.currimg_pixbuf_original.get_height() - coords[1] temp_pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, self.currimg_pixbuf_original.get_has_alpha(), 8, coords[2], coords[3]) self.currimg_pixbuf_original.copy_area(coords[0], coords[1], coords[2], coords[3], temp_pixbuf, 0, 0) self.currimg_pixbuf_original = temp_pixbuf del temp_pixbuf gc.collect() self.load_new_image(False, False, True, False, False) self.image_modified = True else: dialog.destroy() def crop_image_expose_cb(self, image, event, pixbuf, width, height): image.window.draw_pixbuf(None, pixbuf, 0, 0, 0, 0, width, height) def crop_image_mouse_moved(self, widget, event, image): x, y, state = event.window.get_pointer() if self.drawing_crop_rectangle == True: if self.crop_rectangle != None: gc = image.window.new_gc(function=gtk.gdk.INVERT) if self.rect != None: # Get rid of the previous drawn rectangle: image.window.draw_rectangle(gc, False, self.rect[0], self.rect[1], self.rect[2], self.rect[3]) self.rect = [0, 0, 0, 0] if self.crop_rectangle[0] > x: self.rect[0] = x self.rect[2] = self.crop_rectangle[0]-x else: self.rect[0] = self.crop_rectangle[0] self.rect[2] = x-self.crop_rectangle[0] if self.crop_rectangle[1] > y: self.rect[1] = y self.rect[3] = self.crop_rectangle[1]-y else: self.rect[1] = self.crop_rectangle[1] self.rect[3] = y-self.crop_rectangle[1] image.window.draw_rectangle(gc, False, self.rect[0], self.rect[1], self.rect[2], self.rect[3]) def crop_image_button_press(self, widget, event, image): x, y, state = event.window.get_pointer() if (state & gtk.gdk.BUTTON1_MASK): self.drawing_crop_rectangle = True self.crop_rectangle = [x, y] gc = image.window.new_gc(function=gtk.gdk.INVERT) if self.rect != None: # Get rid of the previous drawn rectangle: image.window.draw_rectangle(gc, False, self.rect[0], self.rect[1], self.rect[2], self.rect[3]) self.rect = None def crop_image_button_release(self, widget, event): x, y, state = event.window.get_pointer() if not (state & gtk.gdk.BUTTON1_MASK): self.drawing_crop_rectangle = False