Loading...
Logo
Processing Forum
Hi,

I'm working on a simple paint type application, but I want to use masks. You pick two images and 'paint' the mask.

Copy code
  1. PGraphics maskPattern;
  2. PImage img;
  3. PImage img2;

  4. void setup() {
  5.   img = loadImage("moonwalk.jpg");
  6.   size(800,400);
  7.   maskPattern = createGraphics(img.width,img.height);
  8.   
  9.   img2 = loadImage("greenmountains.jpg");
  10. }

  11. void draw() {
  12.   background(0);
  13.   image(img2,0,0);
  14.   
  15.   if (mousePressed == true) {
  16.   maskPaint();
  17.   }


  18.   
  19.   img.mask(maskPattern);
  20.   image(img,0,0);
  21. }

  22. void maskPaint(){
  23.   maskPattern.beginDraw();
  24.   //pg.background(0);
  25.   maskPattern.stroke(255);
  26.   maskPattern.fill(255);
  27.   maskPattern.ellipse(mouseX,mouseY,50,50);
  28.   maskPattern.endDraw();  
  29. }

  30. void keyPressed() {
  31.  if(key == ' ')
  32. {
  33.  saveFrame("mask_image.jpg");
  34. }  
  35. }

I would like to be able to save these images at higher resolution than the stage dimensions allow. saveFrame "flattens" the first image, the mask, and the second image to make an image. This is the functionality I would like, but it is at the resolution of the stage. It would be nice to be able to bring in photos with higher resolution than the stage (like a cellphone res), draw your mask and save everything at a higher resolution jpg.

Is there a simple "combining" or "flattening" functionality? Or will I need to go through the pixels on a new PImage and assign img or img2 to a particular pixel depending on the mask's pixel information (0 vs. 255)? Or maybe this is a job for shaders (which I have no experience with)?

Any advice is appreciated.

Replies(3)

saveFrame() flattens nothing, it just saves what is on the sketch area.
Given that you don't use the ### pattern, you can use as well save().
And nothing prevents you to use somePGraphics.save() too.
This is probably what you look for: make a PGraphics at the wanted size, draw on it, and save it.
Ok, yeah. I just had to have a better understanding of how PGraphics worked. Creating a separate PGraphics savedImg, and using the following for my save functionality worked the way I needed:


Copy code
  1. void keyPressed() {
  2.  if(key == ' ')
  3.       {
  4.       savedImg.beginDraw();
  5.           savedImg.image(img2,0,0);
  6.           img.mask(maskPattern);
  7.           savedImg.image(img,0,0);
  8.       savedImg.endDraw();
  9.   
  10.       savedImg.save("final_image.jpg");
  11.       }  
  12. }

I'll need to make adjustments to the drawing, re-sizing and saving functionality. But this was the real hurdle. I needed to think about the PGraphics like a separate "draw" function on it's own. I know that's probably rudimentary stuff, but hopefully this will help out some beginners when they are starting.

" PGraphics like a separate "draw" function on it's own"
Exactly. Actually, when you draw on the sketch area, you already draw on a PGraphics, the main one, which can even be accessed under the name g.