Mask() can only be used with an image that's the same size

Can anyone please explain the error mask() can only be used with an image that's the same size.? Every time I try to use a Py5Graphics object/image as a mask, I run into that error. But the image loaded is the same pixel size as Py5Graphic object both 200x200px.

What am I missing? :face_with_peeking_eye:

Sample code

import py5


def setup():
    global pg, img
    py5.size(400, 400)
    py5.background(100)
    pg = py5.create_graphics(200, 200)
    img = py5.load_image("sq.jpg")  # image is 200x200px


def draw():
    pg.begin_draw()
    pg.background(255)
    pg.fill(0)
    pg.circle(pg.width / 2, pg.height / 2, 150)
    pg.end_draw()
    
    img.mask(pg)
    py5.image(img, 50, 50)


py5.run_sketch()
cut-out-shapes.py", line 19, in draw
    12   def draw():
 (...)
    15       pg.fill(0)
    16       pg.circle(pg.width / 2, pg.height / 2, 150)
    17       pg.end_draw()
    18       
--> 19       img.mask(pg)
    20       py5.image(img, 50, 50)
    ..................................................
     pg = Py5Graphics(width=200, height=200)
    ..................................................

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: mask() can only be used with an image that's the same size.

Hello @SNitaine,

Try printing out the size to verify, setting it exactly or using resize().

This is a working Processing example:

// Masking
// v1.0.0
// GLV 2021-07-24

PGraphics pg1;
PImage img1;

void setup() 
  {
  size(300, 300);
  
  String urlString = "http://learningprocessing.com/code/assets/sunflower.jpg";
  img1 = loadImage(urlString);
  println(img1.width, img1.height);
  
  pg1 = createGraphics(300, 300); //IllegalArgumentException: mask() can only be used with an image that's the same size.
  pg1 = createGraphics(img1.width, img1.height);
  //img1.resize(pg1.width, pg1.height);
  }

void draw() 
  {
  background(255, 255, 0);

  pg1.beginDraw();
  pg1.background(255);
  pg1.fill(0);
  pg1.circle(pg1.width/2, pg1.height/2, 150);
  pg1.endDraw();
  
  img1.mask(pg1);
  
  image(img1, 0, 0);
  }

:)

This is the Java source code that throws that exception:

Somehow, the corresponding pixels[] from pg & img aren’t matching for len()!

Debug them using load_pixels() at the end of setup():

pg.load_pixels(); img.load_pixels()

print(
    pg_len := len(pg.pixels),
    img_len := len(img.pixels), # 200 x 200 px must be 40_000 pixels
    pg_len == img_len,
    sep='\n'
)

If they’re indeed of different len(), you might try out the py5.pixel_density(1) workaround (no guarantees though):

1 Like

Yes @GoToLoop you are correct, the pixels aren’t the same density.

pg.load_pixels()
print("pg pixels: ", len(pg.pixels))
print("pg density: ", pg.pixel_density)
img.load_pixels()
print("img pixels: ", len(img.pixels))
print("img density:", img.pixel_density)

Prints the following output

pg pixels:  160000
pg density:  2
img pixels:  40000
img density: 1

Indeed setting py5.pixel_density(1) fixes the issues.

pg pixels:  40000
pg density:  1
img pixels:  40000
img density: 1

Thanks :ok_hand:

2 Likes