DALLE 2 - Transparent background and load image

Hi brains !
I’ll be honest, first day into processing today.
I’ve done what I needed, basically crop and reposition images but I still have an issue about how to set the gray background to transparent.

Here’s the code:


PImage img;
void setup() {
  size(4096,4096);
  noLoop();  
  int run = 0;
  int abscenterX = (width/2)-512;
  int abscenterY = (height/2)-512;
  imagePoss(abscenterX,abscenterY,run);
  run++;
  imagePoss(abscenterX+768,abscenterY-768,run);
  exit();
}
  
void imagePoss(int centerX,int centerY,int run)
{
  int sides = 5;
  int posX = 0;
  int posY = 0;
  int cropX = 0;
  int cropY = 0;
  for (int i = 0; i<sides; i++)
  {
    if (i%5 == 0)
    {  
      posX = centerX;
      posY = centerY;
    }
    if (i%5 == 1)
    {  
       posX = centerX;
       posY = centerY-768;
    }
    if (i%5 == 2)
    {
       posX = centerX+768;
       posY = centerY;
    }
    if (i%5 == 3)
    {
       posX = centerX;
       posY = centerY+768;
    }
     if (i%5 == 4)
    {
       posX = centerX-768;
       posY = centerY;
    }
    cropX = posX;
    cropY = posY;
    img = loadImage((run*sides)+i + ".png");  // Load the image into the program  
    image(img, posX, posY, 1024, 1024);
    PImage CropImage = get(cropX,cropY,1024,1024);
    CropImage.save(run+"_"+i+"_Crop.png");
    save("out.png");
  } 
}

Hi @Scaprendering,

The canvas background in processing is always opaque.
If you want to save your images with transparency you need to go via PGraphics object

Also check this.

Cheers
— mnse

Thanks for the reply.
I’ve found the same answer around the forum but I have no idea about how to implement that in my script.

Hi @Scaprendering,

Unfortunately I can’t test as I’m not at my box … but s.th. like this should help…

Cheers
— mnse

PGraphics pg;
PImage img;

void setup() {
  size(4096,4096, P2D);
  noLoop();  
  pg = createGraphics(width, height, P2D);
  pg.beginDraw();
  pg.clear();
  int run = 0;
  int abscenterX = (width/2)-512;
  int abscenterY = (height/2)-512;
  imagePoss(abscenterX,abscenterY,run);
  run++;
  imagePoss(abscenterX+768,abscenterY-768,run);
  pg.endDraw();
  exit();
}
  
void imagePoss(int centerX,int centerY,int run)
{
  int sides = 5;
  int posX = 0;
  int posY = 0;
  int cropX = 0;
  int cropY = 0;
  for (int i = 0; i<sides; i++)
  {
    if (i%5 == 0)
    {  
      posX = centerX;
      posY = centerY;
    }
    if (i%5 == 1)
    {  
       posX = centerX;
       posY = centerY-768;
    }
    if (i%5 == 2)
    {
       posX = centerX+768;
       posY = centerY;
    }
    if (i%5 == 3)
    {
       posX = centerX;
       posY = centerY+768;
    }
     if (i%5 == 4)
    {
       posX = centerX-768;
       posY = centerY;
    }
    cropX = posX;
    cropY = posY;
    img = loadImage((run*sides)+i + ".png");  // Load the image into the program  
    pg.image(img, posX, posY, 1024, 1024);
    PImage CropImage = pg.get(cropX,cropY,1024,1024);
    CropImage.save(run+"_"+i+"_Crop.png");
  } 
  pg.save("out" + run + ".png");
}

Edit: changed back on get to PImage

Oh thanks for that !
I understood a little better how this work thanks to your example but unfortunately I still get an error.

PGraphics CropImage = get(cropX,cropY,1024,1024);

“cannot convert from PImage to PGraphics”