Tuesday, December 4, 2012

Downloading and Upload Pictures via Flickr with Processing

Over the  weekend i was asked to do some processing code for someone. Basically, it involved searching by a inputted term, grabbing a few of the search results, modifying them and then a final re-upload back to Flickr. There were a few more challenging parts:
1. Grabbing the images.
2. Using PGraphics to modify them.
3. Uploading.

So i have created a simple sketch that does all of these, commented, and simple as possible so its easy to  understand. http://dl.dropbox.com/u/28052258/example_Flickr.zip

Some code snipits
1.
 if (millis() - lastTime > loopTime) //refresh images over loopTime
  {

    println("Getting images from flickr...");
    try
    {
      println(searchTerm);
      searchTerm = URLEncoder.encode(searchTerm, "UTF-8"); //encode string to UTF8
    }
    catch (Exception e) //maily for catching exceptions if string is null
    {
      e.printStackTrace();
      exit();
      return;
    }

    //create the search XML URL based on search term
    String url =  "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=bfaa37fbd00c1291ced65b903fb15fda&text="
      + searchTerm + "&format=rest";

    //process data into XML elelment from the URL above
    processing.xml.XMLElement xml = new processing.xml.XMLElement(this, url);

    xml = xml.getChild(0);
    processing.xml.XMLElement[] photos = xml.getChildren();

    //Everything until know only needed to be run once per search term.
    //the following gets each indiividual image. so its a loop
    for (int i = 0; i < searchQuantity; i++) //run until we reach the last image wanted
    {
      processing.xml.XMLElement photo = photos[i]; //grab the specific line for photo i
      //make the URL for that specific image
      String imgURL = "http://farm" + photo.getStringAttribute("farm")
        + ".static.flickr.com/"
          + photo.getStringAttribute("server") + "/"
          + photo.getStringAttribute("id") + "_"
          + photo.getStringAttribute("secret") + "_z.jpg"; //adding _z makes the image 640x480 instead of the smaller image.

      name[i] = photo.getStringAttribute("name"); //grab name from photo and put it in the name String array

      //status message
      print("downloading " + (i+1) + "/" + searchQuantity + ":");
      //paste this into the URL bar of you browser and you wll see the image
      println(imgURL); // the URL of the actaul image.

      //the actual retrieve image command.
      FlickrPics[i] = loadImage(imgURL);//display the images with mods


      if (i < 4)
      {
        //first row
        image(FlickrPics[i], 20 + (160 * i), 20, 160, 120); //image(PImage object to display,
        //image(PImage object to display,
        //x coord of the images left corner,
        //y ---------------------right corner,
        //width to resize to,    //i just m
        //height to resize to); ade it a fourth of full size
      }
      else
      {
        //second
        image(FlickrPics[i], 20 + (160 * (i - 4)), 140, 160, 120);
      }
    }

    modifyImages();
    UploadImages(); // call the uplaod function and upload the images to flickr/save to hdd

    for (int i = 0; i < searchQuantity; i++) //update images again with new pgraphics
    {
      if (i < 4)
      {
        //first row
        image(FlickrPics[i], 20 + (160 * i), 20, 160, 120); //image(PImage object to display,
        //image(PImage object to display,
        //x coord of the images left corner,
        //y ---------------------right corner,
        //width to resize to,    //i just m
        //height to resize to); ade it a fourth of full size
      }
      else
      {
        //second
        image(FlickrPics[i], 20 + (160 * (i - 4)), 140, 160, 120);
      }
    }
  }



2.
 void modifyImages()
{
  for (int i=0; i < searchQuantity; i++) //loop through each image adding the img to it
  {
    println("modifying FlickrPics["+ i+ "]");
    PGraphics output = createGraphics(640, 480, P2D);

    //start to draw on the PGraphics
    output.beginDraw();

    output.image(FlickrPics[i], 0, 0); //640x480
    output.image(img, 0, 0); //640x480
    //both are 640x480 so ill added them both in the upper left corcner with 0,0

      output.endDraw(); //done drawing

    FlickrPics[i] = output.get(); // saves the new iamge back to the source
  }
}


3.

 void UploadImages()
{
  for (int i=0; i < searchQuantity; i++)
  {
    // First compress it as a jpeg.
    byte[] compressedImage = compressImage(FlickrPics[i]);

    // Set some meta data.
    UploadMetaData uploadMetaData = new UploadMetaData(); 
    uploadMetaData.setTitle(uploadTitle); 
    uploadMetaData.setDescription(uploadDescription);   
    uploadMetaData.setPublicFlag(true);

    String saveName = "/created/" + "Tag.Grind" + i + ".jpg";
    try
    {
      //FlickrPics[i] = 
      FlickrPics[i].save(saveName);
    }
    catch (Exception e)
    {
      println("Local Save failed");
    }

    // Finally, upload/
    try
    {
      uploader.upload(compressedImage, uploadMetaData);
      println("Picture" + " " + (i + 1) + " uploaded");
    }
    catch (Exception e)
    {
      println("Upload failed");
    }
  }
  println("Upload Complete!");
}


No comments:

Post a Comment