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

**URL:** <https://discourse.processing.org/t/mask-can-only-be-used-with-an-image-thats-the-same-size/46633>\
**Category:** py5\
**Created:** [June 30, 2025, 3:30pm UTC](https://discourse.processing.org/t/mask-can-only-be-used-with-an-image-thats-the-same-size/46633 "2025-06-30T15:30:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNitaine](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@SNitaine](https://discourse.processing.org/u/SNitaine)\
**Post date:** [June 30, 2025, 3:30pm UTC](https://discourse.processing.org/t/mask-can-only-be-used-with-an-image-thats-the-same-size/46633/1 "2025-06-30T15:30:29Z")

</div>

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? 🫣

Sample code

```py
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()

```

```auto
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.

```

---

<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:** [June 30, 2025, 3:49pm UTC](https://discourse.processing.org/t/mask-can-only-be-used-with-an-image-thats-the-same-size/46633/2 "2025-06-30T15:49:15Z")

</div>

Hello @SNitaine,

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

This is a working Processing example:

```auto
// 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);
  }

```

`:)`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 30, 2025, 4:04pm UTC](https://discourse.processing.org/t/mask-can-only-be-used-with-an-image-thats-the-same-size/46633/3 "2025-06-30T16:04:21Z")

</div>

This is the Java source code that throws that exception:

> <https://github.com/processing/processing4/blob/processing-1304-4.4.4/core/src/processing/core/PImage.java#L798-L799>

Somehow, the corresponding _pixels[]_ from _pg_ & _img_ aren’t matching for **len()**!

Debug them using **load\_pixels()** at the end of **setup()**:

> **[load\_pixels() — py5 documentation](https://py5coding.org/reference/sketch_load_pixels.html)**

```py
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):

> **[pixel\_density() — py5 documentation](https://py5coding.org/reference/sketch_pixel_density.html)**

---

<div class="post-metadata">

**Author:** ![SNitaine](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@SNitaine](https://discourse.processing.org/u/SNitaine)\
**Post date:** [June 30, 2025, 5:23pm UTC](https://discourse.processing.org/t/mask-can-only-be-used-with-an-image-thats-the-same-size/46633/4 "2025-06-30T17:23:59Z")

</div>

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

```python
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

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

```

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

```auto
pg pixels: 40000
pg density: 1
img pixels: 40000
img density: 1

```

Thanks 👌
