Loading in folder outside of my sketch's data

2019-08-13 15:09发布

问题:

Im sure there is a pretty straight forward answer to this...cant quite figure it out though.
In my processing sketch's data folder, there is a folder named test_segments. test_segments contains a bunch of images.
I need to load an image from test_segments into my PImage.
It looks like this: http://imgur.com/a/iG3B6
My code:

final int len=25;
final float thresh=170;

boolean newDesign=false;
PImage pic;

ArrayList<PImage> imgContainer;
int n=3;

void setup() {  
  size(800, 800, P2D);
  colorMode(RGB, 255);
  background(250, 250, 250);
  rectMode(CENTER);
  //imageMode(CENTER);

  pic=loadImage("hand.jpg");
  pic.resize(width, height);
  color c1 = color(200,25,25);
  color c2 = color(25, 255, 200);  

  imgContainer=new ArrayList<PImage>();
PImage pimg1=loadImage("this is where test_0.png needs to go")
pimg1.resize(50, 50);
imgContainer.add(pimg1);
  noLoop();
  noStroke();
}

void draw() {
  if (newDesign==false) {
    return;
  }

  pic.loadPixels();

  for (int y = 0; y < height; y+=40) {
    for (int x = 0; x < width; x+=40) {
      int index=y*width+x;
      color pixelValue = pic.pixels[index];
      color rgb = pixelValue;
      int r = (rgb >> 16) & 0xFF;  // Faster way of getting red(argb)
      int g = (rgb >> 8) & 0xFF;   // Faster way of getting green(argb)
      int b = rgb & 0xFF;  

//How far is the current color from white
float dista=dist(r,g,b,255,255,255);

//50 is a threshold value allowing close to white being identified as white
//This value needs to be adjusted based on your actual background color
//Next block is processed only if the pixel not white
if(dista>30){    
      float pixelBrightness = brightness(pixelValue); 
      float imgPicked=constrain(pixelBrightness/thresh, 0, n-1);
      image(imgContainer.get((int)imgPicked),x,y);
}
    }
  }
}



void mouseReleased() {
  newDesign=!newDesign;
  redraw();
}  

Thanks!

回答1:

You should just be able to do:

PImage pimg1 = loadImage("test_segments/test_0.png");

If that doesn't work, please try to post a MCVE like we talked about before. Here's an example of an MCVE that would demonstrate your problem:

PImage pimg1 = loadImage("test_segments/test_0.png");
image(pimg1, 0, 0);

Don't forget to include exactly what you expect to happen, and exactly what's happening instead. Good luck.



标签: processing