The Wayback Machine - https://web.archive.org/web/20250312101329/https://www.geeksforgeeks.org/python-pil-image-save-method/
Open In App

Python PIL | Image.save() method

Last Updated : 02 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The

Image

module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

Image.save()

Saves this image under the given filename. If no format is specified, the format to use is determined from the filename extension, if possible. Keyword options can be used to provide additional instructions to the writer. If a writer doesn’t recognise an option, it is silently ignored. The available options are described in the image format documentation for each writer. You can use a file object instead of a filename. In this case, you must always specify the format. The file object must implement the seek, tell, and write methods, and be opened in binary mode.

Syntax: Image.save(fp, format=None, **params) Parameters:fp – A filename (string), pathlib.Path object or file object. format – Optional format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used. options – Extra parameters to the image writer. Returns: None Raises:KeyError – If the output format could not be determined from the file name. Use the format option to solve this. IOError – If the file could not be written. The file may have been created, and may contain partial data.

Image Used:

Python3 1==
 

# Importing Image module from PIL package 
from PIL import Image 
import PIL 

# creating a image object (main image) 
im1 = Image.open(r"C:\Users\System-Pc\Desktop\flower1.jpg") 

# save a image using extension
im1 = im1.save("geeks.jpg")

Output:


Python PIL | Image.save() method – FAQs

What is the PIL Function in Python?

PIL, or Python Imaging Library, now known as Pillow, is a library in Python that provides capabilities for opening, manipulating, and saving many different image file formats. It is not a single function but a library that includes functions to process images and graphics.

How to Save Images Using PIL?

To save images using PIL (Pillow), you first need to open or create an image file, and then you can use the .save() method to write it to a file in the desired format.

Example:

from PIL import Image

# Create an image with RGB mode
img = Image.new('RGB', (100, 100), color = 'red')

# Save the image
img.save('pil_red.png')

How to Import PIL Image in Python?

To import and use the image functionality in PIL (Pillow), you generally import the Image class from the PIL module.

Example:

from PIL import Image

How to Open an Image in Python PIL?

To open an image using PIL (Pillow), you use the Image.open() method, which loads an image from a file and returns an Image object.

Example:

from PIL import Image

# Open an image file
img = Image.open('path_to_image.jpg')
img.show() # Display the image

Why Do We Use PIL?

PIL (Pillow) is used in Python for several reasons:

  1. Versatility: It supports a wide range of image file formats, easily handling conversions between them.
  2. Powerful Imaging Capabilities: It provides extensive file manipulation capabilities including rotating, resizing, filtering, and much more.
  3. Ease of Use: The library simplifies image processing tasks, making complex operations on images straightforward with minimal code.
  4. Integration: It works well with other Python libraries such as NumPy for further manipulation, making it a popular choice in scientific computing contexts.
  5. Graphics: Apart from image processing, it also supports drawing text, lines or shapes directly onto images, which is useful for many graphical applications.

Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg