Page 1 of 1

Using love.graphics.newImage() with png files in an external directory

Posted: Tue Jun 12, 2018 5:41 am
by molul
I'm making an UI for an application (a launcher for a few utilities) and I need to show a few images on screen, which are located on a different path than my löve project. There's no option to include them in the project directory, at least for now. Each part of the project is located on different partitions in a Rockchip board.

These would be the paths:

-Love project: /ui
-Png files: /assets/png

I have this variable:
local externalPngDir = /assets/png/

As expected, when I use love.graphics.newImage(externalPngDir .. externalPngFile), I get an error.

Would there be a way for this to work or should I ask my teammates to change the project directories structure?

Re: Using love.graphics.newImage() with png files in an external directory

Posted: Tue Jun 12, 2018 6:00 am
by Nelvin
I use a helper function like this in such situations

Code: Select all

local function loadImageFromPath( filePath )
    local f = io.open( filePath, "rb" )
    if f then
        local data = f:read( "*all" )
        f:close()
        if data then
            data = love.filesystem.newFileData( data, "tempname" )
            data = love.image.newImageData( data )
            local image = love.graphics.newImage( data )
            return image
        end
    end
end

Re: Using love.graphics.newImage() with png files in an external directory

Posted: Tue Jun 12, 2018 6:48 am
by molul
Worked like a charm. Thank you very much! :D

Re: Using love.graphics.newImage() with png files in an external directory

Posted: Tue Jun 12, 2018 8:07 pm
by Ref
That's the easy part!
How do I save the image to the original directory after modifying?

Re: Using love.graphics.newImage() with png files in an external directory

Posted: Tue Jun 12, 2018 9:41 pm
by HDPLocust
Ref wrote: Tue Jun 12, 2018 8:07 pm How do I save the image to the original directory after modifying?
Same as reading, but reverse order of function calls, or save file in lovegame dir and copy/move to destination path.

Re: Using love.graphics.newImage() with png files in an external directory

Posted: Tue Jun 12, 2018 11:53 pm
by Ref
Not if you want a jpg or png file.
You have to encode the data into the appropriate format.
There in lies the rub.
Love combines formatting and sending the image to the app folder - not to the directory of your choice.

Re: Using love.graphics.newImage() with png files in an external directory

Posted: Wed Jun 13, 2018 6:48 am
by grump

Code: Select all

data = image:encode("png"):getString()
file:write(data)

Re: Using love.graphics.newImage() with png files in an external directory

Posted: Wed Jun 13, 2018 2:09 pm
by Ref
Thanks grump.
Assume you mean:

Code: Select all

local data = imagedata:encode( "png" ):getString()
file:write( data )