在python中将tiff转换为jpeg [英] converting tiff to jpeg in python

查看:1170
本文介绍了在python中将tiff转换为jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我阅读.tiff图像并将其转换为jpeg格式吗?

Can anyone help me to read .tiff image and convert into jpeg format?

from PIL import Image
im = Image.open('test.tiff')
im.save('test.jpeg')

上面的代码不起作用.

推荐答案

我已成功解决了该问题.我发布了代码,以读取文件夹中的tiff文件并自动将其转换为jpeg.

I have successfully solved the issue. I posted the code to read the tiff files in a folder and convert into jpeg automatically.

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() == ".tiff":
            if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + ".jpg"):
                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] + ".jpg"
                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, e:
                    print e

这篇关于在python中将tiff转换为jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