# Using PGraphics convenience methods for drawing on PImage

**URL:** <https://discourse.processing.org/t/using-pgraphics-convenience-methods-for-drawing-on-pimage/13076>\
**Category:** Coding Questions\
**Created:** [July 30, 2019, 10:29pm UTC](https://discourse.processing.org/t/using-pgraphics-convenience-methods-for-drawing-on-pimage/13076 "2019-07-30T22:29:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)\
**Post date:** [July 30, 2019, 10:30pm UTC](https://discourse.processing.org/t/using-pgraphics-convenience-methods-for-drawing-on-pimage/13076/1 "2019-07-30T22:30:00Z")

</div>

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 )

---

<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:** [July 30, 2019, 11:41pm UTC](https://discourse.processing.org/t/using-pgraphics-convenience-methods-for-drawing-on-pimage/13076/2 "2019-07-30T23:41:53Z")

</div>

> [@LoliProcessor](#):
>
> / to get a PGraphics from a PImage.

## Java Mode:

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

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

```

---

<div class="post-metadata">

**Author:** ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)\
**Post date:** [July 31, 2019, 7:19am UTC](https://discourse.processing.org/t/using-pgraphics-convenience-methods-for-drawing-on-pimage/13076/3 "2019-07-31T07:19:40Z")

</div>

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