6 Ways to Save image to file in Python

In this Python tutorial, we will learn how to save an image to file in python.

Saving an image to a file in Python can be done using various methods. Here are some of the most common ways to save an image in Python:

Table of Contents

How to Python save an image to file

Let us see in detail the above 6 ways to save an image to a file in Python.

Method-1: Python save an image to file using OpenCV library

OpenCV is a popular computer vision library that provides a function cv2.imwrite() to save an image to a file in Python.

# Import the necessary libraries import cv2 import os # Set the file path for the source image path = r'C:\Users\Administrator.SHAREPOINTSKY\Downloads\dora.jpg' # Set the directory for saving the image directory = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work' # Load the image using OpenCV img = cv2.imread(path) # Change the working directory to the specified directory for saving the image os.chdir(directory) # Print the list of files in the directory before saving the image print("Before saving") print(os.listdir(directory)) # Save the image with the filename "cat.jpg" filename = 'cat.jpg' cv2.imwrite(filename, img) # Print the list of files in the directory after saving the image print("After saving") print(os.listdir(directory)) 

The above code uses the OpenCV and OS libraries in Python to perform the following tasks:

Here, we can the list of directories before and after saving as the output. You can refer to the below screenshot for the output.

Python save the file with OpenCV2

Method-2: Python save an image to file using the PIL library

PIL (Python Imaging Library) is another popular library for image manipulation in Python. It provides a method Image.save() to save an image to a file.

# Import the Image module from the PIL library from PIL import Image import PIL # Open the image with the specified file path picture = Image.open(r'Downloads\3.jpg') # Save the image with the specified file name picture = picture.save("dolls.jpg") 

The above code uses the Python Imaging Library (PIL) to perform the following tasks:

How to save an image using a pillow in python

Method-3: Python save an image to file using the matplotlib library

Matplotlib is a plotting library in Python that provides a function savefig() to save a figure to a file. To save an image, you can first plot it using Matplotlib and then save it using the savefig() function.

# Import the matplotlib.pyplot library as plt import matplotlib.pyplot as plt # Read the image file img = plt.imread("/content/simon-berger.jpg") # Display the image plt.imshow(img) # Save the image plt.savefig("saved_image.jpg") 

The above code uses the matplotlib library to perform the following tasks:

Python save an image to file using the matplotlib

Method-4: Python save an image to file using the URLLIB library

Another way to save an image to a file in Python is by using the urllib library. The urllib library provides a function urlretrieve() that can be used to download an image from a URL and save it to a file.

# Import the urllib and PIL libraries import urllib.request from PIL import Image import PIL # Retrieve the image from the specified URL and print the result print(urllib.request.urlretrieve("https://bit.ly/3oAeohK")) # Open the image with the specified file name image = PIL.Image.open("new.png") # Show the image image.show() 

The above code uses the urllib and Python Imaging Library (PIL) to perform the following tasks:

  1. Imports the urllib and PIL libraries using the import urllib.request and from PIL import Image statements.
  2. Retrieves the image from the specified URL using the urllib.request.urlretrieve() function and stores the result in the new.png file.
  3. Opens the image with the specified file name new.png using the PIL.Image.open() function and stores it in the image variable.
  4. Displays the image using the image.show() function.

The URL is saved in the image format as the output in the below screenshot.

Python save an image to file from URL

Method-5: Python save an image to file using the pickle module

The pickle module in Python can also be used to save an image to a file. Pickle is a module that allows you to serialize and deserialize Python objects, including images.

# Import the pickle and matplotlib.pyplot modules import pickle import matplotlib.pyplot as plt # Read the image file with the specified file path img = plt.imread("/content/saved_image.jpg") # Open the file with write binary mode to store the image using pickle with open("saved_image.pickle", "wb") as f: # Dump the image data into the file using pickle pickle.dump(img, f) 

The above code performs the following tasks:

Python save an image to file using the pickle

Method-6: Python save an image to file using the skimage library

Scikit-image is a library for image processing in Python that provides a function imsave() to save an image to a file.

# Import the imsave and imread functions from the skimage.io module from skimage.io import imsave, imread # Read the image file with the specified file path img = imread("/content/simon-berger.jpg") # Save the image to disk with the specified file name imsave("saved_image.jpg", img)

The above code uses the skimage library to perform the following tasks:

  1. Imports the imsave and imread functions from the skimage.io module using the from skimage.io import imsave, imread statement.
  2. Reads the image file with the specified file path /content/simon-berger.jpg using the imread() function and stores it in the img variable.
  3. Saves the image to disk with the specified file name saved_image.jpg using the imsave() function. The img variable is passed as the image data to be saved.

Python save an image to file using the skimage

You may also like to read the following Python tutorials.

In this tutorial, we have learned about how to save an image to file in python, and also we have covered these methods:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.