# Bug related to PImage::copy() of images with transparency?

**URL:** https://discourse.processing.org/t/bug-related-to-pimage-copy-of-images-with-transparency/34536
**Category:** Libraries
**Created:** [January 9, 2022, 1:31pm UTC](https://discourse.processing.org/t/bug-related-to-pimage-copy-of-images-with-transparency/34536 "2022-01-09T13:31:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![yodan](https://avatars.discourse-cdn.com/v4/letter/y/f04885/32.png) [@yodan](https://discourse.processing.org/u/yodan)
#### Post date: [January 9, 2022, 1:31pm UTC](https://discourse.processing.org/t/bug-related-to-pimage-copy-of-images-with-transparency/34536/1 "2022-01-09T13:31:00Z")

</div>

Hello all I’d like to report what seems to be a bug related to PImage::copy() of images with transparency.

There’s an easy hack-fix and I don’t have time to further investigate so I’m just posting this in case this is somehow meant to work that way or in case others have a similar issue.

Background:  
I start with a PImage object where every pixel is transparent. I draw a simple grid with rect() and the PImage on top of it. Since the PImage it’s transparent you can see the grid (fig.1).  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/44b706da0fefdcc0bb3eae62e14aa89594d2e73b.png)  
fig.1

I can then add images (with or without transparency) to this PImage to specific coordinates, which I do with:

```auto
  void addImageGrid(PImage newImg, int x, int y, int w, int h) {
    if (x < 0 || y < 0 || x >= this.gridX || y >= this.gridY) {
      println("ERROR: addImageGrid coordinate out of range");
      return;
    }
    if (w < 1 || h < 1 || x + w > this.gridX || y + h > this.gridY) {
      println("ERROR: addImageGrid coordinate our of range");
      return;
    }
    this.img.copy(newImg, 0, 0, newImg.width, newImg.height,
      round(this.img.width * (float(x) / this.gridX)),
      round(this.img.height * (float(y) / this.gridY)),
      w * (this.img.width / this.gridX), h * (this.img.height / this.gridY));
  }

```

For example I can add the same image of a bush to many grid coordinates (fig.2).  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/655d48640eaaf016a3c5d71bb94b4e737db0b83a.png)

Problem:  
If I add an image with transparency to the 0, 0 coordinate all transparency is lost in the PImage (fig.3).  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/747678f77c5be355423afbbe1e21547de59e7628.png)

Workaround  
This seems to be an issue with the pixel at index 0 so if I skip that pixel it works fine.

I assume if I wrote my own PImage::copy() function I could avoid this issue so I think it is a bug in PImage::copy(), however I haven’t had time to test that theory. If I do I’ll post the results!

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [January 9, 2022, 2:13pm UTC](https://discourse.processing.org/t/bug-related-to-pimage-copy-of-images-with-transparency/34536/2 "2022-01-09T14:13:21Z")

</div>

Hello @yodan,

This is a related topic:

> [@Alpha of Zero in first pixel makes whole image opaque](https://discourse.processing.org/t/alpha-of-zero-in-first-pixel-makes-whole-image-opaque/32400):
>
> When I use an image as a background, then set the alpha values for all pixels in a second image to something low, then draw that image, I can see “through” the second image to the background, just as I would expect. Here, the mandrill is the second image, with alpha set to 0x40 for every pixel: If I set alpha to 0x00 for the very first pixel, the entire mandrill becomes opaque: I’m using Processing 3.5.4. Here’s my code: PImage imgFace; PImage imgMandrill; …

[https://processing.org/reference/PImage\_copy\_.html](https://processing.org/reference/PImage_copy_.html) states:

> No alpha information is used in the process, however if the source image has an alpha channel set, it will be copied as well.

Another topic that is related:  
[https://discourse.processing.org/t/pixel-processing-grabbing-the-canvas-and-wheres-the-alpha/7162](https://discourse.processing.org/t/pixel-processing-grabbing-the-canvas-and-wheres-the-alpha/7162)

`:)`
