# No loadPixels() for PGraphicsPDF

**URL:** https://discourse.processing.org/t/no-loadpixels-for-pgraphicspdf/31746
**Category:** Beginners
**Created:** [August 16, 2021, 1:59pm UTC](https://discourse.processing.org/t/no-loadpixels-for-pgraphicspdf/31746 "2021-08-16T13:59:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)
#### Post date: [August 16, 2021, 1:59pm UTC](https://discourse.processing.org/t/no-loadpixels-for-pgraphicspdf/31746/1 "2021-08-16T13:59:29Z")

</div>

i found this code [Pointillism | Learning Processing 2nd Edition](http://learningprocessing.com/examples/chp15/example-15-14-Pointillism)

but instead of run it on the screen, I wanted to run in 5 times on backend and save it as a pdf file,  
and got this error  
**RuntimeException: No loadPixels() for PGraphicsPDF**

here is the code I used:

```auto
import processing.pdf.*;

PImage img;
int pointillize = 16;

void setup() {
  //size(600,400);
  size(600,400,PDF,"f:\\process\\acres-01.pdf");
  img = loadImage("f:\\process\\acres-1.jpg");
  
  smooth();
}

void draw() {
  
  for (int loop = 0; loop<5; loop++)
  {
  background(0);
  
  for (int i=0; i<500; i++)
  
  {
      
  
  
  // Pick a random point
  int x = int(random(img.width));
  int y = int(random(img.height));
  int loc = x + y*img.width;

  // Look up the RGB color in the source image
  loadPixels();
  float r = red(img.pixels[loc]);
  float g = green(img.pixels[loc]);
  float b = blue(img.pixels[loc]);
  noStroke();

  // Draw an ellipse at that location with that color
  fill(r,g,b,100);
  ellipse(x,y,pointillize,pointillize);
  }
   PGraphicsPDF pdf = (PGraphicsPDF) g; // Get the renderer
  
  //saveFrame("f:\\process\\ellipse-a-loop.png");
      pdf.nextPage(); // Tell it to go to the next page 
      
  }
  exit();
  
      
}

```

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [August 16, 2021, 5:21pm UTC](https://discourse.processing.org/t/no-loadpixels-for-pgraphicspdf/31746/2 "2021-08-16T17:21:36Z")

</div>

it won’t work because PDF is a vector file and it does not store pixels.

But in fact, I think the original code is wrong. `loadPixels` loads what is on the canvas window. Instead, you need to call `img.loadPixels` to specifically load pixels from the image.

---

<div class="post-metadata">

### Author: ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)
#### Post date: [August 16, 2021, 11:41pm UTC](https://discourse.processing.org/t/no-loadpixels-for-pgraphicspdf/31746/3 "2021-08-16T23:41:10Z")

</div>

Thanks for your reply.

As a matter of fact, I don’t need to save the imgae to the pdf.  
I only want to save ellipses I create.

After I load all the pixels from the image, I no longer need it

---

<div class="post-metadata">

### Author: ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)
#### Post date: [August 17, 2021, 12:25am UTC](https://discourse.processing.org/t/no-loadpixels-for-pgraphicspdf/31746/4 "2021-08-17T00:25:59Z")

</div>

I figured it out  
I changes the loadPixels() to img.loadPixels as you suggested,  
and moved it to the setup,

and it worked

---

<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: [August 18, 2021, 11:27am UTC](https://discourse.processing.org/t/no-loadpixels-for-pgraphicspdf/31746/5 "2021-08-18T11:27:47Z")

</div>

Hello,

In _**[this example](http://learningprocessing.com/examples/chp15/example-15-14-Pointillism)**_ you did not need loadPixels() or img.loadPixels (for the PImage) at all.

Try it!

There is a GitHub issue here that discusses the language in the references and tutorials:

> <https://github.com/processing/processing-docs/issues/626>
>
> \### Issue description
> 
> Hi all. This remark comes in the first instance from Da…n Schiffman's tut "Images and Pixels", about 1/3 of the way down, where he mentions that loadPixels() and updatePixels() must be called when working with the pixels array in a PImage and the screen buffer image.
> 
> However this concept gets mentioned all over the place in Processing reference pages and tutorials. Often it's accompanied by words that these calls "should" be made, and without them code "might" fail at future releases, or with certain renderers, all very vague. 
> 
> It feels to me that it would be easier to just briefly explain the reasons behind all this. Which are presumably, that the pixel buffer is stored as a separate linear array, and that for efficiency the rest of the PImage data structure is -not- updated every time you touch a pixel, or a screen image is -not- redrawn, and etc. But rather you must signal to Processing that you are done with altering pixels. And I guess with regard to loadPixels(), there are efficiencies too. Eg. you could loadImage() an image and check the x,y dimensions of it, without the overhead of loading all the pixels, which you can do later if you actually need to, with loadPixels(). 
> 
> Maybe there are other efficiency issues .. from days long ago at SGI I recall that reading a pixel from the screen could need an expensive retrieve of the whole frame buffer, in the wrong (slow) direction of the pipeline, ie. backwards, and so you'd be better to do it once with loadPixels() and then work in a memory buffer for a while.
> 
> Anyway, I can see from the multitudes of references to the loadPixels() / updatePixels() calls that this is a tricky issue. And no doubt differences between Processing 1.0, 2.0, 3.0, and renderers P2D, P3D etc, and environments Java, JavaScript, Python etc. have made it messy. But maybe there could be a reference page, I guess pixels\[\], which actually spells it out, so the interested / pedantic user could know a bit more why this protocol is needed. 
> 
> 
> \### URL(s) of affected page(s)
> 
> https://processing.org/tutorials/pixels/
> 
> https://processing.org/reference/pixels.html
> https://processing.org/reference/updatePixels\_.html
> https://processing.org/reference/loadPixels\_.html
> 
> \### Proposed fix
> 
> A fuller explanation, somewhere, of the background behind pixels\[\], loadPixels(), updatePixels().
> 
> Cheers, Greg E

`:)`
