# Problem with saving PImage with transparency

**URL:** <https://discourse.processing.org/t/problem-with-saving-pimage-with-transparency/5566>\
**Category:** Coding Questions\
**Created:** [November 15, 2018, 5:06pm UTC](https://discourse.processing.org/t/problem-with-saving-pimage-with-transparency/5566 "2018-11-15T17:06:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spu7Nix](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Spu7Nix](https://discourse.processing.org/u/Spu7Nix)\
**Post date:** [November 15, 2018, 5:06pm UTC](https://discourse.processing.org/t/problem-with-saving-pimage-with-transparency/5566/1 "2018-11-15T17:06:24Z")

</div>

Hi, i have written this little program to cut a spritesheet for me, but it doesn’t work with transparency. Anybody know a fix?

```auto
PImage sheet = loadImage("dirtTiles.png");
PImage[] tiles = new PImage[25];
int s = 16;

background(200, 100, 100);

String tileName = "dirt";
size(80, 80);

loadPixels();

int index = 0;

for(int x = 0; x < sheet.width; x += s){
  for(int y = 0; y < sheet.height; y += s){
    //tile
    
    PImage cTile = createImage(s, s, RGB);
    
    for(int xt = 0; xt < s; xt++){
      for(int yt = 0; yt < s; yt++){
        
         int sheetindex = x + xt + (y + yt) * sheet.width;
         int tileindex = xt + yt * s;
         
         
         
           cTile.pixels[tileindex] = sheet.pixels[sheetindex];
         
        
      }
    }
    
    cTile.updatePixels();
    tiles[index] = cTile;
    index++;
    
    
  }
}

for (int i = 0; i < tiles.length; i++) {
    tiles[i].save(tileName + str(i));
    
}

```

---

<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:** [November 16, 2018, 2:12am UTC](https://discourse.processing.org/t/problem-with-saving-pimage-with-transparency/5566/2 "2018-11-16T02:12:45Z")

</div>

besides that the array length missmatch,  
the used filename causes to save a \*.tif file,  
[https://processing.org/reference/save\_.html](https://processing.org/reference/save_.html)  
you could try as \*.png ??

```auto
    tiles[i].save("data/"+tileName +"_"+ str(i)+ ".png");

```

---

<div class="post-metadata">

**Author:** ![Spu7Nix](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Spu7Nix](https://discourse.processing.org/u/Spu7Nix)\
**Post date:** [November 16, 2018, 11:34am UTC](https://discourse.processing.org/t/problem-with-saving-pimage-with-transparency/5566/3 "2018-11-16T11:34:01Z")

</div>

Thanks, but the transparency is still not perserved. It is replaced with black pixels.

EDIT: Idk if i was clear enough. The program works fine, the problem is that the the transparent pixels is the image “sheet” isn’t perserved in the output tiles.

---

<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:** [November 16, 2018, 11:48am UTC](https://discourse.processing.org/t/problem-with-saving-pimage-with-transparency/5566/4 "2018-11-16T11:48:36Z")

</div>

ARGB instead RGB ???

[https://processing.org/reference/createImage\_.html](https://processing.org/reference/createImage_.html)

---

<div class="post-metadata">

**Author:** ![Spu7Nix](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Spu7Nix](https://discourse.processing.org/u/Spu7Nix)\
**Post date:** [November 16, 2018, 6:09pm UTC](https://discourse.processing.org/t/problem-with-saving-pimage-with-transparency/5566/5 "2018-11-16T18:09:07Z")

</div>

That did it, thank you!
