123456789101112131415161718192021 |
- import sys
- from PIL import Image
- import os
- def convert_pgm_to_png(pgm_file, png_file):
- # 打开 PGM 文件
- with Image.open(pgm_file) as img:
- # 将图片转换为 PNG 格式并保存
- img.save(png_file, "PNG")
- if __name__ == '__main__':
- pgm_file = sys.argv[1] # 输入 PGM 文件路径
- png_file = sys.argv[2] # 输出 PNG 文件路径' # 输出 PNG 文件路径
- # 检查文件是否存在
- if os.path.exists(pgm_file):
- convert_pgm_to_png(pgm_file, png_file)
- print(f"转换完成:{pgm_file} -> {png_file}")
- else:
- print(f"文件 {pgm_file} 不存在。")
|