save image to a txt file?

Blitz3D Forums/Blitz3D Programming/save image to a txt file?

ckob(Posted 2004) [#1]
how do you do it? ive tried all sorts of stuff like saving pixel info but i cant seem to get it to work any idea?


Bot Builder(Posted 2004) [#2]
erm 0_0

Untested
fil=WriteFile("image.txt")

WriteLine fil,ImageWidth(img)
WriteLine fil,ImageHeight(img)
SetBuffer ImageBuffer(img)
For x=1 to ImageWidth(img)
 For y=1 to ImageHeight(img)
  WriteLine fil,ReadPixel(x,y)
 Next
Next

fil=ReadFile("image.txt")

w=ReadLine$(fil)
h=ReadLine$(fil)
img=CreateImage(w,h)
For x=1 to w
 For y=1 to h
  WritePixel x,y,Readline$(fil)
 Next
Next


Not sure why you would want to do this though. its more inefficient thatn a bmp :D


jfk EO-11110(Posted 2004) [#3]
I guess you want to include it as a Data Code Snippet? I'd use hex values, they don't use too much space. But still, if it's stored in Datas, it will be held in Memory twice.

Or what do you need it for?


ckob(Posted 2004) [#4]
nope i just wanna save the pixel info to a file :P the above code is what ive already tried and it didnt work :(


JazzieB(Posted 2004) [#5]
OK, I've tried the code below, which isn't too different from that above and it works.

Graphics 400,300,0,2

old_image=CreateImage(64,64)
SetBuffer ImageBuffer(old_image)
Color 255,0,0:		Rect 0,0,64,64
Color 255,255,255:	Text 32,32,"TEST",True,True

; save image
file=WriteFile("test.txt")
WriteLine file,ImageWidth(old_image)
WriteLine file,ImageHeight(old_image)
For y=0 To ImageHeight(old_image)-1
	For x=0 To ImageWidth(old_image)-1
		WriteLine file,ReadPixel(x,y)
	Next
Next
CloseFile file

; load new image
file=ReadFile("test.txt")
w=ReadLine(file)
h=ReadLine(file)
new_image=CreateImage(w,h)
SetBuffer ImageBuffer(new_image)
For y=0 To h-1
	For x=0 To w-1
		WritePixel x,y,ReadLine(file)
	Next
Next
CloseFile file

; show image as test
SetBuffer FrontBuffer()
DrawImage new_image,200-w/2,150-h/2
WaitKey:End


One difference is that the two loops that loop through the pixel data should start from 0 and finish from 1 less than the height or width of the image, since co-ordinates start at 0 and not 1.

Another is that Bot Builder's code doesn't close the files after writing or reading. If you've not been doing this then this could be causing the problem, as it thinks the file is still open for writing when it comes to reading it.

However, if you don't want the pixel info in a readable form then I would make use of WriteInt() and ReadInt() instead, as they would produce smaller files.


ckob(Posted 2004) [#6]
weird I tried this exact same thing before and it never worked.. i must of missed something when I did it