Skip to content
Automate TIFF to PNG conversion with Python

Automating TIFF to PNG Conversion with Python

Daniel Gorski
Daniel Gorski June 02, 2023
3 min read

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!

Related posts

▸ All posts
PowerShell script to automate NAV tenant sync

Auto-Retry NAV Tenant Sync with PowerShell

June 08, 2023 · Daniel Gorski

Failed NAV tenant syncs no longer need a manual rerun: this PowerShell script with try-catch handling retries the synchronization automatically.

Read More
Read More: Streamline NAV Tenant Synchronization with PowerShell
Migrate your Business Central pipeline to AL-Go

Switch to AL-Go right now!

January 20, 2023 · Daniel Gorski

AL-Go is the fastest way to build production-ready CI/CD pipelines for Business Central. Learn how to start fresh or migrate with our automation template.

Read More
Read More: Switch to AL-Go right now!
Automatic PDF Export with DOCFX

DOCFX PDF Export with wkhtmltopdf and Azure

March 24, 2022 · Jannic Weidel

Here's how we extend our DOCFX documentation with automated PDF export using wkhtmltopdf, Azure Pipeline, and templates.

Read More
Read More: How-To: Automatically Generate PDF Files from DOCS Documentation (DOCFX)