How to scale an exported image in a script?

Hello, I have been using sprite:saveAs() to save images in a script and I wanted to know if there’s a quick way to get the image to be scaled by 500% at saving time like when using the normal export function from the menu.

Thanks in advance.

Hi @Edgardo_Morales,

You have options, depending on file format and what data from the sprite you need to be retained.

local scale = 5
local sprite = app.activeSprite
local dupe = Sprite(sprite)
dupe:resize(
    sprite.width * scale,
    sprite.width * scale)
dupe:saveCopyAs("\path\to\test.webp")
dupe:close()

Test that this behaves as you want for a sprite with multiple frames when saving a .png vs. a .webp or .gif.

If you don’t want to create a duplicate sprite or change the sprite in-place (resizing again by 1/scale after the save), here’s another possibility:

local scale = 5
local blit = Image(app.activeSprite.spec)
blit:drawSprite(app.activeSprite, app.activeFrame, Point(0, 0))
blit:resize(blit.width * scale, blit.height * scale)
blit:saveAs {
    filename = "\path\to\test.png",
    palette = app.activeSprite.palettes[1] }

A key difference here is one frame per file only. The advantage over the script above is there’s less distraction created in the UI by the script.

The sprite resize method could be better documented. I’d have to test that function to be sure, but speaking generally: doing and undoing a raster image resize as I said above has potential to change the sprite’s data. See Resizing Images - Computerphile - YouTube . I wouldn’t let an export script change a sprite in-place. And were I forced to do so, I’d want extra validation for the scale and the interpolation method.

Best to you,
Jeremy

1 Like

Thanks! I waws hoping to have a quicker way to do it at export, but I can work with this.