Creating a new image from a portion of an existing image

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
tomshreds
Party member
Posts: 101
Joined: Thu Oct 18, 2012 8:49 pm

Creating a new image from a portion of an existing image

Post by tomshreds »

Hi!

I'm trying to create a new image object, from an existing image. I'd like to get only the face of my character's spritesheet and have it as an image object in the memory to use where I want to.

The reason I can't use quads and drawq to achieve so is that I use LöveFrames for my game's UI and it only accept image objects or path.

Any ideas?

Thanks!
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: Creating a new image from a portion of an existing image

Post by verilog »

Have you tried set/get Scissor?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Creating a new image from a portion of an existing image

Post by Santos »

You sure can, using ImageData and the paste method.

Code: Select all

function love.load()
	source = love.image.newImageData('image.png')

	width = 100
	height = 200
	target = love.image.newImageData(width, height)

	dx = 0   -- Destination x
	dy = 0   -- Destination y
	sx = 100 -- Source x
	sy = 0   -- Source y
	sw = 100 -- Source width
	sh = 200 -- Source height
	target:paste(source, dx, dy, sx, sy, sw, sh)

	image = love.graphics.newImage(target)
end

function love.draw()
	love.graphics.draw(image, 10, 10)
end
Attachments
imagedata_crop.love
(13.16 KiB) Downloaded 44 times
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Creating a new image from a portion of an existing image

Post by Ref »

Wow!
Had to give it a try (just some non-sense).
Thanks Santos.
Attachments
cropImage.love
Simple test demo
(340.33 KiB) Downloaded 48 times
tomshreds
Party member
Posts: 101
Joined: Thu Oct 18, 2012 8:49 pm

Re: Creating a new image from a portion of an existing image

Post by tomshreds »

Santos wrote:You sure can, using ImageData and the paste method.

Code: Select all

function love.load()
	source = love.image.newImageData('image.png')

	width = 100
	height = 200
	target = love.image.newImageData(width, height)

	dx = 0   -- Destination x
	dy = 0   -- Destination y
	sx = 100 -- Source x
	sy = 0   -- Source y
	sw = 100 -- Source width
	sh = 200 -- Source height
	target:paste(source, dx, dy, sx, sy, sw, sh)

	image = love.graphics.newImage(target)
end

function love.draw()
	love.graphics.draw(image, 10, 10)
end

wow thanks! it works! :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 183 guests