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

View File

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

View File

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

View File

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

View File

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