I Can't export pdf

Hi, i would like export in pdf the mycelium project “https://github.com/rspt/processing-mycelium.git” by many frames into one file, but the pde don’t start for this error in the last line of code ( } ) : Bad identifier? Did you forget a variable or start an identifier with digits near ‘’?

Any help?

Thanks

import processing.pdf.*;

// https://github.com/rspt/processing-mycelium.git

import java.util.Iterator;

ArrayList cells;
ArrayList newcells;
PImage img;
float food[][];

String filename = "darkvador.jpg";
boolean inverted = false; // true -> white background

void setup()
{
  size(1200, 750);
  beginRecord(PDF, "everything.pdf");
  img = loadImage("images/base/" + filename);
  food = new float[width][height];
  for (int x = 0; x < width; ++x)
    for (int y = 0; y < height; ++y) {
      food[x][y] = ((img.pixels[(x+y*width)] >> 8) & 0xFF)/255.0;
      if (inverted) food[x][y] = 1-food[x][y];
    }
  if (inverted) {
    background(255);
  } else {
    background(0);
  }
  cells = new ArrayList();
  newcells = new ArrayList();
  Cell c = new Cell();
  c.xpos = width/2;
  c.ypos = height/4;
  cells.add(c);
}
void draw()
{
  loadPixels();
  newcells.clear();
  Iterator<Cell> itr = cells.iterator();
  while (itr.hasNext ()) {
    Cell c = itr.next();
    c.draw();
    c.update();
  }

  cells.addAll(newcells);
  updatePixels();
}

float feed(int x, int y, float thresh) {
  float r = 0.0;
  if (x >= 0 && x < width && y >= 0 && y < height) {
    if (food[x][y] > thresh) {
      r = thresh;
      food[x][y] -= thresh;
    } else {
      r = food[x][y];
      food[x][y] = 0.0;
    }
  }
  return r;
}

class Cell {
  float xpos, ypos;
  float dir;
  float state;
  Cell() {
    xpos = random(width);
    ypos = random(height);
    dir = random(2*PI);
    state = 0;
  }
  Cell(Cell c) {
    xpos = c.xpos;
    ypos = c.ypos;
    dir = c.dir;
    state = c.state;
  }
  void draw() {
    if (state > 0.001 && xpos >= 0 && xpos < width && ypos >= 0 && ypos < height) {
      if (inverted) {
        pixels[ int(xpos) + int(ypos) * width ] = color(0, 0, 0);
      } else {
        pixels[ int(xpos) + int(ypos) * width ] = color(255, 255, 255);
      }
    }
  }
  void update()
  {
    state += feed(int(xpos), int(ypos), 0.3) - 0.295;
    xpos += cos(dir);
    ypos += sin(dir);
    dir += random(-PI/4, PI/4);
    if (state > 0.15 && cells.size() < 100) {
      divide();
    } else
      if (state < 0) {
      xpos += random(-15, +15);
      ypos += random(-15, +15);
      state = 0.001;
    }
  }
  void divide() {
    state /= 2;
    Cell c = new Cell(this);
    float dd = random(PI/4);
    dir += dd;
    c.dir -= dd;
    newcells.add(c);
  }
  void keyPressed() {
  if (key == 'q') {
    endRecord();
    exit();
    }
   }

Welcome to the forum. The very first thing that you should learn to do is to format your code so that we can copy/paste it into our own editors. eg, the following won’t compile.

Furthermore the url to the GitHub repository is incorrect; it should be: https://github.com/rspt/processing-mycelium

When you go to copy/paste code to the forum enter two sets of three grave characters with your code sandwiched in between, eg

```
type or paste code here
```

These steps will increase the likelihood that someone can help you.

1 Like

Hi and welcome

Here some hints

Thank you Swan for your advices.

Thanks Jafal, I’ve tried also that but doesn’t work…

The next thing you should do is edit your code so it will run without error. I found at least three major errors:

