简体   繁体   English

Gimp python:如何撤消缩放

[英]Gimp python: how to undo scaling

I wrote a GIMP plugin to save my image in different sizes:我写了一个 GIMP 插件来以不同的尺寸保存我的图像:

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()

Right now, the image gets more blurier after every scale.现在,图像在每次缩放后变得更加模糊。 (At the end, 48x48 is a lot more blurry than if i would have scaled it to this size from the initial size). (最后,48x48 比我将它从初始大小缩放到这个大小要模糊得多)。

Is there a way to undo the scaling after exporting?有没有办法在导出后撤消缩放?

Not using the right technique.没有使用正确的技术。 Remember that the file-save operation applies to the layer, not to the image as a whole so you can:请记住,文件保存操作适用于图层,而不是整个图像,因此您可以:

  • duplicate your layer,复制你的图层,
  • scale the layer,缩放图层,
  • save the scaled layer保存缩放图层
  • discard the saved layer丢弃保存的图层

From the ofn-export-sizes script :ofn-export-sizes脚本

saveLayer=pdb.gimp_layer_new_from_visible(image,image, saveName)
image.add_layer(saveLayer,0)
saveLayer.scale(w,h,False)
pdb.gimp_file_save(image,saveLayer,filename,filename)
image.remove_layer(saveLayer)

Note that the code does a new-from-visible to generate the scaled layer instead of a plain copy,because this works even if the image has several layers.请注意,代码执行new-from-visible以生成缩放图层而不是普通副本,因为即使图像有多个图层,这也有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM