Using PGraphics convenience methods for drawing on PImage

Java Mode:

PGraphics clonePImageAsPGraphics(final PImage img) {
  final PGraphics pg = createGraphics(img.width, img.height);

  pg.format = img.format;
  pg.pixelDensity = img.pixelDensity;
  pg.pixelWidth = img.pixelWidth;
  pg.pixelHeight = img.pixelHeight;

  pg.beginDraw();
  pg.endDraw();

  img.loadPixels();
  pg.loadPixels();
  arrayCopy(img.pixels, pg.pixels);
  pg.updatePixels();

  return pg;
}

Python Mode:

def clonePImageAsPGraphics(img):
    pg = createGraphics(img.width, img.height)

    pg.format = img.format
    pg.pixelDensity = img.pixelDensity
    pg.pixelWidth = img.pixelWidth
    pg.pixelHeight = img.pixelHeight

    pg.beginDraw()
    pg.endDraw()

    img.loadPixels()
    pg.loadPixels()
    arrayCopy(img.pixels, pg.pixels)
    pg.updatePixels()

    return pg
2 Likes