Strange problem with image/graphic not being found/created on linux

edited March 2016 in Questions about Code

Can anyone help with this please?

In main project file:

Map map;

void setup(){
...
map = new Map();
...
}

In Map's constructor, where (members) pgMap is a PGraphic and imgMap is a PImage, I create a graphic draw some stuff on it, save it out then reload it as an image:

...
//save to file
pgMap.save("map.png");

//re-load as an image
imgMap = loadImage("map.png");
...
}

Another of Map's methods:

void renderWorld() {
    image(imgMap, 0, 0);
}

If I call map.renderWorld() from draw() I get a null pointer/"map.png" not found error and sure enough map.png hasn't been created. If I comment out the single line in renderWorld() the map gets created normally and the program functions as expected!

I suspected a path issue but I've tried no path (as above), using a relative path to ./data and using an explicit path to /data but none of those work. A separate image used by the program stored in ./data loads from setup() without any path being specified:

loadImage("compass.jpeg");

I've also tried loading Map's image from setup(), rather than within the constructor like so:

map.imgMap = loadImage("map.png");

but that doesn't work either (also tried specifiying paths at that point).

This is on Linux Mint 32 bit with Processing 3.0.2, any help appreciated!

Answers

  • Why do you need to save it and reload it? You could use the PGraphics instead.

    I've tried this:

    Test t;
    
    void setup() {
      size(200, 200);
      t = new Test();
    }
    
    void draw() {
      t.display();
    }
    
    class Test {
      PGraphics pg;
      PImage img;
    
      Test() {
        pg = createGraphics(200, 200);  
    
        pg.beginDraw();
        pg.background(random(255), random(255), random(255));
        pg.endDraw();
    
        pg.save("test.png");
        img = loadImage("test.png");
      }
    
      void display() {
        image(img, 0, 0);
      }
    }
    

    Worked fine on Processing 2.2.1 but I get a NullPointerException on Processing 3.0.2.

  • Thanks for that, maybe it's a bug then - I'll probably get fried for saying it but I'm using OpenJDK, are you? I'm saving the images out because I want to resume the program with a previously created image (like a 'save game' feature).

  • After a little testing I guess this is some sort of malformed file issue caused by pg.save() which means loadImage() can't load it (according to debugger).

    Manually copying the image created by your code then overwriting the original in the OS file manager allows the program to work. Maybe Processing's code that saves out a file when it already exists is different to the code when creating a file from scratch...?

  • I'll probably get fried for saying it but I'm using OpenJDK, are you?

    No

    I'm saving the images out because I want to resume the program with a previously created image (like a 'save game' feature).

    In this case, don't you need to load the image only if there is a saved game?

  • No

    Good, it's not related to that then.

    In this case, don't you need to load the image only if there is a saved game?

    I need to save the image when the program is first run and re-load it on subsequent runs. I could probably work round the issue by saving it out some other way.

    Thanks for helping.

  • Test t;
    
    void setup() {
      size(200, 200);
      t = new Test();
    }
    
    void draw() {
      t.display();
    }
    
    class Test {
      PGraphics pg;
      PImage img;
    
      Test() {      
        File file = new File(sketchPath("test.png"));
        if (file.exists()) {
          img = loadImage("test.png");
        } else {
          pg = createGraphics(200, 200);      
          pg.beginDraw();
          pg.background(random(255), random(255), random(255));
          pg.endDraw(); 
    
          pg.save("test.png");    
          img = pg.get();
        } 
      }
    
      void display() {
        image(img, 0, 0);  
      }
    }
    
  • Yep, that works under 3.0.2 (must admit I didn't know about PGraphics.get() - very useful), although dataPath() is probably a more logical place for resources :D Thanks again.

  • edited March 2016

    Must admit I didn't know about PGraphics::get()

    Actually get() is from PImage. Class PGraphics extends PImage:
    https://Processing.org/reference/PImage_get_.html

    And it's a pity (and a total disconnection of Processing users' needs) that dataPath() is undocumented!
    Others similar: dataFile(), sketchPath() and sketchFile().

  • edited March 2016

    Tip: We can replace File file = new File(sketchPath("test.png"));
    With: File file = sketchFile("test.png");

    And img = loadImage("test.png"); with img = loadImage(file.getPath());

  • Good points - thank you!

Sign In or Register to comment.