Using PGraphics convenience methods for drawing on PImage

Is there any way to use the convenience methods of PGraphics ( rect, circle, line, etc ) on a PImage / to get a PGraphics from a PImage.

Also, what is the easiest way to convert from PImage to a BufferedImage and the other way around? ( Then one could use the Java Graphics to draw and then convert back )

1 Like

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

Oh, right.
When I went to sleep I remembered that PGraphics extends PImage.

1 Like