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
Let us see in detail the above 6 ways to save an image to a file in Python.
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.
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:
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:
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:
The URL is saved in the image format as the output in the below screenshot.
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:
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:
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.