Initial commit

This commit is contained in:
2026-03-09 17:23:53 +08:00
parent 17e0db880e
commit 1422fb5026
515 changed files with 124791 additions and 0 deletions

39
png2jpg.py Normal file
View File

@ -0,0 +1,39 @@
from PIL import Image
import os
def png_to_jpg(input_path, output_path=None, quality=95):
"""
将PNG图片转换为JPG格式
:param input_path: 输入PNG文件路径
:param output_path: 输出JPG文件路径可选默认同目录同名
:param quality: JPG保存质量1-100
"""
if not os.path.exists(input_path):
print(f"文件不存在: {input_path}")
return
# 自动生成输出路径
if output_path is None:
output_path = os.path.splitext(input_path)[0] + '.jpg'
# 打开图片转换为RGB去除alpha通道
with Image.open(input_path) as img:
# 如果图片有透明通道,用白色背景填充
if img.mode in ('RGBA', 'LA', 'P'):
# 创建白色背景图
rgb_img = Image.new('RGB', img.size, (255, 255, 255))
# 根据alpha通道合成
if img.mode == 'P':
img = img.convert('RGBA')
rgb_img.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
else:
rgb_img = img.convert('RGB')
# 保存为JPG
rgb_img.save(output_path, 'JPEG', quality=quality)
print(f"转换成功: {output_path}")
# 示例使用
png_to_jpg(r"D:\汇报\水库项目资料\gif\2026-03-09 104228.png")