This repository has been archived on 2025-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
2025-03-18 11:35:10 +08:00

26 lines
604 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PIL import Image
def change_image_dpi(input_path, output_path, new_dpi):
# 打开图像
image = Image.open(input_path)
# 获取当前的dpi值如果有
dpi = image.info.get('dpi', (72, 72))
print(f"当前DPI: {dpi}")
# 保存图像并设置新的dpi值
image.save(output_path, dpi=(new_dpi, new_dpi))
print(f"DPI已更改为: {new_dpi}")
# 输入图像路径
input_image_path = "image.jpg"
# 输出图像路径
output_image_path = "image.jpg"
# 目标DPI
new_dpi = 96
# 调用函数
change_image_dpi(input_image_path, output_image_path, new_dpi)