我似乎无法在调整大小后保留图像中的额外信息。

huangapple go评论58阅读模式
英文:

I can't seem to keep the extra info in the images after resizing them

问题

我正在尝试通过Python脚本将图像调整为宽度760像素和高度580像素的分辨率。但是,调整大小后的图像在“图像属性” >> “详细信息”选项卡中不包含它们的原始元数据。图像调整大小后,所有元数据都丢失了。我能够保留原始dpi并在调整大小时保持原始宽高比。然而,我不知道如何保留元数据。请帮忙!

我正在使用PIL库。上面的函数能够保留图像调整大小后的原始dpi和宽高比,还可以将文件名从大写更改为小写,但是不会保留调整大小后的图像中的元数据。

英文:

I am trying to resize images to a resolution of width 760 pixels and height 580 pixels through a python script. But, the resized images do not contain their original metadata in Image properties >> details tab. All the metadata is lost after the image is resized. I was able to retain the original dpi and preserve the original aspect ratio while resizing. However, I am lost at preserving the metadata. Please help !

def resize_images(input_folder, output_folder, width, height):
    # output folder
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    
    for filename in os.listdir(input_folder):
        if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, filename)
            with Image.open(input_path) as image:
                # Calculate the aspect ratio
                aspect_ratio = image.width / float(image.height)
                new_height = int(width / aspect_ratio)
                resized_image = image.resize((width, new_height), Image.LANCZOS)
                dpi = image.info.get("dpi", (300, 300))  # Default DPI: 300
                resized_image.save(output_path, dpi=dpi)

I am using PIL library. The above function was able to retain original dpi and aspect ratio of image after resized and it can also change the case of file names from upper to lower case. however, it doesn't retain the metadata in resized images.

答案1

得分: 1

尝试:

resized_image.save(output_path, dpi=dpi, exif=image.getexif())
英文:

Try:

resized_image.save(output_path, dpi=dpi, exif=image.getexif())

huangapple
  • 本文由 发表于 2023年6月15日 18:10:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定