Fixed scripts import of tkinter/Tkinter

This commit is contained in:
Andrew Murray 2017-03-24 19:42:58 +11:00
parent f5a74e1862
commit 51f5a0123c
5 changed files with 60 additions and 53 deletions

View File

@ -7,21 +7,22 @@
# drag the slider to modify the image. # drag the slider to modify the image.
# #
try: import sys
from tkinter import Tk, Toplevel, Frame, Label, Scale, HORIZONTAL
except ImportError: if sys.version_info[0] > 2:
from Tkinter import Tk, Toplevel, Frame, Label, Scale, HORIZONTAL import tkinter
else:
import Tkinter as tkinter
from PIL import Image, ImageTk, ImageEnhance from PIL import Image, ImageTk, ImageEnhance
import sys
# #
# enhancer widget # enhancer widget
class Enhance(Frame): class Enhance(tkinter.Frame):
def __init__(self, master, image, name, enhancer, lo, hi): def __init__(self, master, image, name, enhancer, lo, hi):
Frame.__init__(self, master) tkinter.Frame.__init__(self, master)
# set up the image # set up the image
self.tkim = ImageTk.PhotoImage(image.mode, image.size) self.tkim = ImageTk.PhotoImage(image.mode, image.size)
@ -29,10 +30,10 @@ class Enhance(Frame):
self.update("1.0") # normalize self.update("1.0") # normalize
# image window # image window
Label(self, image=self.tkim).pack() tkinter.Label(self, image=self.tkim).pack()
# scale # scale
s = Scale(self, label=name, orient=HORIZONTAL, s = tkinter.Scale(self, label=name, orient=tkinter.HORIZONTAL,
from_=lo, to=hi, resolution=0.01, from_=lo, to=hi, resolution=0.01,
command=self.update) command=self.update)
s.set(self.value) s.set(self.value)
@ -49,15 +50,15 @@ if len(sys.argv) != 2:
print("Usage: enhancer file") print("Usage: enhancer file")
sys.exit(1) sys.exit(1)
root = Tk() root = tkinter.Tk()
im = Image.open(sys.argv[1]) im = Image.open(sys.argv[1])
im.thumbnail((200, 200)) im.thumbnail((200, 200))
Enhance(root, im, "Color", ImageEnhance.Color, 0.0, 4.0).pack() Enhance(root, im, "Color", ImageEnhance.Color, 0.0, 4.0).pack()
Enhance(Toplevel(), im, "Sharpness", ImageEnhance.Sharpness, -2.0, 2.0).pack() Enhance(tkinter.Toplevel(), im, "Sharpness", ImageEnhance.Sharpness, -2.0, 2.0).pack()
Enhance(Toplevel(), im, "Brightness", ImageEnhance.Brightness, -1.0, 3.0).pack() Enhance(tkinter.Toplevel(), im, "Brightness", ImageEnhance.Brightness, -1.0, 3.0).pack()
Enhance(Toplevel(), im, "Contrast", ImageEnhance.Contrast, -1.0, 3.0).pack() Enhance(tkinter.Toplevel(), im, "Contrast", ImageEnhance.Contrast, -1.0, 3.0).pack()
root.mainloop() root.mainloop()

View File

@ -9,21 +9,23 @@
# the image into a set of tiles. # the image into a set of tiles.
# #
try: import sys
from tkinter import Tk, Canvas, NW
except ImportError: if sys.version_info[0] > 2:
from Tkinter import Tk, Canvas, NW import tkinter
else:
import Tkinter as tkinter
from PIL import Image, ImageTk from PIL import Image, ImageTk
import sys
# #
# painter widget # painter widget
class PaintCanvas(Canvas): class PaintCanvas(tkinter.Canvas):
def __init__(self, master, image): def __init__(self, master, image):
Canvas.__init__(self, master, width=image.size[0], height=image.size[1]) tkinter.Canvas.__init__(self, master,
width=image.size[0], height=image.size[1])
# fill the canvas # fill the canvas
self.tile = {} self.tile = {}
@ -33,7 +35,7 @@ class PaintCanvas(Canvas):
for y in range(0, ysize, tilesize): for y in range(0, ysize, tilesize):
box = x, y, min(xsize, x+tilesize), min(ysize, y+tilesize) box = x, y, min(xsize, x+tilesize), min(ysize, y+tilesize)
tile = ImageTk.PhotoImage(image.crop(box)) tile = ImageTk.PhotoImage(image.crop(box))
self.create_image(x, y, image=tile, anchor=NW) self.create_image(x, y, image=tile, anchor=tkinter.NW)
self.tile[(x, y)] = box, tile self.tile[(x, y)] = box, tile
self.image = image self.image = image
@ -70,7 +72,7 @@ if len(sys.argv) != 2:
print("Usage: painter file") print("Usage: painter file")
sys.exit(1) sys.exit(1)
root = Tk() root = tkinter.Tk()
im = Image.open(sys.argv[1]) im = Image.open(sys.argv[1])

View File

