Search code examples
imageurlprocessingflickr

bad image data error when loading image from url processing


i have a piece of code that gets image urls of flickr now i want the images fro these urls to be saved in an array so i can use them later on. but i keep getting the following error:

The file https://www.flickr.com/photos/7851888@N08/22115289859 contains bad image data, or may not be an image

see my code: in the code will be a comment to show what line i think generates the error.

PImage[] images;
int imageIndex;
XML xml; 
String tag_mode = "all";
String words[];
PImage[] displayImages;
int amount = 500;

void setup() {
size(50, 50);
String lines[] = loadStrings("text.txt");
words = split(lines[0], " ");
displayImages = new PImage[amount];


for (int k = 0; k<words.length; k++) {  
String query = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=MYKEY&tags="+ words[k] + "&sort=relevance&extras=url_o&tag_mode="+ tag_mode +"format=rest";
xml = loadXML(query);
XML[] children = xml.getChildren("photos");
if (children.length > 0) {
  XML[] childPhoto = children[0].getChildren("photo");
  //    println(childPhoto);

  if (childPhoto.length > 0) {
    for (int i = 0; i < 1; i++) {
      String id = childPhoto[i].getString("id");
      String title = childPhoto[i].getString("title");
      String user = childPhoto[i].getString("owner");
      String url = "https://www.flickr.com/photos/"+user+"/"+id;
      println(url);
      println("=====================");
      displayImages[i] = loadImage(url, "jpg");   // here is probably the error
      println(displayImages.length);
    }
  }
}
}

textAlign(CENTER, CENTER);
smooth();
}

void draw() {
}

Solution

  • The url you're providing isn't the URL of an image. It's the URL of a flickr webpage. You have to extract the actual image URL, not just the page URL.

    For example, this is the URL you're going to: https://www.flickr.com/photos/7851888@N08/22115289859

    That's a page, not an image. That's why you're getting the error: Processing doesn't know how to create an image from a page.

    That page happens to contain an image, but Processing isn't smart enough to figure that out. For what it's worth, that image's url is this: https://c2.staticflickr.com/6/5826/22115289859_91944c93cb_b.jpg

    You have to find the image url on the page. It's probably somewhere in the xml structure.