1 Star 0 Fork 0

johnvon/little_tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
compressor.py 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
johnvon 提交于 2023-10-19 04:27 . initial repo
import os
from PIL import Image
def compress_bmp(input_path,output_path,quality=50):
with Image.open(input_path) as img:
img.save(output_path,"JPEG",quality=quality)
def compressor(input_folder,output_folder,quality):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
path_list = os.listdir(input_folder)
#path_list.remove('bmp')
path_list.sort(key=lambda x:int(x.split('.')[0]))
for filename in path_list:
if filename.endswith("bmp"):
input_path = os.path.join(input_folder, filename)
output_path=os.path.join(output_folder,filename.replace('bmp','jpg'))
compress_bmp(input_path,output_path,quality)
def convert2pdf(output_folder,pdf_name):
img_open_list = []
path_list = os.listdir(output_folder)
path_list.sort(key=lambda x:int(x.split('.')[0]))
for filename in path_list:
file_path = os.path.join(output_folder,filename)
img_open=Image.open(file_path)
if img_open.mode!="RGB":
img_open = img_open.convert("RGB")
img_open_list.append(img_open)
img_1 = img_open_list[0]
img_open_list = img_open_list[1:]
img_1.save(output_folder+'/'+pdf_name,"PDF",resolution=100,save_all=True,append_images=img_open_list)
print("转换成功!pdf文件在输出目录下")
if __name__=='__main__':
input_folder = r"E:/buffer"
output_folder = r"E:/before"
pdf_name = "tmp1.pdf"
quality = 100
#compressor(input_folder,output_folder,quality)
convert2pdf(output_folder,pdf_name)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/johnvonlzf/little_tools.git
[email protected]:johnvonlzf/little_tools.git
johnvonlzf
little_tools
little_tools
master

搜索帮助