How to convert JPG to TIFF using Python

How to Convert JPG to TIFF Using Python

Converting image formats is a common task in image processing, and Python makes it simple with its powerful libraries. If you need to convert a .jpg file to a .tiff file, the Pillow (PIL) library is the most popular and reliable choice. In this guide, we'll walk you through the process step by step.

Convert JPG to TIFF using Python

Why Convert JPG to TIFF?

TIFF (Tagged Image File Format) is a lossless format that retains high-quality image data, making it ideal for professional photography, printing, and archival purposes. JPG, on the other hand, is a compressed format that loses some quality. Converting JPG to TIFF ensures better preservation of image details.

TIFF file

Key Advantages of TIFF:

  • Lossless compression – No quality degradation.
  • Supports layers and transparency – Unlike JPG.
  • Preferred for professional use – Used in printing and medical imaging.

Installing the Pillow Library

Before converting images, you need to install the Pillow library, a fork of the original PIL (Python Imaging Library). Run the following command in your terminal or command prompt:

pip install Pillow

Step-by-Step Conversion Process

Step 1: Import the Required Module

First, import the Image module from Pillow:

from PIL import Image

Step 2: Open the JPG File

Load the JPG image using Image.open():

image = Image.open("input.jpg")

Step 3: Save as TIFF

Use the save() method to convert and save the image in TIFF format:

image.save("output.tiff", "TIFF")

Complete Code Example

Here’s the full script:

from PIL import Image

# Open the JPG file
image = Image.open("input.jpg")

# Save as TIFF
image.save("output.tiff", "TIFF")

print("Conversion successful!")

Handling Multiple Files

If you need to convert multiple JPG files to TIFF, you can use a loop:

import os
from PIL import Image

# Directory containing JPG files
input_dir = "images/"
output_dir = "converted/"

# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)

# Convert all JPG files in the directory
for filename in os.listdir(input_dir):
    if filename.endswith(".jpg"):
        img = Image.open(os.path.join(input_dir, filename))
        output_filename = filename.replace(".jpg", ".tiff")
        img.save(os.path.join(output_dir, output_filename), "TIFF")

print("Batch conversion completed!")

Conclusion

Converting JPG to TIFF in Python is straightforward with the Pillow library. Whether you're working with a single file or multiple images, Python provides an efficient way to handle the conversion while maintaining image quality.

Keywords: Python convert JPG to TIFF, Pillow image conversion, batch image processing Python, lossless image format conversion, JPG vs TIFF Python.

Incoming search terms
- How to convert JPG to TIFF in Python easily
- Best Python library for image conversion
- Convert multiple JPG to TIFF using Pillow
- Step-by-step guide for JPG to TIFF conversion
- Python script for batch image format conversion
- Difference between JPG and TIFF formats
- How to preserve image quality in TIFF format
- Automate JPG to TIFF conversion with Python
- Using Pillow for professional image processing
- Save JPG as TIFF without quality loss

No comments:

Post a Comment