# Making part of a PImage transparent

**URL:** <https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295>\
**Category:** Coding Questions\
**Created:** [January 8, 2019, 12:48am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295 "2019-01-08T00:48:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BongoTurtle](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@BongoTurtle](https://discourse.processing.org/u/BongoTurtle)\
**Post date:** [January 8, 2019, 12:48am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/1 "2019-01-08T00:48:50Z")

</div>

I would like to map an image to a sphere and have part of it transparent, but I can’t figure out how to make the image transparent. Is this possible? If not what would be the best way to do something like this. Thanks.

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 8, 2019, 12:57am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/2 "2019-01-08T00:57:50Z")

</div>

Does the image itself contain transparent pixels?

---

<div class="post-metadata">

**Author:** ![BongoTurtle](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@BongoTurtle](https://discourse.processing.org/u/BongoTurtle)\
**Post date:** [January 8, 2019, 1:39am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/3 "2019-01-08T01:39:19Z")

</div>

That’s the idea, but when I do that those pixels just end up at full alpha when they’re mapped to the sphere.

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 8, 2019, 1:48am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/4 "2019-01-08T01:48:39Z")

</div>

Okay. I did a bit of testing and the only way to do this is create the sphere as a PShape and then call setTexture()

```auto
PImage img;
PShape sphere;
void setup(){
size(400,400,P3D);
img=loadImage("treeBranches.png");
sphere = createShape(SPHERE, 90);
sphere.setTexture(img);
sphere.setStroke(color(0,0));
}
void draw(){
  background(0);
  translate(width/2.0,height/2.0,-150);
  shape(sphere); 
}

```

---

<div class="post-metadata">

**Author:** ![BongoTurtle](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@BongoTurtle](https://discourse.processing.org/u/BongoTurtle)\
**Post date:** [January 8, 2019, 1:59am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/5 "2019-01-08T01:59:43Z")

</div>

I tried using this but its still not transparent.

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 8, 2019, 2:04am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/6 "2019-01-08T02:04:32Z")

</div>

could you post the part of the code where you’re rendering the sphere?

---

<div class="post-metadata">

**Author:** ![BongoTurtle](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@BongoTurtle](https://discourse.processing.org/u/BongoTurtle)\
**Post date:** [January 8, 2019, 2:16am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/7 "2019-01-08T02:16:53Z")

</div>

Something like this

```auto
PImage image;
PShape sphere;

void setup(){
  size(1080,720,P3D);
  sphere = createShape(SPHERE,200);
  image = new PImage(200,100);
  for(int i = 0; i < 200; i++){
    for(int j = 0; j < 100; j++){
      image.set(i,j,color(i,10));
    }
  }
  sphere.setTexture(image);
}

void draw(){
  background(0);  
  translate(540,360,-250);
  rotateY(frameCount*.01);
  shape(sphere); 
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 8, 2019, 3:19am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/8 "2019-01-08T03:19:46Z")

</div>

i try play with background color and fill transparency  
and it works with ARGB,  
but not like you see the stroke of the sphere through…

```auto
PImage imaget;
PShape sphere;

void setup(){
  size(500,500,P3D);
  sphere = createShape(SPHERE,100);
  imaget = createImage(200,100,ARGB);
  for(int i = 0; i < 200; i++){
    for(int j = 0; j < 100; j++){
      imaget.set(i,j,color(0,100,0,50));
    }
  }
  sphere.setTexture(imaget);
  println("use: mouseX for background color, mouseY for transparency");
}

void draw(){
  background(map(mouseX,0,width,0,255),0,255-map(mouseX,0,width,0,255));  
  translate(width/2,height/2,-100);
  rotateY(frameCount*.01);
  shape(sphere); 
}

void mouseMoved() {
  imaget = createImage(200,100,ARGB);
  for(int i = 0; i < 200; i++){
    for(int j = 0; j < 100; j++){
      imaget.set(i,j,color(0,100,0,map(mouseY,0,height,0,255)));
    }
  }
  sphere.setTexture(imaget);
}

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [January 8, 2019, 4:08am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/9 "2019-01-08T04:08:10Z")

</div>

You can remove the lines on the sphere, using kll’s demo.

Kf

```java
PImage imaget;
PShape sphere;

int spAlpha;

void setup() {
  size(500, 500, P3D);
  colorMode(RGB,256,256,256,256);

  spAlpha = 255;

  sphere = createShape(SPHERE, 100);
  setSphere();
 // sphere.setTexture(imaget);
  println("use: mouseX for background color, mouseY for transparency");
}

void draw() {
  
  background(map(mouseX, 0, width, 0, 255), 0, 255-map(mouseX, 0, width, 0, 255));  
  translate(width/2, height/2, -100);
  rotateY(frameCount*.01);
  
  setSphere();  
  shape(sphere);
}

void mouseMoved() {
  spAlpha=int(map(mouseY, 0, height, 0, 255));
}

void setSphere() {  
  sphere.setStroke( color(0, 100, 0, spAlpha)); //For no lines, set alpha to 0
  imaget = createImage(200, 100, ARGB);
  for (int i = 0; i < imaget.width; i++) {
    for (int j = 0; j < imaget.height; j++) {
      imaget.set(i, j, color(0, 100, 0, spAlpha));
    }
  }
  sphere.setTexture(imaget);
}

```

---

<div class="post-metadata">

**Author:** ![BongoTurtle](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@BongoTurtle](https://discourse.processing.org/u/BongoTurtle)\
**Post date:** [January 8, 2019, 4:58am UTC](https://discourse.processing.org/t/making-part-of-a-pimage-transparent/7295/10 "2019-01-08T04:58:00Z")

</div>

Thank you that worked.
