Export as Image/Write to .txt

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
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Export as Image/Write to .txt

Post by Zilarrezko »

Hello! back again to Love 2d after my desperate struggle with OOP in lua.

I'd like to start with making tools to make game development easier for me and my group cause that's awesome. And I got an idea for a tasty jam animation maker/tile set maker. Yet I'm having some trouble of course because it's programming and I'm no kikito.

One problem I'm trying to work around is somehow exporting an image from a 2D array or similar to the like. Looked around the wiki for a couple days, and couldn't find much of anything that was what I was looking for. Any ideas or pointers to a function would be great!

My second problem out of my 99 is that I've been having trouble with writing text to a .txt. I set up a nice little...

Code: Select all

function love.keypressed(key, uni)
     if key == "f1" then
          love.filesystem.write("data.txt","Heyo!\n")
     end
end
I press f1 from 3 times to about 25 times and I only seem to get that one like to say "heyo!". I think claptrap would be happy this doesn't work. (excuse the reference)
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Export as Image/Write to .txt

Post by HugoBDesigner »

1. I'm not sure what you mean. At all. But here's something that might or might not be helpful: you can save images as .txt and load them later. Just make love.filesystem.write("imagedata.txt", tostring(love.filesystem.read("image.png"))). To load, you can just make love.graphics.newImage("imagedata.txt").

2. I'm also not sure what you want. Do you want it to save multiple files or to increase the text on the current file? In the first case, just make this:

Code: Select all

local filename = "data.txt"
local n = 1
local can = true
if love.filesystem.exists(filename) then
	while can do
		n = n+1
		if not love.filesystem.exists("data" .. n .. ".txt") then
			filename = "data"..n..".txt"
			can = false
		end
	end
end
love.filesystem.write(filename, "Heyo!\n")
In the second case (I believe it's your problem), make this:

Code: Select all

local text = "heyo!\n"
if love.filesystem.exists("data.txt") then
	text = love.filesystem.read("data.txt") .. text
end
love.filesystem.write("data.txt", text)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Export as Image/Write to .txt

Post by bartbes »

The latter case can also be handled by using [wiki]love.filesystem.append[/wiki].
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Export as Image/Write to .txt

Post by Zilarrezko »

Hmm, yes the append is what I was looking for! and the other way seems to work too. yet I'm having trouble with getting a for loop to write the iterator on a new line each time. so that was it says in notepad

Code: Select all

1
2
3
4
5
6
7
instead of

Code: Select all

1234567
And as for the explanation of the first question. All I got to say is how interesting that works, yet entirely not what I'm looking for haha. I'm looking to save something as a png. Let's say I'm making a tileset in game. and I want to save the map as a png, how would I do that? On another note, how would I add another image onto the same png so I can create another tileset yet attach it to the other tileset.png into one png image file.

Thanks for your guys' help so far!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Export as Image/Write to .txt

Post by Robin »

You can open the file in a better text editor, like Notepad++ or Wordpad.

Alternatively, separate each line with "\r\n".
Help us help you: attach a .love.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Export as Image/Write to .txt

Post by Zilarrezko »

Works! Thank ya omniscient being! and I do already use notepad++. However I still have the first problem.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Export as Image/Write to .txt

Post by HugoBDesigner »

Wait, do you wanna it to be at the same line instead of one in each line? Just remove "\r\n". It should put the text always in the same line ;)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Export as Image/Write to .txt

Post by Zilarrezko »

uh I solved that problem, btw congrates on party member status.

I'm done with the adding text to a file. Now I want to make an image in love2D and save it as a png or jpg.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Export as Image/Write to .txt

Post by HugoBDesigner »

Zilarrezko wrote:uh I solved that problem, btw congrates on party member status.

I'm done with the adding text to a file. Now I want to make an image in love2D and save it as a png or jpg.
Thanks! Didn't realize 'til I read your post :P

Making images in LÖVE are a bit less easy (but not hard, though). You can either take screenshots or make blank images and add things to them later. With screenshots:

Code: Select all

local screenshot = love.graphics.newScreenshot()
screenshot:encode("image.png")
And with the blank image:

Code: Select all

local ImageData = love.image.newImageData(width, height)
Then set their pixels by using [wiki]ImageData:setPixel[/wiki]. After it, do this:

Code: Select all

ImageData:encode("image.png")
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 94 guests