GIMP Python插件:如何在导出后撤销图像缩放操作
问题描述
我写了一个GIMP插件,用来把图像导出成不同尺寸,作为React Native Android项目的图标,代码如下:
from gimpfu import * import os def execScale(image, drawable, toSave, isRound): addround = "" if(isRound): addround = "_round" scale_and_export(image, drawable, 192, toSave + "\\mipmap-xxxhdpi\\ic_launcher" + addround + ".png") scale_and_export(image, drawable, 144, toSave + "\\mipmap-xxhdpi\\ic_launcher" + addround + ".png") scale_and_export(image, drawable, 96, toSave + "\\mipmap-xhdpi\\ic_launcher" + addround + ".png") scale_and_export(image, drawable, 72, toSave + "\\mipmap-hdpi\\ic_launcher" + addround + ".png") scale_and_export(image, drawable, 48, toSave + "\\mipmap-mdpi\\ic_launcher" + addround + ".png") pdb.gimp_image_scale(image, 1024, 1024) return def scale_and_export(image, drawable, size, filename): try: os.makedirs(os.path.dirname(filename)) except OSError: print("Path exists") pdb.gimp_image_scale(image, size, size) pdb.gimp_file_save(image, drawable, filename, "?") register( "scale", "Scale image to be usable in a react-native android project as an icon", "Scale image to be usable in a react-native android project as an icon", "JaRoMaster", "JaRoMaster", "2022", "<Image>/CustomPlugins/Scale", "RGB*, GRAY*", [ (PF_DIRNAME, "toSave", "export", 0), (PF_BOOL, "isRound", "Round: ", False) ], [], execScale) main()
现在的问题是:每次缩放后图像都会越来越模糊,最后导出的48×48图像,比直接从初始尺寸缩放过来的模糊很多。想问有没有办法在导出后撤销缩放操作?
解决方案
问题出在你每次都是直接修改原图像,后续缩放是基于已经缩小过的图像进行的,自然会累积模糊。正确的做法是每次都基于原始图像的副本进行缩放导出,这样所有尺寸都从原图生成,清晰度一致,而且原图像完全不受影响,根本不需要撤销缩放。
修改后的代码如下:
from gimpfu import * import os def execScale(image, drawable, toSave, isRound): addround = "_round" if isRound else "" # 定义所有需要导出的尺寸和对应目录 sizes = [ (192, "mipmap-xxxhdpi"), (144, "mipmap-xxhdpi"), (96, "mipmap-xhdpi"), (72, "mipmap-hdpi"), (48, "mipmap-mdpi") ] for size, dir_name in sizes: # 创建原图的副本,基于副本操作,不改动原图 temp_image = pdb.gimp_image_duplicate(image) temp_drawable = pdb.gimp_image_get_active_drawable(temp_image) # 拼接导出路径 export_path = os.path.join(toSave, dir_name, f"ic_launcher{addround}.png") scale_and_export(temp_image, temp_drawable, size, export_path) # 关闭副本,释放资源 pdb.gimp_image_delete(temp_image) return def scale_and_export(image, drawable, size, filename): try: # 自动处理目录已存在的情况,简化逻辑 os.makedirs(os.path.dirname(filename), exist_ok=True) except OSError as e: print(f"创建目录出错: {e}") pdb.gimp_image_scale(image, size, size) # 用文件名作为第四个参数,符合GIMP API规范 pdb.gimp_file_save(image, drawable, filename, filename) register( "scale", "Scale image to be usable in a react-native android project as an icon", "Scale image to be usable in a react-native android project as an icon", "JaRoMaster", "JaRoMaster", "2022", "<Image>/CustomPlugins/Scale", "RGB*, GRAY*", [ (PF_DIRNAME, "toSave", "导出目录", 0), (PF_BOOL, "isRound", "是否为圆形图标", False) ], [], execScale) main()
修改核心点
- 用
pdb.gimp_image_duplicate(image)生成原图副本,所有缩放导出操作都在副本上进行,原图像保持初始状态,无需后续还原。 - 所有尺寸均基于原始图像生成,彻底解决多次缩放累积的模糊问题。
- 用
os.path.join拼接路径,比硬编码反斜杠更适配不同系统。 - 优化目录创建逻辑,
exist_ok=True避免重复创建目录时的报错。 - 调整
gimp_file_save的参数,符合GIMP官方API的规范用法。
内容的提问来源于stack exchange,提问作者JaRoMaster
相关产品推荐
相关产品推荐

