Python PIL Image.save() method

Python PIL Image.save() Method

PIL is the Python Imaging Library, which provides image editing capabilities for the Python interpreter. The Image module provides a class of the same name that represents a PIL Image. The module also provides factory functions for loading images from files and creating new images.

Image.save() saves the image to the given filename. If no format is specified, the format to be used is determined by the filename’s extension, if possible.

Keyword options can be used to provide additional instructions to the writer. If the writer does not recognize an option, it is silently ignored. 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 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 – An optional format override. If omitted, the format to use is determined by the filename’s extension. This parameter should always be used if a file object is used instead of a filename.
options – Additional parameters for the image writer.

Return Value: None

Throws:

KeyError – If the output format cannot be determined from the filename. Use the format options to resolve this issue.
IOError – If the file could not be written. The file may have already been created and may contain partial data.

Images used:
Python PIL Image.save() method

# Importing Image module from PIL package
from PIL import Image
import PIL
  
# creating an image object (main image)
im1 = Image.open(r"C:UsersSystem-PcDesktopflower1.jpg")
  
# save an image using extension
im1 = im1.save("geeks.jpg")

Output:
Python PIL Image.save() method

Leave a Reply

Your email address will not be published. Required fields are marked *