Page 1 of 1

Can I save an image loaded with love.graphics.newImage() as png?

Posted: Sat Aug 04, 2018 8:11 am
by molul
I'd to be able to resize and save (overwrite) an image if it's bigger than what I expect. The workflow would be:

-Load the image with love.graphics.newImage.
-Get the image dimensions.
-If it's bigger than 230x320, resize it to 230x320 (I guess I can only render it to a Canvas with the right size, scaling the image accordingly).
-Save the resized image with the same name.

Would that be possible on LÖVE 11.1?

Re: Can I save an image loaded with love.graphics.newImage() as png?

Posted: Sat Aug 04, 2018 8:24 am
by bartbes
Yes, but not easily. I think you have to draw the Image to a Canvas, then get ImageData from that. Then you can use the ImageData:encode function to save it.

Re: Can I save an image loaded with love.graphics.newImage() as png?

Posted: Sat Aug 04, 2018 8:42 am
by molul
Oh, so encode does write the imagedata! Cool. I'll try that. Thanks a lot!

EDIT: it worked like a charm. Thanks again! :)

Re: Can I save an image loaded with love.graphics.newImage() as png?

Posted: Sun Aug 05, 2018 7:46 pm
by raidho36
I don't think you should be overwriting any files (other than config.ini and such). If you want to do this, make a unique folder for them. It's normally impossible to overwrite game files as all the file write operations occur in appdata folder instead. But if you have your game files in that folder, then that could be a problem.

Anyway, what you should do is simply create a new Image from downscaled canvas and discard the original image and the canvas, to save memory. No need to actually save the resulting files.

Re: Can I save an image loaded with love.graphics.newImage() as png?

Posted: Sun Aug 05, 2018 8:04 pm
by molul
Well, I overwrite them so next time load time is lower. The images are stored in the app directory, but as the app is running on a Linux system, I can just call os.execute("mv...") to put it where I need (a directory in the SD card).

On the other hand, now you mention, I can't seem to discard the original file and that's a big problem, because the system doesn't have much texture memory, and loading a couple of 7mb images crashes the system.

I tried setting the variable to nil and calling collectgarbage immediately after as I read on another thread, but I'm printing texture memory value from love.system.getstats() Everytime I load an image and I see it hasn't been unloaded. I might have to drop this feature, although I'm currently discarding any image bigger than 300kb.

Is there any other way to unload images from memory? A love.graphics.unloadImage function would be a really handy feature, I think.

Re: Can I save an image loaded with love.graphics.newImage() as png?

Posted: Mon Aug 06, 2018 11:43 pm
by raidho36
There is Object:release function which immediately frees up system-allocated memory. Setting the reference to nil would only work if there are no other references to it anywhere, and collectgarbage may not actually collect it on the first pass.

Re: Can I save an image loaded with love.graphics.newImage() as png?

Posted: Tue Aug 07, 2018 7:34 am
by molul
Thanks! Unfortunately, that doesn't seem to unload the image from memory :(

I'm dropping this feature, and will only allow images below 200KB instead, without resizing and overwriting them. I've also managed to get the maximum texture memory I can use before the system crashes. Not ideal, but good enough :)