def image_names(): if len(sys.argv) > 1: for i in sys.argv[1:]: yield i else: while 1: names = tkFileDialog.askopenfilenames(master=app, defaultextension=".jpg", multiple=1, parent=app, filetypes=( (_("JPEG Image Files"), ".jpg .JPG .jpeg .JPEG"), (_("All files"), "*"), ), title=_("Select images to crop")) if not names: break for name in names: yield name
def output_name(self, image_name, image_type): image_name = os.path.abspath(image_name) d = os.path.dirname(image_name) i = os.path.basename(image_name) j = os.path.splitext(i)[0] if j.endswith('-crop'): j += os.path.splitext(i)[1] else: j += "-crop" + os.path.splitext(i)[1] if os.access(d, os.W_OK): return os.path.join(d, j) title = _('Save cropped version of %s') % i if self.dirchooser is None: self.dirchooser = filechooser.DirChooser(self['window1'], title) self.dirchooser.set_current_folder(desktop_name()) else: self.dirchooser.set_title(title) self.dirchooser.set_current_name(j) r = self.dirchooser.run() if not r: return '' r = r[0] e = os.path.splitext(r)[1] if image_type == "jpeg": if e.lower() in ['.jpg', '.jpeg']: return r return e + ".jpg" elif e.lower() == image_type: return r else: return e + "." + image_type
def __init__(self): self.builder = gtk.Builder() self.builder.add_from_file(gladefile) #self.glade = gtk.glade.XML(gladefile) self.drag = DragManager(self) self.task = CropTask(self) self.dirchooser = None self['window1'].set_title(_("CropGTK"))
def run(self): drag = self.drag task = self.task for image_name in self.image_names(): self["window1"].set_title(_("%s - CropGTK") % os.path.basename(image_name)) self.set_busy() try: i = Image.open(image_name) drag.w, drag.h = i.size scale = 1 scale = max(scale, (drag.w - 1) / max_w + 1) scale = max(scale, (drag.h - 1) / max_h + 1) i.thumbnail((drag.w / scale, drag.h / scale)) except (IOError,), detail: m = gtk.MessageDialog( self["window1"], gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Could not open %s: %s" % (image_name, detail), ) m.show() m.run() m.destroy() continue drag.image = i drag.rotation = image_rotation(i) drag.scale = scale self.set_busy(0) v = self.drag.wait() self.set_busy() if v == -1: break # user closed app if v == 0: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "next" / escape t, l, r, b = drag.top, drag.left, drag.right, drag.bottom # t *= scale # l *= scale # r *= scale # b *= scale cropspec = "%dx%d+%d+%d" % (r - l, b - t, l, t) command = ["nice", "jpegtran"] if drag.rotation == 3: command.extend(["-rotate", "180"]) elif drag.rotation == 6: command.extend(["-rotate", "90"]) elif drag.rotation == 8: command.extend(["-rotate", "270"]) command.extend(["-copy", "all", "-crop", cropspec, image_name]) target = self.output_name(image_name) if not target: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "cancel" on save dialog print " ".join(command), ">", target task.add(command, target)
def image_names(self): if len(sys.argv) > 1: for i in sys.argv[1:]: yield i else: c = filechooser.Chooser(self['window1'], _("Select images to crop")) while 1: files = c.run() if not files: break for i in files: yield i
def run(self): drag = self.drag task = self.task for image_name in self.image_names(): self['window1'].set_title( _("%s - CropGTK") % os.path.basename(image_name)) self.set_busy() try: image = Image.open(image_name) drag.round_x, drag.round_y = image_round(image) drag.w, drag.h = image.size scale = 1 scale = max(scale, nextPowerOf2((drag.w - 1) / (max_w + 1))) scale = max(scale, nextPowerOf2((drag.h - 1) / (max_h + 1))) thumbnail = image.copy() thumbnail.thumbnail((drag.w / scale, drag.h / scale)) except (IOError, ) as detail: m = gtk.MessageDialog( self['window1'], gtk.DialogFlags.MODAL | gtk.DialogFlags.DESTROY_WITH_PARENT, gtk.MessageType.ERROR, gtk.ButtonsType.OK, "Could not open %s: %s" % (image_name, detail)) m.show() m.run() m.destroy() continue image_type = imghdr.what(image_name) drag.image = thumbnail drag.rotation = 1 rotation = image_rotation(image) if rotation in (3, 6, 8): while drag.rotation != rotation: drag.rotate_ccw() drag.scale = scale self.set_busy(0) v = self.drag.wait() self.set_busy() if v == -1: break # user closed app if v == 0: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "next" / escape target = self.output_name(image_name, image_type) if not target: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "cancel" on save dialog task.add( CropRequest( image=image, image_name=image_name, corners=drag.get_corners(), rotation=drag.rotation, target=target, ))
def image_names(): if len(sys.argv) > 1: for i in sys.argv[1:]: yield i else: while 1: names = tkinter.filedialog.askopenfilenames( master=app, defaultextension=".jpg", multiple=1, parent=app, filetypes=( (_("JPEG Image Files"), ".jpg .JPG .jpeg .JPEG"), (_("All files"), "*"), ), title=_("Select images to crop")) if not names: break for name in names: yield name
def run(self): drag = self.drag task = self.task for image_name in self.image_names(): self['window1'].set_title( _("%s - CropGTK") % os.path.basename(image_name)) self.set_busy() try: i = Image.open(image_name) iw, ih = i.size iz = max(iw, ih) scale = max(1, iz/max_sz) print iz, scale, max_sz i.thumbnail((iw/scale, ih/scale)) except (IOError,), detail: m = gtk.MessageDialog(self['window1'], gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Could not open %s: %s" % (image_name, detail)) m.show() m.run() m.destroy() continue drag.image = i drag.rotation = image_rotation(i) drag.round = max(1, 8./scale) drag.scale = scale self.set_busy(0) v = self.drag.wait() self.set_busy() if v == -1: break # user closed app if v == 0: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "next" / escape t, l, r, b = drag.top, drag.left, drag.right, drag.bottom t *= scale l *= scale r *= scale b *= scale cropspec = "%dx%d+%d+%d" % (r-l, b-t, l, t) command = ['nice', 'jpegtran'] if drag.rotation == 3: command.extend(['-rotate', '180']) elif drag.rotation == 6: command.extend(['-rotate', '90']) elif drag.rotation == 8: command.extend(['-rotate', '270']) command.extend(['-copy', 'all','-crop', cropspec, image_name]) target = self.output_name(image_name) if not target: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "cancel" on save dialog print " ".join(command), ">", target task.add(command, target)
def excepthook(exc_type, exc_obj, exc_tb): try: w = app['window1'] except NameError: w = None lines = traceback.format_exception(exc_type, exc_obj, exc_tb) print "".join(lines) m = gtk.MessageDialog(w, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, _("Stepconf encountered an error. The following " "information may be useful in troubleshooting:\n\n") + "".join(lines)) m.show() m.run() m.destroy()
def output_name(self, image_name): image_name = os.path.abspath(image_name) d = os.path.dirname(image_name) i = os.path.basename(image_name) j = os.path.splitext(i)[0].lower() + "-crop.jpg" if os.access(d, os.W_OK): return os.path.join(d, j) title = _('Save cropped version of %s') % i if self.dirchooser is None: self.dirchooser = filechooser.DirChooser(self['window1'], title) self.dirchooser.set_current_folder(desktop_name()) else: self.dirchooser.set_title(title) self.dirchooser.set_current_name(j) r = self.dirchooser.run() if not r: return '' r = r[0] e = os.path.splitext(r)[1] if e.lower() in ['.jpg', '.jpeg']: return r return e + ".jpg"
# along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from cropgui_common import * from cropgui_common import _ import tkinter from PIL import ImageTk import tkinter.filedialog import sys import os import signal import log app = tkinter.Tk() app.wm_title(_("CropGUI -- lossless cropping and rotation of jpeg files")) app.wm_iconname(_("CropGUI")) preview = tkinter.Label(app) preview.pack(side="bottom") do_crop = tkinter.Button(app, text="Crop") do_crop.pack(side="left") crop169_button = tkinter.Menubutton(app, text="16:9") crop169_button.pack(side="left") crop169 = tkinter.Menu(crop169_button) crop169_button.config(menu=crop169) crop85_button = tkinter.Menubutton(app, text="8:5") crop85_button.pack(side="left")
# along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from cropgui_common import * from cropgui_common import _ import Tkinter import ImageTk import tkFileDialog import sys import os import signal import log app = Tkinter.Tk() app.wm_title(_("CropGUI -- lossless cropping and rotation of jpeg files")) app.wm_iconname(_("CropGUI")) preview = Tkinter.Label(app) preview.pack(side="bottom") do_crop = Tkinter.Button(app, text="Crop") do_crop.pack(side="left") crop169_button = Tkinter.Menubutton(app, text="16:9") crop169_button.pack(side="left") crop169 = Tkinter.Menu(crop169_button) crop169_button.config(menu=crop169) crop85_button = Tkinter.Menubutton(app, text="8:5") crop85_button.pack(side="left")
def run(self): drag = self.drag task = self.task for image_name in self.image_names(): self['window1'].set_title( _("%s - CropGTK") % os.path.basename(image_name)) self.set_busy() try: i = Image.open(image_name) drag.w, drag.h = i.size scale = 1 scale = max (scale, (drag.w-1)/max_w+1) scale = max (scale, (drag.h-1)/max_h+1) i.thumbnail((drag.w/scale, drag.h/scale)) except (IOError,), detail: m = gtk.MessageDialog(self['window1'], gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Could not open %s: %s" % (image_name, detail)) m.show() m.run() m.destroy() continue image_type = imghdr.what(image_name) drag.image = i drag.rotation = 1 rotation = image_rotation(i) if rotation in (3,6,8): while drag.rotation != rotation: drag.rotate_ccw() drag.scale = scale self.set_busy(0) v = self.drag.wait() self.set_busy() if v == -1: break # user closed app if v == 0: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "next" / escape t, l, r, b = drag.top, drag.left, drag.right, drag.bottom cropspec = "%dx%d+%d+%d" % (r-l, b-t, l, t) if drag.rotation == 3: rotation = '180' elif drag.rotation == 6: rotation = '90' elif drag.rotation == 8: rotation = '270' else: rotation = "none" target = self.output_name(image_name,image_type) if not target: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "cancel" on save dialog # Copy file if no cropping or rotation. if (r+b-l-t) == (drag.w+drag.h) and rotation =="none": command = ['nice', 'cp' , image_name, target] # JPEG crop uses jpegtran elif image_type is "jpeg": command = ['nice', 'jpegtran'] if not rotation == "none": command.extend(['-rotate', rotation]) command.extend(['-copy', 'all', '-crop', cropspec,'-outfile', target, image_name]) # All other images use imagemagic convert. else: command = ['nice', 'convert'] if not rotation == "none": command.extend(['-rotate', rotation]) command.extend([image_name, '-crop', cropspec, target]) print " ".join(command) task.add(command, target)
def __init__(self): self.glade = gtk.glade.XML(gladefile) self.drag = DragManager(self) self.task = CropTask(self) self.dirchooser = None self["window1"].set_title(_("CropGTK"))
def run(self): drag = self.drag task = self.task for image_name in self.image_names(): self['window1'].set_title( _("%s - CropGTK") % os.path.basename(image_name)) self.set_busy() try: i = Image.open(image_name) drag.w, drag.h = i.size scale = 1 scale = max (scale, (drag.w-1)/max_w+1) scale = max (scale, (drag.h-1)/max_h+1) i.thumbnail((drag.w/scale, drag.h/scale)) except (IOError,), detail: m = gtk.MessageDialog(self['window1'], gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Could not open %s: %s" % (image_name, detail)) m.show() m.run() m.destroy() continue image_type = imghdr.what(image_name) drag.image = i drag.rotation = 1 rotation = image_rotation(i) if rotation in (3,6,8): while drag.rotation != rotation: drag.rotate_ccw() drag.scale = scale self.set_busy(0) v = self.drag.wait() self.set_busy() if v == -1: break # user closed app if v == 0: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "next" / escape t, l, r, b = drag.top, drag.left, drag.right, drag.bottom cropspec = "%dx%d+%d+%d" % (r-l, b-t, l, t) if drag.rotation == 3: rotation = '180' elif drag.rotation == 6: rotation = '90' elif drag.rotation == 8: rotation = '270' else: rotation = "none" target = self.output_name(image_name,image_type) if not target: self.log("Skipped %s" % os.path.basename(image_name)) continue # user hit "cancel" on save dialog # JPEG crop uses jpegtran if image_type is "jpeg": command = ['nice', 'jpegtran'] if not rotation == "none": command.extend(['-rotate', rotation]) command.extend(['-copy', 'all', '-crop', cropspec,'-outfile', target, image_name]) # All other images use imagemagic convert. else: command = ['nice', 'convert'] if not rotation == "none": command.extend(['-rotate', rotation]) command.extend([image_name, '-crop', cropspec, target]) print " ".join(command) task.add(command, target)