How to Save Images to File in Python?

As a developer working on a project for a US-based client, I recently faced the challenge of programmatically saving images. After researching and testing various methods, I discovered several effective ways to accomplish this task. In this tutorial, I will explain how to save images to file in Python.

Image Formats and File Extensions

Before getting into the code, it’s crucial to understand the different image formats and their corresponding file extensions. Some common formats include:

  • JPEG (.jpg or .jpeg): A lossy compression format widely used for photographs and web graphics.
  • PNG (.png): A lossless compression format that supports transparency, and is suitable for logos and icons.
  • GIF (.gif): A format that supports animation and is commonly used for simple graphics and logos.
  • BMP (.bmp): An uncompressed bitmap format that results in larger file sizes.

When saving images, it’s important to choose the appropriate format based on the image content and the desired quality.

Read How to Import All Functions from a File in Python?

Save Images to File in Python

Let us learn how to save images to file in Python with examples.

Method 1. Use Pillow (PIL)

Pillow is a popular Python library for image manipulation and file I/O. It provides a simple and intuitive way to save images. Here’s an example of how to save an image using Pillow:

from PIL import Image

# Open the image file
image = Image.open("golden_gate_bridge.jpg")

# Save the image with a different name and format
image.save("san_francisco_bridge.png")

I executed the above example code and added the screenshot below.

Save Images to File in Python

In this example, we open an image file named “golden_gate_bridge.jpg” using the Image.open() method. Then, we save the image with a different name and format using the save() method. The resulting image will be saved as “san_francisco_bridge.png”.

Check out How to Get the Basename of a File in Python?

Method 2. with OpenCV

OpenCV is another useful library for computer vision and image processing in Python. It provides functions for reading, displaying, and writing images. Here’s an example of saving an image using OpenCV:

import cv2

# Read the image file
image = cv2.imread("statue_of_liberty.jpg")

# Save the image with a different name and format
cv2.imwrite("new_york_landmark.png", image)

I executed the above example code and added the screenshot below.

How to Save Images to File in Python

In this code snippet, we use cv2.imread() to read an image file named “statue_of_liberty.jpg”. Then, we save the image with a different name and format using cv2.imwrite(). The resulting image will be saved as “new_york_landmark.png”.

Read How to Create a CSV File in Python?

Method 3. from URLs

Sometimes, you may need to save images directly from URLs without downloading them manually. Python provides libraries like urllib and requests to handle this task. Here’s an example using urllib:

import urllib.request

# Specify the URL of the image
url = "https://example.com/images/hollywood_sign.jpg"

# Save the image to a file
urllib.request.urlretrieve(url, "los_angeles_icon.jpg")

In this example, we specify the URL of the image we want to save. Then, we use urllib.request.urlretrieve() to download the image and save it to a file named “los_angeles_icon.jpg”.

Check out How to Write Bytes to File in Python?

Save Images to a Directory

When working with multiple images, saving them to a specific directory is often convenient. You can use the os module in Python to create directories and save images to them. Here’s an example:

import os
from PIL import Image

# Create a directory to store the images
os.makedirs("usa_landmarks", exist_ok=True)

# Open the image file
image = Image.open("washington_monument.jpg")

# Save the image to the directory
image.save("usa_landmarks/washington_dc_monument.jpg")

In this code snippet, we use os.makedirs() to create a directory named “usa_landmarks”. The exist_ok=True parameter ensures that the directory is created if it doesn’t already exist. Then, we open an image file and save it to the “usa_landmarks” directory using the save() method.

Read How to Read Large CSV Files in Python?

Conclusion

In this article, I explored how to save images to file in Python. I discussed three methods to save images to file such as using Pillow(PIL) , with openCV , from URLs. I also covered how to save images to the directory.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.