# Erase part of PGraphics image to transparent

**URL:** <https://discourse.processing.org/t/erase-part-of-pgraphics-image-to-transparent/7605>\
**Category:** Coding Questions\
**Created:** [January 17, 2019, 9:53pm UTC](https://discourse.processing.org/t/erase-part-of-pgraphics-image-to-transparent/7605 "2019-01-17T21:53:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ottenm](https://avatars.discourse-cdn.com/v4/letter/o/f08c70/32.png) [@ottenm](https://discourse.processing.org/u/ottenm)\
**Post date:** [January 17, 2019, 9:53pm UTC](https://discourse.processing.org/t/erase-part-of-pgraphics-image-to-transparent/7605/1 "2019-01-17T21:53:42Z")

</div>

Any help cutting/cropping out part of a PGraphics image, leaving behind a transparent rectangle?

I tried the code below, but it just adds a transparent rectangle on top (no visual change to the underlying image). I did change the fill to (255, 100) to verify the coordinates and dimensions, it added a light white semi-transparent rectangle on top of what’s (still visible) underneath.

`img.beginDraw();
img.noStroke();
img.fill(255, 0);
img.rect(100, 100, 200, 200);
img.endDraw();`

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 18, 2019, 12:20am UTC](https://discourse.processing.org/t/erase-part-of-pgraphics-image-to-transparent/7605/2 "2019-01-18T00:20:08Z")

</div>

You could probably use the `set()` function or one of the pixel-related functions in [the reference](https://processing.org/reference/).

If you’re still stuck, can you please post a more complete example that we can copy and paste to run ourselves?

---

<div class="post-metadata">

**Author:** ![ottenm](https://avatars.discourse-cdn.com/v4/letter/o/f08c70/32.png) [@ottenm](https://discourse.processing.org/u/ottenm)\
**Post date:** [January 18, 2019, 4:08pm UTC](https://discourse.processing.org/t/erase-part-of-pgraphics-image-to-transparent/7605/3 "2019-01-18T16:08:47Z")

</div>

Thanks for the help Kevin. Sorry to leave out the constructor and display:  
`
size(500, 500);
PGraphics img = createGraphics(500, 500);
img.beginDraw();
img.noStroke();
img.fill(255, 0);
img.rect(100, 100, 200, 200);
img.endDraw();
image(img, 0, 0);`

I am having some luck writing to the pixels directly:  
`java.util.Arrays.fill(img.pixels, 1000, 100000, color(255, 0));`

Thanks again!

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 18, 2019, 4:43pm UTC](https://discourse.processing.org/t/erase-part-of-pgraphics-image-to-transparent/7605/4 "2019-01-18T16:43:41Z")

</div>

You could also play with the `blendMode()` function. I think `blendMode(REPLACE)` might be what you’re looking for, but I haven’t tried it out. More info can be found in [the reference](https://processing.org/reference/blendMode_.html).

---

<div class="post-metadata">

**Author:** ![JackWitherell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jackwitherell/32/3361_2.png) [@JackWitherell](https://discourse.processing.org/u/JackWitherell)\
**Post date:** [January 19, 2019, 12:01pm UTC](https://discourse.processing.org/t/erase-part-of-pgraphics-image-to-transparent/7605/5 "2019-01-19T12:01:54Z")

</div>

I’ve actually used blendMode(REPLACE) before in a (failed) project I worked on a while back.  
[link to the line i used it on with a bit of context on the surrounding lines](https://github.com/JackWitherell/Pictochat-PC/blob/master/PictoChat/PictoChat.pde#L359)

setting blendMode(REPLACE); and then setting your stroke/fill color to transparent color(0,0,0,0); will let you draw transparency with lines, rects, anything you want really.

It works well. Just don’t forget to send your blendMode back to the default blendMode(BLEND); when you’re done.

Love the link to the reference, Kevin!
