Difference between revisions of "ImageData:encode"

(Examples)
Line 49: Line 49:
 
== Examples ==
 
== Examples ==
 
=== Draw a circle in a canvas, save it in file and display it on screen ===
 
=== Draw a circle in a canvas, save it in file and display it on screen ===
function love.load()
+
  function love.load()
  canvas = love.graphics.newCanvas(800,600)
+
    canvas = love.graphics.newCanvas(800,600)
  love.graphics.setCanvas(canvas)
+
    love.graphics.setCanvas(canvas)
  love.graphics.circle("fill",200,200,100)
+
    love.graphics.circle("fill",200,200,100)
 
   -- need to set another Canvas to avoid Canvas:newImageData cannot be called while that Canvas is currently active.
 
   -- need to set another Canvas to avoid Canvas:newImageData cannot be called while that Canvas is currently active.
  love.graphics.setCanvas()
+
    love.graphics.setCanvas()
  imagedata = canvas:newImageData()
+
    imagedata = canvas:newImageData()
  imagedata:encode("tga", "test.png")
+
    imagedata:encode("tga", "test.png")
end
+
  end
 
+
 
function love.draw()
+
  function love.draw()
  love.graphics.draw(canvas)
+
    love.graphics.draw(canvas)
end
+
  end
  
 
== User Notes ==
 
== User Notes ==

Revision as of 19:59, 26 April 2022

Encodes the ImageData to a file format and optionally writes it to the save directory.

Function

Available since LÖVE 0.10.0
This variant is not supported in earlier versions.

Synopsis

filedata = ImageData:encode( format, filename )

Arguments

ImageEncodeFormat format
The format to encode the image as.
string filename (nil)
The filename to write the file to. If nil, no file will be written but the FileData will still be returned.

Returns

FileData filedata
The encoded image as a new FileData object.

Function

Available since LÖVE 0.8.0 and removed in LÖVE 0.10.0
This variant is not supported in earlier or later versions.

Synopsis

ImageData:encode( outFile )

Arguments

string outFile
Name of a file to write encoded data to. The format will be automatically deduced from the file extension.

Returns

Nothing.

Function

Available since LÖVE 0.8.0 and removed in LÖVE 0.10.0
This variant is not supported in earlier or later versions.

Synopsis

ImageData:encode( outFile, format )

Arguments

string outFile
Name of a file to write encoded data to.
ImageEncodeFormat format
The format to encode the image in.

Returns

Nothing.

Function

Removed in LÖVE 0.8.0
This variant is not supported in that and later versions.

Synopsis

data = ImageData:encode( format )

Arguments

ImageEncodeFormat format
The format to encode the image in.

Returns

Data data
The encoded image data.

Examples

Draw a circle in a canvas, save it in file and display it on screen

 function love.load()
   canvas = love.graphics.newCanvas(800,600)
   love.graphics.setCanvas(canvas)
   love.graphics.circle("fill",200,200,100)
 -- need to set another Canvas to avoid Canvas:newImageData cannot be called while that Canvas is currently active.
   love.graphics.setCanvas()
   imagedata = canvas:newImageData()
   imagedata:encode("tga", "test.png")
 end
 
 function love.draw()
   love.graphics.draw(canvas)
 end

User Notes

  • If you want the file to have an extension when saved add it in the file name. Example :
    image:encode("png","aPngImage.png")
    

See Also


Other Languages