Automate TIFF to PNG conversion with Python

Automating TIFF to PNG Conversion with Python

Daniel Gorski
Daniel Gorski 02. Juni 2023
3 Min. Lesezeit

Introduction

In today's digital era, images are widely used for various purposes, from documentation to multimedia production.

Often, we encounter situations where we need to convert TIFF (Tagged Image File Format) images to PNG (Portable Network Graphics) format. Manually converting a large number of files can be time-consuming and tedious. Thankfully, Python provides a powerful and efficient solution for automating this process.

In this blog post, we will explore a Python script that can effortlessly convert TIFF files to PNG format, making the conversion task quick and hassle-free.

The Python Script

The Python script we will discuss utilizes the PIL (Python Imaging Library), specifically its Image and ImageSequence modules, to handle image manipulation tasks. PIL is a popular library for working with various image file formats and performing operations such as resizing, cropping, and format conversion.

With PIL, we can easily open TIFF files, extract their individual pages, and save them as PNG files.

from PIL import Image, ImageSequence
import os
import os.path
import glob

files = glob.glob('C:\\\\Install\\\\Temp\\\\tiffs\\\\*.tiff')

for file in files:
    filename_ext = os.path.basename(file)
    filename = os.path.splitext(filename_ext)[0]
    try:
        im = Image.open(file)
        for i, page in enumerate(ImageSequence.Iterator(im)):
            path = "C:\\\\Install\\\\Temp\\\\tiffs-pngs\\\\" + filename + "-" + str(i+1) + ".png"
            if not os.path.isfile(path):
                try:
                    page.save(path)
                except:
                    print(filename_ext)
    except:
        print(filename_ext)

The Benefits of Using the Python Script

  1. Time-saving Automation
    Converting a large number of TIFF files to PNG format manually can be extremely time-consuming. This script automates the process, saving you significant time and effort.

  2. Efficient Batch Processing
    Convert multiple TIFF files in a batch by iterating over all files in a specified directory.

  3. Error Handling
    The script uses exception handling to ensure the process continues even if a file fails to convert.

  4. Flexibility and Customization
    You can modify the script to include additional features like resizing or filtering images.

Conclusion

Automating the conversion of TIFF files to PNG format using Python provides numerous advantages, including time-saving automation, efficient batch processing, error handling, and customization options.

By utilizing the PIL library and the provided Python script, you can streamline your image conversion workflow and improve productivity. Whether you need to convert a few TIFF files or deal with a large number of them regularly, this script is a valuable tool that simplifies the process and saves you valuable time and effort.

Give it a try and experience the power of Python in automating your image conversion tasks!

beyondit
tech
Python
Image Processing
Automation
E-Invoice Viewer for Business Central

New Open-Source E-Invoice Viewer for Business Central

View and convert German XRechnung XML files directly in Microsoft Dynamics 365 Business Central with the new open-source E-Invoice Viewer from BEYONDIT.

Read More
Read More
E-Invoice Viewer für Business Central

Neue Open-Source-App: E-Invoice Viewer für Business Central

Lesen und konvertieren Sie deutsche XRechnung-XML-Dateien direkt in Microsoft Dynamics 365 Business Central mit dem neuen Open-Source E-Invoice Viewer von BEYON

Read More
Read More