简体   繁体   English

如何使用 Python 将图像从一种格式转换为另一种格式?

[英]How to convert an image from one format to another with Python?

I have to write an application for image processing in python.我必须用 python 编写图像处理应用程序。 Does anyone know how to convert the file type of an image from JPEG to TIFF?有谁知道如何将图像的文件类型从 JPEG 转换为 TIFF?

Check out the Python Image Library (PIL) . 查看Python图像库(PIL) See this tutorial , the PIL is quite easy to use. 看到本教程 ,PIL非常易于使用。

Supported image formats . 支持的图像格式

To do the conversion you open the image and then save it with the new extension (which PIL uses to determine what format to use for saving). 要进行转换,请打开图像,然后将其保存为新的扩展名(PIL使用该扩展名来确定用于保存的格式)。

import Image
im = Image.open('test.jpg')
im.save('test.tiff')  # or 'test.tif'

Note that the official distribution does not support Python 3.x (yet?), however, at least under Windows there's an unofficial version available that works with v 3.x. 请注意,官方发行版尚不支持Python 3.x(但?),但是,至少在Windows下,存在与v 3.x兼容的非官方版本

Use the Python Imaging Library (PIL). 使用Python Imaging Library(PIL)。

from PIL import Image
img = Image.open('image.jpeg')
img.save('image.tiff')

Ref: http://effbot.org/imagingbook/image.htm 参考: http : //effbot.org/imagingbook/image.htm

Did you try to use PIL ? 您尝试使用PIL吗? It can support many image file format. 它可以支持多种图像文件格式。

I generally use OpenCV (cv2) to convert them like:我通常使用 OpenCV (cv2) 将它们转换为:

import os
import cv2

path = r"D:\Folder"
i=0
for x in os.listdir(path):
    image = cv2.imread(r"{}\{}".format(path,x))
    cv2.imwrite(r'{}\{}.png'.format(path,i),image)
    i+=1

(replace png with whatever extension you want, in your case.tiff) (用你想要的任何扩展名替换 png,在你的 case.tiff 中)

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

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