Anb*_*lam 16 jpeg tiff python-imaging-library python-2.7
任何人都可以帮助我阅读.tiff图像并转换为jpeg格式?
from PIL import Image
im = Image.open('test.tiff')
im.save('test.jpeg')
Run Code Online (Sandbox Code Playgroud)
上面的代码无效.
Anb*_*lam 16
我已成功解决了这个问题.我发布了代码来读取文件夹中的tiff文件并自动转换为jpeg.
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
Run Code Online (Sandbox Code Playgroud)
小智 9
这个问题可以借助OpenCV来解决。这对我有用。
OpenCV 版本==4.3.0
import cv2, os
base_path = "data/images/"
new_path = "data/ims/"
for infile in os.listdir(base_path):
print ("file : " + infile)
read = cv2.imread(base_path + infile)
outfile = infile.split('.')[0] + '.jpg'
cv2.imwrite(new_path+outfile,read,[int(cv2.IMWRITE_JPEG_QUALITY), 200])
Run Code Online (Sandbox Code Playgroud)
import os, sys
from PIL import Image
Run Code Online (Sandbox Code Playgroud)
我试图直接保存为jpeg,但错误表明该模式为P模式且与JPEG格式不兼容,因此您必须按以下方式将其转换为RGB模式。
for infile in os.listdir("./"):
print "file : " + infile
if infile[-3:] == "tif" or infile[-3:] == "bmp" :
# print "is tif or bmp"
outfile = infile[:-3] + "jpeg"
im = Image.open(infile)
print "new filename : " + outfile
out = im.convert("RGB")
out.save(outfile, "JPEG", quality=90)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27558 次 |
最近记录: |