@ -6,19 +6,20 @@
from __future__ import print_function from __future__ import print_function
try: import sys
from tkinter import *
except ImportError: if sys.version_info[0] > 2:
from Tkinter import * import tkinter
else:
import Tkinter as tkinter
from PIL import Image, ImageTk from PIL import Image, ImageTk
import sys
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# an image animation player # an image animation player
class UI(Label): class UI(tkinter.Label):
def __init__(self, master, im): def __init__(self, master, im):
if isinstance(im, list): if isinstance(im, list):
@ -34,7 +35,7 @@ class UI(Label):
else: else:
self.image = ImageTk.PhotoImage(im) self.image = ImageTk.PhotoImage(im)
Label.__init__(self, master, image=self.image, bg="black", bd=0) tkinter.Label.__init__(self, master, image=self.image, bg="black", bd=0)
self.update() self.update()
@ -78,7 +79,7 @@ if __name__ == "__main__":
filename = sys.argv[1] filename = sys.argv[1]
root = Tk() root = tkinter.Tk()
root.title(filename) root.title(filename)
if len(sys.argv) > 2: if len(sys.argv) > 2:

View File

@ -7,32 +7,34 @@
# as a dynamically updated overlay # as a dynamically updated overlay
# #
try: import sys
from tkinter import *
except ImportError: if sys.version_info[0] > 2:
from Tkinter import * import tkinter
else:
import Tkinter as tkinter
from PIL import Image, ImageTk from PIL import Image, ImageTk
import sys
# #
# an image viewer # an image viewer
class UI(Frame): class UI(tkinter.Frame):
def __init__(self, master, im, value=128): def __init__(self, master, im, value=128):
Frame.__init__(self, master) tkinter.Frame.__init__(self, master)
self.image = im self.image = im
self.value = value self.value = value
self.canvas = Canvas(self, width=im.size[0], height=im.size[1]) self.canvas = tkinter.Canvas(self, width=im.size[0], height=im.size[1])
self.backdrop = ImageTk.PhotoImage(im) self.backdrop = ImageTk.PhotoImage(im)
self.canvas.create_image(0, 0, image=self.backdrop, anchor=NW) self.canvas.create_image(0, 0, image=self.backdrop, anchor=tkinter.NW)
self.canvas.pack() self.canvas.pack()
scale = Scale(self, orient=HORIZONTAL, from_=0, to=255, scale = tkinter.Scale(self, orient=tkinter.HORIZONTAL, from_=0, to=255,
resolution=1, command=self.update_scale, length=256) resolution=1, command=self.update_scale,
length=256)
scale.set(value) scale.set(value)
scale.bind("<ButtonRelease-1>", self.redraw) scale.bind("<ButtonRelease-1>", self.redraw)
scale.pack() scale.pack()
@ -54,7 +56,7 @@ class UI(Frame):
# update canvas # update canvas
self.canvas.delete("overlay") self.canvas.delete("overlay")
self.canvas.create_image(0, 0, image=self.overlay, anchor=NW, self.canvas.create_image(0, 0, image=self.overlay, anchor=tkinter.NW,
tags="overlay") tags="overlay")
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -64,7 +66,7 @@ if len(sys.argv) != 2:
print("Usage: thresholder file") print("Usage: thresholder file")
sys.exit(1) sys.exit(1)
root = Tk() root = tkinter.Tk()
im = Image.open(sys.argv[1]) im = Image.open(sys.argv[1])

View File

@ -6,10 +6,12 @@
from __future__ import print_function from __future__ import print_function
try: import sys
from tkinter import Tk, Label
except ImportError: if sys.version_info[0] > 2:
from Tkinter import Tk, Label import tkinter
else:
import Tkinter as tkinter
from PIL import Image, ImageTk from PIL import Image, ImageTk
@ -17,34 +19,33 @@ from PIL import Image, ImageTk
# an image viewer # an image viewer
class UI(Label): class UI(tkinter.Label):
def __init__(self, master, im): def __init__(self, master, im):
if im.mode == "1": if im.mode == "1":
# bitmap image # bitmap image
self.image = ImageTk.BitmapImage(im, foreground="white") self.image = ImageTk.BitmapImage(im, foreground="white")
Label.__init__(self, master, image=self.image, bg="black", bd=0) tkinter.Label.__init__(self, master, image=self.image, bd=0,
bg="black")
else: else:
# photo image # photo image
self.image = ImageTk.PhotoImage(im) self.image = ImageTk.PhotoImage(im)
Label.__init__(self, master, image=self.image, bd=0) tkinter.Label.__init__(self, master, image=self.image, bd=0)
# #
# script interface # script interface
if __name__ == "__main__": if __name__ == "__main__":
import sys
if not sys.argv[1:]: if not sys.argv[1:]:
print("Syntax: python viewer.py imagefile") print("Syntax: python viewer.py imagefile")
sys.exit(1) sys.exit(1)
filename = sys.argv[1] filename = sys.argv[1]
root = Tk() root = tkinter.Tk()
root.title(filename) root.title(filename)
im = Image.open(filename) im = Image.open(filename)