[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Rotate a PIL image in Python and tkinter

[Rotate a PIL image in Python and tkinter]

This program lets you interactively rotate an image. The program uses the following code to build its Scale and Canvas widgets.

# Build the window surface. self.angle_var = tk.IntVar() self.angle_var.trace("w", lambda *args : self.rotate()) scale = tk.Scale(self.window, from_=0, to=359, orient=tk.HORIZONTAL, variable=self.angle_var) scale.pack(side=tk.TOP, fill=tk.X, expand=False, anchor='w') self.canvas = tk.Canvas(self.window, bg='white', borderwidth=2, relief=tk.SUNKEN) self.canvas.pack(padx=10, pady=(0, 10), side=tk.BOTTOM, fill=tk.BOTH, expand=True)

The program first creates an IntVar to hold the angle of rotation. It calls that variable's trace method to make the program call a method whenever the variable's value changes. When it calls that method, the variable passes it some parameters that we don't need, so the program uses a lambda statement to ignore those parameters and call the rotate method, which is described shortly.

After setting up the trace, the code uses the IntVar when it creates the Scale widget and then packs the widget.

The program then creates the Canvas widget that will display the image.

When you adjust the Scale widget, the following rotate method executes.

def rotate(self): if self.original_pil_image is None: return angle = self.angle_var.get() self.current_pil_image = self.original_pil_image.rotate(angle) self.show_current_image()

If you have not yet loaded an image, the method exits.

Otherwise the code gets the Scale widget's current value. It calls the original image's rotate method, saving the result in self.current_pil_image and calls the following show_current_image method.

def show_current_image(self): if self.current_pil_image == None: return self.current_tk_image = ImageTk.PhotoImage(self.current_pil_image) self.canvas.delete('all') self.display_image = self.canvas.create_image(0, 0, anchor=tk.NW, image=self.current_tk_image)

This method checks whether there is a loaded image and returns if there is not. It then converts the current PIL image into a PhotoImage, deletes any existing objects from the program's Canvas widget, and creates a new image on the Canvas.

That's the gist of the program, although there are a few other details like how the program loads and saves images. Download the example to see all of the details.

For more information image processing in Python, see my Manning liveProject Algorithm Projects with Python: Image Processing. It explain how to do things like:
• Rotation• Scaling• Stretching
• Brightness enhancement• Contrast enhancement• Cropping
• Remapping colors• Image sharpening• Embossing
• Color enhancement• Sharpening• Gray scale
• Black and white• Sepia tone• Other color scales
© 2024 Rocky Mountain Computer Consulting, Inc. All rights reserved.