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: 232
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain

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

Post by molul » Tue Jun 12, 2018 5:41 am

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
Prole
Posts: 28
Joined: Mon Sep 12, 2016 7:52 am

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

Post by Nelvin » Tue Jun 12, 2018 6:00 am

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: 232
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain

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

Post by molul » Tue Jun 12, 2018 6:48 am

Worked like a charm. Thank you very much! :D

User avatar
Ref
Party member
Posts: 669
Joined: Wed May 02, 2012 11:05 pm

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

Post by Ref » Tue Jun 12, 2018 8:07 pm

That's the easy part!
How do I save the image to the original directory after modifying?

User avatar
HDPLocust
Prole
Posts: 33
Joined: Thu Feb 19, 2015 10:56 pm
Location: CENTER OF THE RED LANDS
Contact:

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

Post by HDPLocust » Tue Jun 12, 2018 9:41 pm

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.
Мой английский не очень хорош, заранее прошу простить. Любви каждому.
If you see it, you may will sheet bricks :3

User avatar
Ref
Party member
Posts: 669
Joined: Wed May 02, 2012 11:05 pm

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

Post by Ref » Tue Jun 12, 2018 11:53 pm

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: 354
Joined: Sat Jul 22, 2017 7:43 pm

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

Post by grump » Wed Jun 13, 2018 6:48 am

Code: Select all

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

User avatar
Ref
Party member
Posts: 669
Joined: Wed May 02, 2012 11:05 pm

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

Post by Ref » Wed Jun 13, 2018 2:09 pm

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: No registered users and 8 guests