Converting images from TIF to PNG

alameer: <p>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?</p>
<pre><code class="lang-auto">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)
</code></pre>

ptrblck: <p>Could you post an example result image containing these lines?</p>
<p>You could try to specify an interpolation method in <code>im.thumbnail</code> e.g. as <code>Image.ANTIALIAS</code>, but your current issue seems to be more significant than a quality issue.</p>
<p>PS: as this seems to be <code>PIL</code> specific, you might get a faster and better answer on e.g. StackOverflow <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

alameer: <p>Please see attached copy of the outputted image.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/a/0a804a3cf5021990129f1396df318bb98e37cd34.png" data-download-href="https://discuss.pytorch.org/uploads/default/0a804a3cf5021990129f1396df318bb98e37cd34" title="Position026 (RGB rendering) - 1024 x 1024 x 1 x 1 - 3 ch (8 bits)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a804a3cf5021990129f1396df318bb98e37cd34_2_500x500.png" alt="Position026 (RGB rendering) - 1024 x 1024 x 1 x 1 - 3 ch (8 bits)" data-base62-sha1="1uTDlXL4TP4WFsLXpWUHR8TiZ6I" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a804a3cf5021990129f1396df318bb98e37cd34_2_500x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a804a3cf5021990129f1396df318bb98e37cd34_2_750x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/a/0a804a3cf5021990129f1396df318bb98e37cd34_2_1000x1000.png 2x" data-dominant-color="353536"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Position026 (RGB rendering) - 1024 x 1024 x 1 x 1 - 3 ch (8 bits)</span><span class="informations">1024×1024 530 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

alameer: <p>If i add Image.ANTIALIAS i get an error message:</p>
<p>‘TiffImageFile’ object has no attribute ‘ANTIALIAS’</p>

ptrblck: <p><code>Image</code> would be the <code>PIL.Image</code> namespace, not the tiff image object.</p>
<p>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).</p>