# Spritesheet/cutting an image

**URL:** <https://discourse.processing.org/t/spritesheet-cutting-an-image/3174>\
**Category:** Coding Questions\
**Created:** [September 1, 2018, 5:36pm UTC](https://discourse.processing.org/t/spritesheet-cutting-an-image/3174 "2018-09-01T17:36:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hedgy](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hedgy/32/1301_2.png) [@Hedgy](https://discourse.processing.org/u/Hedgy)\
**Post date:** [September 1, 2018, 5:36pm UTC](https://discourse.processing.org/t/spritesheet-cutting-an-image/3174/1 "2018-09-01T17:36:48Z")

</div>

I have a spritesheet for a character and i’d like to be able to split it up in processing. Any way possible?  
 ![character](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f2cee7eda113ffcabcc02cb17f8bc38a505e2e4e.png)

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [September 1, 2018, 5:41pm UTC](https://discourse.processing.org/t/spritesheet-cutting-an-image/3174/2 "2018-09-01T17:41:00Z")

</div>

Sure. You can use `get()` with four parameters to grab rectangular regions from it.

```auto
PImage sheet;

void setup(){
  size(600,400);
  sheet = loadImage("sprite_sheet.png");
}

void draw(){
  background(0);
  image(sheet.get(20, 20, 100, 100), 100, 100);
}
```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 1, 2018, 6:36pm UTC](https://discourse.processing.org/t/spritesheet-cutting-an-image/3174/3 "2018-09-01T18:36:46Z")

</div>

Hi Hedgy,

You can also use the [sprites labrary](http://www.lagers.org.uk/s4p/) from @quark.  
I think you can indicate the number of columns and rows in the constructor and the cuts will be done automatically.

---

<div class="post-metadata">

**Author:** ![Hedgy](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hedgy/32/1301_2.png) [@Hedgy](https://discourse.processing.org/u/Hedgy)\
**Post date:** [September 2, 2018, 4:35am UTC](https://discourse.processing.org/t/spritesheet-cutting-an-image/3174/4 "2018-09-02T04:35:50Z")

</div>

This works! Thanks so much.  
Heres the code if you feel like reading my sloppy stuff haha:

```auto
PImage characterSprite;
	PImage characterUp, characterDown, characterLeft, characterRight;
	boolean isCharacterUp, isCharacterDown, isCharacterLeft, isCharacterRight;
	String currentSprite;

	public static void main(String[] args) {
		PApplet.main("zeldaremake.ZeldaRemake");
	}
	
	public void settings() {
		size(768,640);
	}
	
	public void setup() {
		characterSprite = loadImage("character.png");
	}
	
	
	public void keyPressed() {
		switch(key) {
		case 'w':
			isCharacterUp = true;
			break;
		
		case 's':
			isCharacterDown = true;
			break;
		
		case 'a':
			isCharacterLeft = true;
			break;
			
		case 'd':
			isCharacterRight = true;
			break;
		}
	}
	
	public void keyReleased() {
		switch(key) {
		case 'w':
			isCharacterUp = false;
			break;
		
		case 's':
			isCharacterDown = false;
			break;
		
		case 'a':
			isCharacterLeft = false;
			break;
			
		case 'd':
			isCharacterRight = false;
			break;
		}
	}

	public void draw() {
		movement();
	}
	
	public void movement() {
		if(isCharacterUp == true) {
			image(characterSprite.get(0, 0, 16, 16), width/2, height/2);
		}
		if(isCharacterDown == true) {
			image(characterSprite.get(16, 0, 16, 16), width/2, height/2);
		}
		if(isCharacterLeft == true) {
			image(characterSprite.get(32, 0, 16, 16), width/2, height/2);
		}
		if(isCharacterRight == true) {
			image(characterSprite.get(48, 0, 16, 16), width/2, height/2);
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 3, 2018, 3:51pm UTC](https://discourse.processing.org/t/spritesheet-cutting-an-image/3174/5 "2018-09-03T15:51:58Z")

</div>

Thanks so much for sharing your solution, @Hedgy – this is really helpful for people who have similar problems later!
