43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
|
import os
|
|||
|
from PIL import Image
|
|||
|
|
|||
|
|
|||
|
def transfer(input_path, quality=20, resize_factor=0.1):
|
|||
|
# 打开TIFF图像
|
|||
|
# img = Image.open(input_path)
|
|||
|
#
|
|||
|
# # 保存为JPEG,并设置压缩质量
|
|||
|
# img.save(output_path, 'JPEG', quality=quality)
|
|||
|
|
|||
|
# input_path = os.path.join(input_folder, filename)
|
|||
|
# 获取input_path的文件名
|
|||
|
|
|||
|
# 使用os.path.splitext获取文件名和后缀的元组
|
|||
|
# 使用os.path.basename获取文件名(包含后缀)
|
|||
|
filename_with_extension = os.path.basename(input_path)
|
|||
|
filename, file_extension = os.path.splitext(filename_with_extension)
|
|||
|
|
|||
|
# 使用os.path.dirname获取文件所在的目录路径
|
|||
|
output_folder = os.path.dirname(input_path)
|
|||
|
|
|||
|
output_path = os.path.join(output_folder, filename + '.jpg')
|
|||
|
|
|||
|
img = Image.open(input_path)
|
|||
|
|
|||
|
# 将图像缩小到原来的一半
|
|||
|
new_width = int(img.width * resize_factor)
|
|||
|
new_height = int(img.height * resize_factor)
|
|||
|
resized_img = img.resize((new_width, new_height))
|
|||
|
|
|||
|
# 保存为JPEG,并设置压缩质量
|
|||
|
# 转换为RGB模式,丢弃透明通道
|
|||
|
rgb_img = resized_img.convert('RGB')
|
|||
|
|
|||
|
# 保存为JPEG,并设置压缩质量
|
|||
|
# 压缩
|
|||
|
rgb_img.save(output_path, 'JPEG', quality=quality)
|
|||
|
|
|||
|
print(f'{output_path} 转换完成')
|
|||
|
|
|||
|
return output_path
|