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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

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

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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.
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

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

Post 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! :)
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

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

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

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

Post 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 :)
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests