Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

PIL Image.save(): Saving JPEG File in Python

  • 30 Oct, 2025
  • Com 0
PIL Image.save() Method in Python

To save an image in JPEG format in Python, use the PIL library’s Image.save() method. The Image.save() method serializes an Image object to a JPEG file or stream.

What you need to do is load or create an image, then save it as a JPEG. Format is inferred from .jpg extension; specify format=’JPEG’ for streams.

Here is the existing PNG image that we will load and then save as a JPEG.

sample image

from PIL import Image

# Loading an existing image
im = Image.open('Tower.png')  # Assume RGB mode

# Converting to RGB (if not already in that mode)
rgb_image = im.convert('RGB')

# Saving an image to file (quality=75 default)
rgb_image.save('output.jpg')  # Infers JPEG from extension

print("Image saved as output.jpg in RGB mode.")

# Output: Image saved as output.jpg in RGB mode.

output jpeg image

The image above is a JPEG, which takes much less space than the original PNG.

In this code, we first load an existing PNG image. The image was not already in RGB mode, so we converted it with .convert(‘RGB’) and saved it with the .save(output.jpg) method.

The JPEG supports the following modes:

  1. L: 8-bit grayscale (1 channel).
  2. RGB: 24-bit color (3 channels).
  3. CMYK: 32-bit print (4 channels).

Syntax

image.save(fp, format, quality, exif, subsampling)

Parameters

Arguments Description
fp It represents a file name. It can be a string or an actual file object.
format It is an optional string that describes the output image format. For example, PNG or JPEG.
quality It represents compression and optimization options.
exif It preserves metadata and color info.
subsampling It represents a chroma subsampling level (affects quality and size).

Controlling JPEG Quality

By default, the JPEG quality is 75, but you can manually set it by passing the “quality” argument to the save() function.

from PIL import Image

# Loading an existing image
im = Image.open('Tower.png')  # Assume RGB mode

# Converting to RGB (if not already in that mode)
rgb_image = im.convert('RGB')

# Saving an image to file (quality=75 default)
rgb_image.save("output_high.jpg", "JPEG", quality=95)
rgb_image.save("output_medium.jpg", "JPEG", quality=70)
rgb_image.save("output_low.jpg", "JPEG", quality=30)

print("Different type of JPEG Qualities have been saved.")

# Output: Different type of JPEG Qualities have been saved.

Here are three images with three different qualities:

Different type of JPEG qualities

The qualities range from 1 to 95 (100 allowed but not recommended — no gain, bigger size). It affects file size and detail retention.

Progressive JPEG (for Web)

Progressive JPEGs load in layers (from blurry to clear), which is beneficial for the web.

from PIL import Image

# Loading an existing image
im = Image.open('Tower.png')  # Assume RGB mode

# Converting to RGB (if not already in that mode)
rgb_image = im.convert('RGB')

# Progressive JPEG saving
rgb_image.save("progressive.jpg", "JPEG", progressive=True)

print("Progressive JPEG saved.")

# Output: Progressive JPEG saved.

progressive jpegCombine with compression

Let’s combine progressive image with optimization, quality, and compression.

from PIL import Image

# Loading an existing image
im = Image.open('Tower.png')  # Assume RGB mode

# Converting to RGB (if not already in that mode)
rgb_image = im.convert('RGB')

# Progressive JPEG saving
rgb_image.save("web_optimized.jpg", "JPEG", quality=80,
               optimize=True, progressive=True)

print("Compressing and optimizing a JPEG image")

# Output: Compressing and optimizing a JPEG image

web_optimized image

That’s all!

Post Views: 7
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

numpy.ndarray.tobytes: Converting Numpy Array to Python Bytes
How to Check If Numpy Array Contains an Element

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend