简体   繁体   中英

Gimp python: how to undo scaling

I wrote a GIMP plugin to save my image in different sizes:

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

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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