Save individual image files scraped from API

edited October 2015 in Questions about Code

My processing sketch currently grabs 16 images from the New York Times API and output them as a grid. I also want to save the individual images (not the canvas as a whole) as .jpgs within the folder but have been unsuccessful so far

Any help is much appreciated!

Thanks

String baseURL = "http://" + "api.nytimes.com/svc/topstories/v1/";
String baseURLend= ".json?api-key=d4503d73029e6b27fdf3615c1490c4d4:12:62206830";
String homeURL;
PImage home;

void setup(){
  size(840, 560);
  background(0);
  noStroke();
  fill(102);
getHome();
home = loadImage(homeURL, "jpg");
noLoop();
}

void draw(){

  image(home, 0, 0);
}

void getHome() {

String request = baseURL + "home" + baseURLend;
String result = join( loadStrings( request ), "");
//println( result );

JSONObject nytData = JSONObject.parse(result);
//println(nytData.getString("section"));

JSONArray docArray = nytData.getJSONArray("results");
JSONObject levelOne = docArray.getJSONObject(1);
//println (levelOne);
JSONArray multiMedia = levelOne.getJSONArray("multimedia");
//println (multiMedia);
JSONObject thumbLarge = multiMedia.getJSONObject(3);
//println (thumbLarge);
homeURL = thumbLarge.getString("url");
println(homeURL);
}

Answers

  • Answer ✓

    Hey,

    Since you already store the image as a PImage, you can just use its save()-method.

    home.save(dataPath("filename.jpg"));

Sign In or Register to comment.