# Cropping an image buffer on transparency?

**URL:** https://discourse.processing.org/t/cropping-an-image-buffer-on-transparency/35125
**Category:** Coding Questions
**Created:** [February 12, 2022, 3:01pm UTC](https://discourse.processing.org/t/cropping-an-image-buffer-on-transparency/35125 "2022-02-12T15:01:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![xeronimo](https://avatars.discourse-cdn.com/v4/letter/x/8491ac/32.png) [@xeronimo](https://discourse.processing.org/u/xeronimo)
#### Post date: [February 12, 2022, 3:01pm UTC](https://discourse.processing.org/t/cropping-an-image-buffer-on-transparency/35125/1 "2022-02-12T15:01:56Z")

</div>

I’d like to crop/copy an image buffer in #p5js so that only the non-transparent part is kept/copied … any ideas how to do that (quickly)!? Thanks!

---

<div class="post-metadata">

### Author: ![Tinkerer](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tinkerer](https://discourse.processing.org/u/Tinkerer)
#### Post date: [February 16, 2022, 7:10am UTC](https://discourse.processing.org/t/cropping-an-image-buffer-on-transparency/35125/2 "2022-02-16T07:10:20Z")

</div>

Hiya

You could loop through the pixels of the buffer

do it four times starting at each corner and working along an edge

then work inwards until you get the first pixel with color

this would then give you the places to start and end your crop

---

<div class="post-metadata">

### Author: ![xeronimo](https://avatars.discourse-cdn.com/v4/letter/x/8491ac/32.png) [@xeronimo](https://discourse.processing.org/u/xeronimo)
#### Post date: [February 16, 2022, 12:38pm UTC](https://discourse.processing.org/t/cropping-an-image-buffer-on-transparency/35125/3 "2022-02-16T12:38:16Z")

</div>

Hi, I found a solution using Javascript and I have ported it to p5js: [trimCanvas - Pastebin.com](https://pastebin.com/x7KxSMRy)
