import os
from PIL import Image
dir = "G:\\dev\\cs-rkbm\\rskzc2025\\yuan2\\新建文件夹0727\\新建文件夹"
dir = "G:\\dev\\cs-rkbm\\Rsbm_rpt\\zp"
dir = "G:\\dev\\cs-rkbm\\zperr"
for f in os.listdir(dir):
if os.path.isfile(os.path.join(dir,f)):
img = Image.open(os.path.join(dir,f))
if img.format != "JPEG":
print( f + " " + img.format)
if img.mode != "RGB":
print( f + " not RGB " + img.mode )
def png_to_jpg(input_path, output_path, quality=95):
"""
将PNG图片转换为JPG格式
参数:
input_path (str): 输入的PNG文件路径
output_path (str): 输出的JPG文件路径
quality (int): JPG质量,1-100,默认95
"""
try:
# 打开PNG图像
image = Image.open(input_path)
# 如果图像有alpha通道(透明度),创建一个白色背景并合并
if image.mode in ('RGBA', 'LA'):
background = Image.new('RGB', image.size, (255, 255, 255))
background.paste(image, mask=image.split()[-1]) # 使用alpha通道作为掩码
image = background
# 保存为JPG
image.save(output_path, 'JPEG', quality=quality)
print(f"转换成功: {input_path} -> {output_path}")
except Exception as e:
print(f"转换失败: {e}")
def jpgcymk_to_jpg(input_path, output_path, quality=95):
"""
将PNG图片转换为JPG格式
参数:
input_path (str): 输入的PNG文件路径
output_path (str): 输出的JPG文件路径
quality (int): JPG质量,1-100,默认95
"""
try:
# 打开PNG图像
image = Image.open(input_path)
# 如果图像有alpha通道(透明度),创建一个白色背景并合并
if image.mode in ('CMYK'):
# 保存为JPG
image.save(output_path, 'JPEG', quality=quality)
print(f"转换成功: {input_path} -> {output_path}")
except Exception as e:
print(f"转换失败: {e}")
def bmp_to_jpg(input_file, output_file):
# 打开BMP文件
with Image.open(input_file) as im:
# 将BMP文件转换为RGB模式
rgb_im = im.convert('RGB')
# 将RGB模式的图像保存为JPEG文件
rgb_im.save(output_file, 'JPEG')