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