Take the 2-minute tour ×

Is there a way to select a region in a image with mouse click and crop these region using python PIL ? How can i do it?


share|improve this question
Once you have the mouse click coordinates it would be easy in PIL, but are you asking how to get the mouse clicks? That would be beyond the scope of PIL. –  Mark Ransom Jul 7 '11 at 16:21
What gui lib would you be using? –  Steven Rumbalski Jul 7 '11 at 16:23
add comment

2 Answers

The PIL library itself provides no GUI code --what you are asking for is an application with a GUI. I'd suggest using Tkinter + PIL, but there is no way it is trivial - you will have to handle the mouse clicks, create a rectangle object tracking it, have a way to "reset" the rectangle, and so on.

Unfortunatelly the Canvas Tkinter widget which is used to draw things on is poorly documented - you will have to read trough it here: http://www.pythonware.com/library/tkinter/introduction/canvas.htm

Bellow there is an example code that reads an image file from the disk and draws it on a tkinter window. As you can see, here is some object juggling to get it right.

import Tkinter
import Image, ImageTk, ImageDraw

image_file = "svg.png"

w = Tkinter.Tk()

img = Image.open(image_file)
width, height = img.size
ca = Tkinter.Canvas(w, width=width, height=height)
photoimg = ImageTk.PhotoImage("RGB", img.size)
ca.create_image(width//2,height//2, image=photoimg)
share|improve this answer
First of all thank you for your answer. I use your example, but i could not select the image with mouse and crop the select image. –  Carlos Pinto da Silva Neto Jul 10 '11 at 22:02
add comment

the answer at this post is pretty well developed: Image Cropping using Python

it uses pygame as the GUI.

share|improve this answer
add comment

Your Answer


