[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[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: Save a smiley face image in a file in Python

[This program saves a smiley face drawn with PIL in Python]

My earlier example Draw a smiley face with PIL in Python showed how to draw a smiley face. That example displays smiley faces but doesn't save them into a file, and that's where this example comes in. It lets you save a smiley face into a file with either a transparent or gradient background so you can use load it into other programs. (Yes, that's foreshadowing. I'm going to use the smiley face file in a later post.)

Saving Smileys

Saving the smiley face into a file is actually pretty easy once you have the smiley face drawing code from the earlier post. When you click the Save button, the following code draws and saves the smiley face.

def save(self): # Get the canvas's dimensions. self.canvas.update() wid = int(self.width_var.get()) hgt = int(self.height_var.get()) # Make an image to draw on. image = Image.new('RGBA', (wid, hgt), None) dr = ImageDraw.Draw(image) # Fill with a linear gradient. if not self.transparent_var.get(): start_point = (0, 0) end_point = (0, hgt) start_color = (144, 238, 144, 255) # Light green. end_color = (0, 100, 0, 255) # Darkgreen. brush = LinearGradientBrush(start_point, end_point, start_color, end_color) bbox = (0, 0, wid, hgt) pil_fill_rectangle(image, brush, bbox) # Draw the smiley. margin = 10 bbox = margin, margin, wid - margin, hgt - margin pil_draw_smiley(dr, *bbox) # Save the image. filename = self.file_var.get() image.save(filename) # Display the result. self.photo_image = ImageTk.PhotoImage(image) self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo_image)

This code first gets the values that you entered for the image's width and height. It then creates an image with those dimensions. The code passes None into Image.new as its final parameter to make it fill the new image with a transparent background.

Next, if you didn't check the Transparent Background box, the program fills the new image with a linear gradient shading from light green to dark green. For information about linear gradient brushes, see my earlier post Make a LinearGradientBrush class for use with PIL in Python.

The method them calls the pil_draw_smiley function to draw the smiley on the image. It shrinks the smiley's target rectangle to allow a bit of a margin around the face. You may want to remove the margin, particularly if you're using a transparent background.

Next, the code gets the name of the file that you entered and saves the image in that file. Finally, the program displays the image so you can see the result and so you know something happened.

Conclusion

This program's save method makes it easy to save smiley faces with different dimensions into files. My next example will use a smiley face file to show how you can paste an image onto another image.

Download the example to experiment with it and to see additional details.

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