import os from PIL import Image

def png_to_jpg(input_path, output_path): if not os.path.exists(input_path): print("Error: input path does not exist!") return

if not os.path.exists(os.path.dirname(output_path)):
    os.makedirs(os.path.dirname(output_path))

try:
    image = Image.open(input_path)

    if image.mode == "RGBA":
        # Convert RGBA image to RGB mode
        image = image.convert("RGB")

    image.save(output_path, "JPEG")
    print(f"{input_path} has been converted to {output_path}")
except Exception as e:
    print(f"Error: {e}")

if name == "main": input_path = "/path/to/input/image.png" output_path = "/path/to/output/image.jpg" png_to_jpg(input_path, output_path)

帮我写一个Python 功能是将PNG格式图片转为JPG

原文地址: https://www.cveoy.top/t/topic/Z7t 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录