[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: Make thumbnails for images in Python and PIL

[The program creating thumbnail images]

This example demonstrates several useful techniques including selecting multiple files, splitting a file name into its parts, and resizing images in Python.

Selecting Files and Splitting Names

When you click Select Files, the program uses the following select_files method to let the user select multiple image files at once.

def select_files(self): filenames = askopenfilenames() if filenames != '': self.filenames = filenames self.file_list.delete(0, tk.END) for filename in self.filenames: name = os.path.basename(filename) self.file_list.insert(tk.END, name)

This method uses the tkinter.filedialog askopenfilenames method to let the user select multiple files. If the user cancels the dialog, askopenfilenames returns a blank string and the method exits.

If the user did not cancel the dialog, the code saves the file names in self.filenames so it can get the full names later.

The code then loops through the names, uses os.path.basename to get the file's name without its path, and adds the base name to the self.file_list ListBox widget. (Download the example to see how it creates its controls including the ListBox.)

Resizing Images

When you click Make Thumbs, the program executes the following make_thumbs method.

def make_thumbs(self): wid = int(self.thumb_wid.get()) hgt = int(self.thumb_wid.get()) for filename in self.filenames: self.make_thumbnail(filename, wid, hgt) messagebox.showinfo('Done', 'Thumbnails created.')

This method gets the values that you entered in the Width and Height text boxes. It then loops through the file names saved in self.filenames, which is a tuple holding the fully qualified path names to the selected files.

For each file name, the program calls the make_thumbnail method described shortly. The method finishes by displaying a success message.

The following code shows how the program creates a single thumbnail.

def make_thumbnail(self, filename, wid, hgt): name, extension = os.path.splitext(filename) thumb_name = name + "_thumb" + ext with Image.open(filename) as image: image.thumbnail((wid, hgt)) image.save(thumb_name)

This code uses os.path.splitext to split the file's name into its name (including the path) and its extension. It creates the thumbnail's file name by adding "_thumb" to the original file name and replacing the extension. If you would like to change the extension, for example to make all the thumbnails JPG files, change the code to do that here.

Next, the program uses a with statement to open the original image.

It then calls the thumbnail method to make the thumbnail image. That method reduces the image's size so it fits within the desired dimensions but it maintains the image's aspect ratio (width to height ratio). For example, if the original image is 300 × 200 pixels and the desired size is 150 × 150 pixels, then the thumbnail has size 150 & times; 100. Note that the thumbnail method modifies the image in place rather than returning a new image.

The method finishes by saving the thumbnail with its new file name.

Download the example to see all of the details.

© 2024 - 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.