How to load an image from the internet to use as a background

edited February 2016 in Questions about Code

I'm doing an assignment in my programming class on Cellular Automaton, and I want to be able to load images off the internet instead of having to download them and add them into the sketch everytime. This is the image I'm trying to load, and it's open access. Here's my code:

  PImage img;
  void setup() {
     size(620, 620);
     String url = "https://" + "imagecomics.com/uploads/releases/_main/Monstress_01-1.png";
     PImage webImg = loadImage(url, "png");
     surface.setResizable(true);
     surface.setSize(webImg.width, webImg.height)
     }

  void draw() {
    if (frameCount == 1) {
      image(img, 0, 0);
     }
    if (frameCount > 3) {
    set(int(random(width)), int(random(height)), color(random(256), random(256), random(256)));
    }
   for (int x = 0; x < width-1; x++) {
     for (int y = 0; y < height-1; y++) {
       color c0 = get(x+y, y);
       color c1 = get(x-y, y);
        if (blue(c0) > red(c1) || blue(c0) > blue(c1)) { 
         set(x, y, c1);
         set(x+1, y, c0);
        }
    c0 = get(x, y);
    c1 = get(x, y+1);
    if (saturation(c0) < hue(c1)) {
      set(x, y, c1);
      set(x, y+1, c0);
        }
      }
    }
  }
}
Tagged:

Answers

  • Answer ✓

    You haven't initialized variable img! :-\"

  • You have a global variable, img, that would be perfect for loading the image into. It's a shame that you loaded it into a local variable webImg instead, and then never used it.

  • So, I would change webImg to img? Would I have to modify any other bits of code to make it properly fetch the image and any metadata associated with it?

Sign In or Register to comment.