2013-04-21 10:38:36 +04:00
|
|
|
#!/usr/bin/env python
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# this demo script illustrates pasting into an already displayed
|
|
|
|
# photoimage. note that the current version of Tk updates the whole
|
|
|
|
# image everytime we paste, so to get decent performance, we split
|
|
|
|
# the image into a set of tiles.
|
|
|
|
#
|
|
|
|
|
2012-10-17 06:58:29 +04:00
|
|
|
try:
|
2015-04-24 11:24:52 +03:00
|
|
|
from tkinter import Tk, Canvas, NW
|
2012-10-17 06:58:29 +04:00
|
|
|
except ImportError:
|
2015-04-24 11:24:52 +03:00
|
|
|
from Tkinter import Tk, Canvas, NW
|
2012-10-17 06:58:29 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
from PIL import Image, ImageTk
|
|
|
|
import sys
|
|
|
|
|
|
|
|
#
|
|
|
|
# painter widget
|
|
|
|
|
2015-04-24 02:41:33 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class PaintCanvas(Canvas):
|
|
|
|
def __init__(self, master, image):
|
|
|
|
Canvas.__init__(self, master, width=image.size[0], height=image.size[1])
|
|
|
|
|
|
|
|
# fill the canvas
|
|
|
|
self.tile = {}
|
|
|
|
self.tilesize = tilesize = 32
|
|
|
|
xsize, ysize = image.size
|
|
|
|
for x in range(0, xsize, tilesize):
|
|
|
|
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)
|
2015-04-24 02:41:33 +03:00
|
|
|
self.tile[(x, y)] = box, tile
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.image = image
|
|
|
|
|
|
|
|
self.bind("<B1-Motion>", self.paint)
|
|
|
|
|
|
|
|
def paint(self, event):
|
|
|
|
xy = event.x - 10, event.y - 10, event.x + 10, event.y + 10
|
|
|
|
im = self.image.crop(xy)
|
|
|
|
|
|
|
|
# process the image in some fashion
|
|
|
|
im = im.convert("L")
|
|
|
|
|
|
|
|
self.image.paste(im, xy)
|
|
|
|
self.repair(xy)
|
|
|
|
|
|
|
|
def repair(self, box):
|
|
|
|
# update canvas
|
|
|
|
dx = box[0] % self.tilesize
|
|
|
|
dy = box[1] % self.tilesize
|
|
|
|
for x in range(box[0]-dx, box[2]+1, self.tilesize):
|
|
|
|
for y in range(box[1]-dy, box[3]+1, self.tilesize):
|
|
|
|
try:
|
|
|
|
xy, tile = self.tile[(x, y)]
|
|
|
|
tile.paste(self.image.crop(xy))
|
|
|
|
except KeyError:
|
2015-04-24 02:41:33 +03:00
|
|
|
pass # outside the image
|
2010-07-31 06:52:47 +04:00
|
|
|
self.update_idletasks()
|
|
|
|
|
|
|
|
#
|
|
|
|
# main
|
|
|
|
|
|
|
|
root = Tk()
|
|
|
|
|
|
|
|
im = Image.open(sys.argv[1])
|
|
|
|
|
|
|
|
if im.mode != "RGB":
|
|
|
|
im = im.convert("RGB")
|
|
|
|
|
|
|
|
PaintCanvas(root, im).pack()
|
|
|
|
|
|
|
|
root.mainloop()
|