0

Hi i am trying to convert the Tiff file into png or jpg file but the ouput that i am getting is noisy and not what i expected. Below is the code that i have tried :

from PIL import Image
im = Image.open('/content/img.tif')
import numpy as np
imarray = np.array(im)
print(imarray)
from matplotlib import pyplot as plt
plt.imshow(imarray, interpolation='nearest')
plt.show() # To see how the tiff file looks like
import cv2
from PIL import Image, ImageOps
img = (np.maximum(imarray, 0) / imarray.max()) * 255.0
print(img)
img = 255 - img #Inverting the pixel
print("********************************************************************")
print(img)
img = Image.fromarray(np.uint8(img))
img.save(f'/content/img.png')

please find the sample tiff file here

https://drive.google.com/file/d/1Gfyo4dCo_4pfYvUn6_a6lD0SfxZOzUwK/view?usp=sharing

Output png/jpg image i was getting is this enter image description here

Can anyone please help me in converting the tiff into jpg or png

Thanks

NeedToCodeAgain
  • 129
  • 2
  • 9
  • 1
    The output image you are getting looks good, right? – Ander Biguri May 23 '22 at 13:10
  • 1
    Your title says you want to convert TIFF to JPEG. Your question says you want to convert TIFF to zip. Your code tries to convert TIFF to PNG??? – Mark Setchell May 23 '22 at 13:37
  • Sorry for the typo @MarkSetchell i have edited now. – NeedToCodeAgain May 23 '22 at 13:52
  • And @AnderBiguri no this does seems like a good conversion but the chest area you can see have a problem – NeedToCodeAgain May 23 '22 at 13:52
  • There's no JPG/PNG. TIFF, PNG and JPEG are completely different image formats. TIFF and PNG use lossless compression, JPEG is lossy. JPEG will *always* contain artifacts compared to either TIFF or PNG. – Panagiotis Kanavos May 23 '22 at 13:53
  • 1
    @Blackfly `you can see have a problem` no we can't because we have no idea what this was supposed to look like. In any case, it's your own code that modifies the image before saving it as PNG (not JPG). Have you tried saving the original image with `im.save(f'/content/img.png')` ? – Panagiotis Kanavos May 23 '22 at 13:54
  • 2
    Please share your input TIFF - you'll probably need to use Dropbox or Google Drive or similar. – Mark Setchell May 23 '22 at 13:56
  • If you want to invert the image you can use `pil.ImageOps.invert(im)` – Panagiotis Kanavos May 23 '22 at 13:57
  • @Blackfly I can not see the problem no. Can you show me what the problem is? Are you sure you are not confusing normal chest features by noise? – Ander Biguri May 23 '22 at 14:01
  • Thank you for the response everyone, I have added tiff sample file and by png/JPG i meant converting tiff to either on of those will work. Just to avoid confusion i changed png/jpg to png or jpg – NeedToCodeAgain May 24 '22 at 04:22
  • 1
    The processed image looks like a reasonable interpretation of your input image to me - what exactly is wrong with your results and can you show what you were hoping for please? – Mark Setchell May 24 '22 at 10:39

1 Answers1

0

The code below worked for me to read .tiff image and save layers as .jpeg:

from PIL import Image, ImageSequence
#open tiff image 
im = Image.open("YOUR IMAGE PATH")
#navigate to the folder were the layers are going to be saved 
%cd YOUR DIRECTORY

#loop over layers and export jpeg instances
for i, page in enumerate(ImageSequence.Iterator(im)):
    page.mode = 'I'
    page.point(lambda i:i*(1./256)).convert('L').save(str(i)+'.jpeg')
  • Hi @Dalia Mahdy, i tried your code but what i am getting is the high contrast image (https://imgur.com/a/d9DjkOL), if possible can you please show me the output you got – NeedToCodeAgain Aug 03 '22 at 04:59