Skip to content

Commit

Permalink
Merge branch 'python3' of https://github.com/reubot/cropgui
Browse files Browse the repository at this point in the history
  • Loading branch information
jepler committed Sep 5, 2019
2 parents 552ae7b + 954d859 commit 78db49a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 50 deletions.
19 changes: 10 additions & 9 deletions cropgtk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2
#!/usr/bin/python3
# cropgui, a graphical front-end for lossless jpeg cropping
# Copyright (C) 2009 Jeff Epler <[email protected]>
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,9 +19,10 @@
from cropgui_common import _

import gi
from gi.repository import GObject as gobject
#from gi.repository import GObject as gobject
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
from gi.repository import GLib
#import gtk.glade
from gi.repository import Gdk as gdk
import filechooser
Expand All @@ -40,7 +41,7 @@ def excepthook(exc_type, exc_obj, exc_tb):
except NameError:
w = None
lines = traceback.format_exception(exc_type, exc_obj, exc_tb)
print "".join(lines)
print("".join(lines))
m = gtk.MessageDialog(w,
gtk.DialogFlags.MODAL | gtk.DialogFlags.DESTROY_WITH_PARENT,
gtk.MessageType.ERROR, gtk.ButtonsType.OK,
Expand Down Expand Up @@ -146,7 +147,7 @@ def image_set(self):

def render(self):
if self.idle is None:
self.idle = gobject.idle_add(self.do_render)
self.idle = GLib.idle_add(self.do_render)

def do_render(self):
if not self.idle:
Expand Down Expand Up @@ -199,7 +200,7 @@ def do_render(self):


def wait(self):
self.loop = gobject.MainLoop()
self.loop = GLib.MainLoop()
self.result = -1
self.loop.run()
return self.result
Expand All @@ -209,8 +210,8 @@ def wait(self):

class App:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file(gladefile)
self.builder = gtk.Builder()
self.builder.add_from_file(gladefile)
#self.glade = gtk.glade.XML(gladefile)
self.drag = DragManager(self)
self.task = CropTask(self)
Expand Down Expand Up @@ -248,7 +249,7 @@ def run(self):
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:
except (IOError,) as detail:
m = gtk.MessageDialog(self['window1'],
gtk.DialogFlags.MODAL | gtk.DialogFlags.DESTROY_WITH_PARENT,
gtk.MessageType.ERROR, gtk.ButtonsType.OK,
Expand Down Expand Up @@ -299,7 +300,7 @@ def run(self):
command = ['nice', 'convert']
if not rotation == "none": command.extend(['-rotate', rotation])
command.extend([image_name, '-crop', cropspec, target])
print " ".join(command)
print(" ".join(command))
task.add(command, target)

def image_names(self):
Expand Down
52 changes: 26 additions & 26 deletions cropgui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2
#!/usr/bin/python3
# cropgui, a graphical front-end for lossless jpeg cropping
# Copyright (C) 2009 Jeff Epler <[email protected]>
# This program is free software; you can redistribute it and/or modify
Expand All @@ -18,60 +18,60 @@
from cropgui_common import *
from cropgui_common import _

import Tkinter
import ImageTk
import tkFileDialog
import tkinter
from PIL import ImageTk
import tkinter.filedialog
import sys
import os
import signal
import log

app = Tkinter.Tk()
app = tkinter.Tk()
app.wm_title(_("CropGUI -- lossless cropping and rotation of jpeg files"))
app.wm_iconname(_("CropGUI"))

preview = Tkinter.Label(app)
preview = tkinter.Label(app)
preview.pack(side="bottom")

do_crop = Tkinter.Button(app, text="Crop")
do_crop = tkinter.Button(app, text="Crop")
do_crop.pack(side="left")

crop169_button = Tkinter.Menubutton(app, text="16:9")
crop169_button = tkinter.Menubutton(app, text="16:9")
crop169_button.pack(side="left")
crop169 = Tkinter.Menu(crop169_button)
crop169 = tkinter.Menu(crop169_button)
crop169_button.config(menu=crop169)

crop85_button = Tkinter.Menubutton(app, text="8:5")
crop85_button = tkinter.Menubutton(app, text="8:5")
crop85_button.pack(side="left")
crop85 = Tkinter.Menu(crop85_button)
crop85 = tkinter.Menu(crop85_button)
crop85_button.config(menu=crop85)

crop32_button = Tkinter.Menubutton(app, text="3:2")
crop32_button = tkinter.Menubutton(app, text="3:2")
crop32_button.pack(side="left")
crop32 = Tkinter.Menu(crop32_button)
crop32 = tkinter.Menu(crop32_button)
crop32_button.config(menu=crop32)

crop43_button = Tkinter.Menubutton(app, text="4:3")
crop43_button = tkinter.Menubutton(app, text="4:3")
crop43_button.pack(side="left")
crop43 = Tkinter.Menu(crop43_button)
crop43 = tkinter.Menu(crop43_button)
crop43_button.config(menu=crop43)

crop11_button = Tkinter.Menubutton(app, text="1:1")
crop11_button = tkinter.Menubutton(app, text="1:1")
crop11_button.pack(side="left")
crop11 = Tkinter.Menu(crop11_button)
crop11 = tkinter.Menu(crop11_button)
crop11_button.config(menu=crop11)

crop34_button = Tkinter.Menubutton(app, text="3:4")
crop34_button = tkinter.Menubutton(app, text="3:4")
crop34_button.pack(side="left")
crop34 = Tkinter.Menu(crop34_button)
crop34 = tkinter.Menu(crop34_button)
crop34_button.config(menu=crop34)

crop23_button = Tkinter.Menubutton(app, text="2:3")
crop23_button = tkinter.Menubutton(app, text="2:3")
crop23_button.pack(side="left")
crop23 = Tkinter.Menu(crop23_button)
crop23 = tkinter.Menu(crop23_button)
crop23_button.config(menu=crop23)

info = Tkinter.Label(app)
info = tkinter.Label(app)
info.pack(side="left")

task = CropTask(log)
Expand All @@ -93,9 +93,9 @@ def __init__(self, w, b, inf):
app.bind("<Escape>", self.escape)
w.bind("<Button1-Enter>", "#nothing")
w.bind("<Button1-Leave>", "#nothing")
dummy_image = Image.new('L', (max_w/2,max_h/2), 0xff)
dummy_image = Image.new('L', (int(max_w/2),int(max_h/2)), 0xff)
self.dummy_tkimage = ImageTk.PhotoImage(dummy_image)
self.v = Tkinter.IntVar(app)
self.v = tkinter.IntVar(app)
DragManagerBase.__init__(self)

def image_set(self):
Expand Down Expand Up @@ -236,7 +236,7 @@ def image_names():
for i in sys.argv[1:]: yield i
else:
while 1:
names = tkFileDialog.askopenfilenames(master=app,
names = tkinter.filedialog.askopenfilenames(master=app,
defaultextension=".jpg", multiple=1, parent=app,
filetypes=(
(_("JPEG Image Files"), ".jpg .JPG .jpeg .JPEG"),
Expand Down Expand Up @@ -297,7 +297,7 @@ def set_busy(new_busy=True):
cropspec = "%dx%d+%d+%d" % (r-l, b-t, l, t)
target = base + "-crop" + ext
command=['nice', 'jpegtran', '-copy', 'all', '-crop', cropspec, '-outfile', target, image_name]
print " ".join(command)
print(" ".join(command))
task.add(command, target)
finally:
task.done()
Expand Down
23 changes: 12 additions & 11 deletions cropgui_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from PIL import ImageDraw
import subprocess
import threading
import Queue
import queue
import os
import math

Expand All @@ -35,7 +35,7 @@ def _(s): return s # TODO: i18n
DRAG_TL, DRAG_T, DRAG_TR,
DRAG_L, DRAG_C, DRAG_R,
DRAG_BL, DRAG_B, DRAG_BR
) = range(10)
) = list(range(10))

def describe_ratio(a, b):
if a == 0 or b == 0: return "degenerate"
Expand All @@ -56,7 +56,7 @@ def ncpus():
class CropTask(object):
def __init__(self, log):
self.log = log
self.tasks = Queue.Queue()
self.tasks = queue.Queue()
self.threads = set(self.create_task() for i in range(ncpus))
for t in self.threads: t.start()

Expand Down Expand Up @@ -144,7 +144,8 @@ def get_corners(self):

def get_screencorners(self):
t, l, r, b = self.get_corners()
return t/self.scale, l/self.scale, r/self.scale, b/self.scale
return(int(t/int(self.scale)), int(l/int(self.scale)),
int(r/int(self.scale)), int(b/int(self.scale)))

def describe_ratio(self):
w = self.right - self.left
Expand Down Expand Up @@ -276,8 +277,8 @@ def drag_start(self, x, y, fixed=False):
self.fixed_ratio = False

def drag_continue(self, x, y):
dx = (x - self.x0) * self.scale
dy = (y - self.y0) * self.scale
dx = (x - self.x0) * int(self.scale)
dy = (y - self.y0) * int(self.scale)
if self.fixed_ratio:
ratio = (self.r0-self.l0) * 1. / (self.b0 - self.t0)
if self.state in (DRAG_TR, DRAG_BL): ratio = -ratio
Expand Down Expand Up @@ -339,9 +340,9 @@ def rotate_cw(self):

def set_rotation(self, rotation):
if rotation not in (1, 3, 6, 8):
raise ValueError, 'Unsupported rotation %r' % rotation
raise ValueError('Unsupported rotation %r' % rotation)

print "rotation", self.rotation, "->", rotation
print("rotation", self.rotation, "->", rotation)
self._rotation = rotation
self.image_or_rotation_changed()

Expand All @@ -352,14 +353,14 @@ def get_rotation(self):

def image_rotation(i):
if not hasattr(i, '_getexif'):
print "no getexif?", type(i), getattr(i, '_getexif', None)
print("no getexif?", type(i), getattr(i, '_getexif', None))
return 1
exif = i._getexif()
if not isinstance(exif, dict):
print "not dict?", repr(exif)
print("not dict?", repr(exif))
return 1
result = exif.get(0x112, None)
print "image_rotation", result
print("image_rotation", result)
return result or 1

_desktop_name = None
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ XS-Python-Version: current
Standards-Version: 2.0.0-rc

Package: cropgui
Depends: ${python:Depends}, ${misc:Depends}, python-gobject, python-gi, libgtk-3-0, libjpeg-turbo-progs | libjpeg-progs, libimage-exiftool-perl, imagemagick
Depends: python3-pil, python3-gi, libjpeg-turbo-progs | libjpeg-progs, libimage-exiftool-perl, imagemagick
Section: graphics
Architecture: all
Description: interactive tool to losslessly resize jpeg images
6 changes: 3 additions & 3 deletions filechooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import cropgui_common

def apply_rotation(rotation, image):
print "apply_rotation", rotation
print("apply_rotation", rotation)
if rotation == 3: return image.transpose(Image.ROTATE_180)
if rotation == 6: return image.transpose(Image.ROTATE_270)
if rotation == 8: return image.transpose(Image.ROTATE_90)
Expand Down Expand Up @@ -62,8 +62,8 @@ def update_preview_cb(file_chooser, preview):
while len(image_cache) > LOW_WATER:
image_cache.popitem()
image_cache[filename] = pixbuf
except IOError, detail:
print detail
except IOError as detail:
print(detail)
preview.set_from_stock(gtk.STOCK_MISSING_IMAGE,
gtk.IconSize.LARGE_TOOLBAR)
except:
Expand Down

0 comments on commit 78db49a

Please sign in to comment.