  1. There needs to be a terminal closing brace which is the source of the initial error message.
  2. The name of the image file does not match what is in the ‘images’ folder provided by the original author. He used the extension .jpeg and you used the extension .jpg. That won’t work and needs to be changed to be consistent, one way or the other.
  3. The function keyPressed() should be outside of the Cell class block. The app won’t exit after hitting ‘q’ the way it is written.

However, that’s not going to solve all your problems, although it will allow your code to run without error. The pdf file that is produced on my system is pure black; there is no white at all. I _think this has to do with the fact that you are trying to create a .pdf file from pixels and you should be using something else. This is the real dilemma. The original code created a .png image which on a Mac can easily be converted to a .pdf file, but likely that is not the solution you are looking for, and I’m fairly certain there are other ways of doing it.

Thank you Svan, would you gently edit the file so I can easily see the differences? anyway what I would achieve is a vector file from an image, even and better is an svg file is good for me.
I also tried the examples in the processing libraries about exporting svg files but without success.

would you gently edit the file

Unfortunately, I can’t edit your post; only you can do that. Are you using a mobile device to make these posts?

No i post them using a Mac

Hi there,

I found a few things to correct. I think your inverted condition may have been overwriting your .pdf with solid colour and perhaps the logic was inverted between the colour usage.

I replaced a point() inside your class and made it draw a different colour. It seem the updated pixels are not drawn as vector graphics in the .pdf library. Using point it wrote dots where the pixels would have been.

I also removed the ‘q’ keypressed function from the class because as the code was it could not be triggered. ** You must press q to export the .pdf it also outputs a window frame which you will see is not the same image as the pixels are drawn a different colour.**

By running this altered code you may be able to find a way to trouble shoot your goal.

Best of luck!

import processing.pdf.*;

// https://github.com/rspt/processing-mycelium.git

import java.util.Iterator;

ArrayList cells;
ArrayList newcells;
PImage img;
float food[][];

String filename = "darkvador.jpg";
boolean inverted = false; // true -> white background

void setup()
{
  size(1200, 750);
  background(255);
  beginRecord(PDF, "everything.pdf");
  img = loadImage("darkvador.jpg");
  food = new float[width][height];
  for (int x = 0; x < width; ++x)
    for (int y = 0; y < height; ++y) {
      food[x][y] = ((img.pixels[(x+y*width)] >> 8) & 0xFF)/255.0;
      if (inverted) food[x][y] = 1-food[x][y];
    }
  //if (inverted) {
  //  background(255);
  //} else {
  //  background(0);
  //}
  cells = new ArrayList();
  newcells = new ArrayList();
  Cell c = new Cell();
  c.xpos = width/2;
  c.ypos = height/4;
  cells.add(c);
}
void draw()
{
  loadPixels();
  newcells.clear();
  Iterator<Cell> itr = cells.iterator();
  while (itr.hasNext ()) {
    Cell c = itr.next();
    c.draft();
    c.update();
  }

  cells.addAll(newcells);
  updatePixels();
}

float feed(int x, int y, float thresh) {
  float r = 0.0;
  if (x >= 0 && x < width && y >= 0 && y < height) {
    if (food[x][y] > thresh) {
      r = thresh;
      food[x][y] -= thresh;
    } else {
      r = food[x][y];
      food[x][y] = 0.0;
    }
  }
  return r;
}

class Cell {
  float xpos, ypos;
  float dir;
  float state;
  Cell() {
    xpos = random(width);
    ypos = random(height);
    dir = random(2*PI);
    state = 0;
  }
  Cell(Cell c) {
    xpos = c.xpos;
    ypos = c.ypos;
    dir = c.dir;
    state = c.state;
  }
  
  void draft() {
    if (state > 0.001 && xpos >= 0 && xpos < width && ypos >= 0 && ypos < height) {
      if (inverted) {
        pixels[ int(xpos) + int(ypos) * width ] = color(0, 255, 0);
        //strokeWeight(6);
        stroke(0,255,0);
        point(xpos,ypos);
      } else {
        pixels[ int(xpos) + int(ypos) * width ] = color(255, 0, 0);
        
        strokeWeight(3);
        stroke(0,0,255);
        point(xpos,ypos);
      }
    }
  }

  void update()  {
    state += feed(int(xpos), int(ypos), 0.3) - 0.295;
    xpos += cos(dir);
    ypos += sin(dir);
    dir += random(-PI/4, PI/4);
    if (state > 0.15 && cells.size() < 100) {
      divide();
    } else
      if (state < 0) {
        xpos += random(-15, +15);
        ypos += random(-15, +15);
        state = 0.001;
      }
  }
  
  void divide() {
    state /= 2;
    Cell c = new Cell(this);
    float dd = random(PI/4);
    dir += dd;
    c.dir -= dd;
    newcells.add(c);
  }
}
  void keyPressed() {
    if (key == 'q') {
      
      endRecord();
      println("made .pdf");
      saveFrame();
      exit();
    }
  }


1 Like

Thank you very much JSGauthier !!! Your idea of converting pixel in points has been the winning solution.

1 Like

Thanks you all for your help.

1 Like

I’m glad I was able to help @airobit . When you are done can you repost the functional code that works? I am curious to try it out!

The original code that you changed and posted could export a pdf file, so was ok for the goal that i wanted reach. Thanks again.

1 Like