[SOLVED] Loading image from absolute directory using IO.Open

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
Forzi
Prole
Posts: 3
Joined: Fri Oct 14, 2016 8:18 pm

[SOLVED] Loading image from absolute directory using IO.Open

Post by Forzi »

Hey, I'm trying to load an image into my level editor from an absolute file path. Right now i have it working with a relative spritepath but I need to have two identical "Graphics" folders, one for the game and one for the editor. (I don't want one of them under the folder of the other)

This is my code to load the file that I found online and tried to use for my project:

function LoadSprite (aSpritePath, aObjectName)
local file = assert(io.open(aSpritePath, "r"))
local filedata, err = love.filesystem.newFileData(file:read("*all"), aObjectName .. ".png")
file:close()
local image

if (err == nil) then
local imageData = love.image.newImageData(filedata)
image = love.graphics.newImage(imageData)
end
return image
end

I get this error: Could not decode file '<myImageName>.png' to ImageData: unsupported file format.

I've tried to search online, but I can't really find much about this.

Would be awesome if anyone could help me :awesome:
Last edited by Forzi on Fri Oct 14, 2016 10:54 pm, edited 1 time in total.
User avatar
slime
Solid Snayke
Posts: 3163
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Loading image from absolute directory using IO.Open

Post by slime »

Try opening the file in binary mode: io.open("rb") instead of io.open("r").
Forzi
Prole
Posts: 3
Joined: Fri Oct 14, 2016 8:18 pm

Re: Loading image from absolute directory using IO.Open

Post by Forzi »

Omg that works! Thank you so much @slime! Funny that only one extra letter made it work ^^ #programminglife
Forzi
Prole
Posts: 3
Joined: Fri Oct 14, 2016 8:18 pm

Re: Loading image from absolute directory using IO.Open

Post by Forzi »

Here is the working code if anyone else wanna load pictures outside of your project folder and with absolute paths!

function LoadSprite (aSpritePath, aObjectName)
local file = assert(io.open(aSpritePath, "rb"))
local filedata, err = love.filesystem.newFileData(file:read("*all"), aObjectName .. ".png")
local image = love.graphics.newImage("/Graphics/Objects/Pickupable/Floor2_Bedroom_MrTiny.png")
file:close()
if (err == nil) then
local imageData = love.image.newImageData(filedata)
image = love.graphics.newImage(imageData)
end
return image
end

--- > Use love.filesystem.getSource() to get the file path to your project!
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 5 guests