简体   繁体   English

将 Tiff 文件转换为 JPG 或 Png python

[英]Converting Tiff file to JPG or Png python

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.嗨,我正在尝试将 Tiff 文件转换为 png 或 jpg 文件,但我得到的输出很嘈杂,而不是我所期望的。 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请在此处找到示例 tiff 文件

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

Output png/jpg image i was getting is this我得到的 Output png/jpg 图像是这个在此处输入图像描述

Can anyone please help me in converting the tiff into jpg or png谁能帮我将tiff转换为jpg或png

Thanks谢谢

The code below worked for me to read.tiff image and save layers as.jpeg:下面的代码对我有用,可以读取.tiff 图像并将图层另存为.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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM