top of page
Daniel Gorski

Automating TIFF to PNG Conversion with Python

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) 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. By using the Python script, you can automate the entire conversion process, significantly reducing the time and effort required.

  2. Efficient Batch Processing: The script allows you to convert multiple TIFF files in a batch. Instead of processing each file individually, the script iterates over all the files in a specified directory, converting them to PNG format one by one. This batch processing capability further enhances productivity.

  3. Error Handling: The script incorporates exception handling mechanisms to catch any errors that may occur during the conversion process. This ensures that the script continues processing even if it encounters an error with a particular file. By printing the problematic file names, you can easily identify and address any issues.

  4. Flexibility and Customization: Since the script is written in Python, a versatile programming language, you have the freedom to customize it according to your specific requirements. You can modify the script to include additional functionalities, such as resizing the images or applying image filters, to suit your needs.

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!

115 views2 comments

2 Comments


Hi, I was testing this script and it didn't work. I removed the try and except to actually see the error the occurs with path.save(path) and it's the following: Traceback (most recent call last):

File "C:\Users\Name\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL\PngImagePlugin.py", line 1279, in _save

rawmode, mode = _OUTMODES[mode]

~~~~~~~~~^^^^^^

KeyError: 'F'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File "c:\Users\Name\Scripts\convert_tiff2png.py", line 18, in <module>

page.save(path)

File "C:\Users\Name\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL\Image.py", line 2439, in save

save_handler(self, fp, filename)

File "C:\Users\Name\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL\PngImagePlugin.py", line 1282, in _save

raise OSError(msg) from e

OSError: cannot write mode F as PNG Do you know how to solve it? Thanks


Like
Replying to

Hi, Due to the fact that the TIFF image you're trying to convert has a mode 'F' (32-bit floating point pixels), which is not directly supported by the PNG format. The PNG format does not support this specific image mode, so PIL (Python Imaging Library) raises an error when attempting to save it. Try to convert it to grayscale:                 try:

                    # Convert the image mode to 'L' or 'RGB' if the mode is 'F'

                    if page.mode == 'F':

                        page = page.convert('L')  # Convert to grayscale

     …

Like
bottom of page