我有一个扫描仪,当我扫描页面时,它会生成一个BMP文件,但每页的大小为50MB。我该如何告诉Python将其转换为JPEG并缩小尺寸。
rv = ss.XferImageNatively()
if rv:
(handle, count) = rv
twain.DIBToBMFile(handle,'imageName.bmp')
你应该如何告诉他把它变成JPEG或PDF?(原生传输总是无压缩图像,因此您的图像大小将为:(每英寸点数*高度英寸)*(每英寸点数*宽度英寸)*每像素字节数)
我有一个扫描仪,当我扫描页面时,它会生成一个BMP文件,但每页的大小为50MB。我该如何告诉Python将其转换为JPEG并缩小尺寸。
rv = ss.XferImageNatively()
if rv:
(handle, count) = rv
twain.DIBToBMFile(handle,'imageName.bmp')
你应该如何告诉他把它变成JPEG或PDF?(原生传输总是无压缩图像,因此您的图像大小将为:(每英寸点数*高度英寸)*(每英寸点数*宽度英寸)*每像素字节数)
编辑:请参见(懒惰模式)
from PIL import Image
img = Image.open('C:/Python27/image.bmp')
new_img = img.resize( (256, 256) )
new_img.save( 'C:/Python27/image.png', 'png')
输出:
from PIL import Image
import glob
ext = input('Input the original file extension: ')
new = input('Input the new file extension: ')
# Checks to see if a dot has been input with the images extensions.
# If not, it adds it for us:
if '.' not in ext.strip():
ext = '.'+ext.strip()
if '.' not in new.strip():
new = '.'+new.strip()
# Creates a list of all the files with the given extension in the current folder:
files = glob.glob('*'+ext)
# Converts the images:
for f in files:
im = Image.open(f)
im.save(f.replace(ext,new))