Converting images from TIF to PNG

Hi, I’m not sure if this forum would help with my query. If not apologies for my question. I have a number of images that i would like to convert from Tiffs to PNGs. These images will then be fed to my model to carry out the training. This is the code i am running to convert the images. It seem to run without any issue but the converted image is done with lines across the half of the image and in grey scale. Does anyone have any idea why is this the case please?

import os
from PIL import Image

yourpath = os.getcwd()
for root, dirs, files in os.walk(yourpath, topdown=False):
    for name in files:
        print(os.path.join(root, name))
        if os.path.splitext(os.path.join(root, name))[1].lower() == ".tif":
            if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + ".png"):
                print ("A jpeg file already exists for %s" % name)
            # If a jpeg is *NOT* present, create one from the tiff.
            else:
                outfile = os.path.splitext(os.path.join(root, name))[0] + ".png"
                try:
                    im = Image.open(os.path.join(root, name))
                    print("Generating jpeg for %s" % name)
                    im.thumbnail(im.size)
                    im.save(outfile, "JPEG", quality=100)
                except Exception as e:
                    print (e)

Could you post an example result image containing these lines?

You could try to specify an interpolation method in im.thumbnail e.g. as Image.ANTIALIAS, but your current issue seems to be more significant than a quality issue.

PS: as this seems to be PIL specific, you might get a faster and better answer on e.g. StackOverflow :wink:

Please see attached copy of the outputted image.

If i add Image.ANTIALIAS i get an error message:

‘TiffImageFile’ object has no attribute ‘ANTIALIAS’

Image would be the PIL.Image namespace, not the tiff image object.

Unfortunately, I don’t know what might cause these issues and don’t think that the interpolation method would change anything (if these errors are not visible in the input image).