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

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:

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

Post 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?
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

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

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

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

Post by molul »

Worked like a charm. Thank you very much! :D
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

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

Post by Ref »

That's the easy part!
How do I save the image to the original directory after modifying?
User avatar
HDPLocust
Citizen
Posts: 65
Joined: Thu Feb 19, 2015 10:56 pm
Location: Swamp
Contact:

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

Post 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.
Science and violence
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

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

Post 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.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

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

Post by grump »

Code: Select all

data = image:encode("png"):getString()
file:write(data)
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

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

Post by Ref »

Thanks grump.
Assume you mean:

Code: Select all

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

Who is online

Users browsing this forum: Bing [Bot] and 46 guests