What is Food to You?

Flickr associating Charred Octopus with Wilting Death and Dismay

Description

For my Image exercise, I chose to explore how different sources can provide incredibly distinct results when presented with the same information. I specifically am concerned with the perception of Food in the context of PunchFork.com and Flickr.com.

 

The concept driving my work stems from the modern phenomena of highly categorized repositories of digital media. Specifically, the question I asked through this exercise is “What is Food to You?”, which, through the medium of IMAGE, I take to more explicitly ask, “Given a search term, what does an opinionated data source return?”

As more and more of the resources we interact with on a daily basis are served over the Internet, the implication follows that some system must be processing that data in order to categorize and understand it. In order to explore the relationship between categorical schemes, I used two images, related only through a query expression.

The first image comes from the PunchFork.com API as a companion to a randomly served recipe. This image represents a resource that guarantees food related result and thus, in the context of all possible internet images, can be considered highly and accurately categorized. The second image comes from a Flickr.com API query based on the title of the random recipe. Then, from a set of 500 images, one is chosen at random as a companion to the recipe image. This image represents a resource that has been categorized by some number of unknown sources. In this way, it represents an entity that though highly categorized, may or may not been an accurate reflection of its assigned role.

In addition, a third image is displayed that shows a more mathematical, yet still artistic exploration of the concept. The third image is simply a composite of the two above, however at the points where they overlap, the different between the RGB values has been computed. In this way, a literal manifestation of the difference between PunchFork.com  and Flickr.com exists.

From a technical point of view, the code is fairly simple. I first set up the infrastructure required to make HTTP requests to both of the APIs, convert the response messages to JSON, build URLs for the images, and create PImage references to each. I then position and display each image, accompanied by its title and a minimal frame. I then create the difference image by iterating through both images, finding the difference between overlapping pixels, and finally positioning and displaying it below.

Overall, I am fairly satisfied with my results. The artistic concept certainly comes across, though the randomness of the content generation lends itself to image pairs and differences that aren’t incredibly interesting. I have however, posted one of the more interesting pairings along with this post, as an example of the interesting results that can come out of this process.

Additional Images and Files

Project Zip File

Braised Lamb

 

 

Code

/**
*    Max Seiden - Adaptive Art F11
*    Exercise #1
*
*    More comments are on the way!
*
*/

/*
*    You need to import the org.json.* library to
*    use this code. If you shoot me an email, I
*    can send you the files, seeing at it was a
*    small pain to find them and get it working.
*/
import org.json.*;
import java.net.*;
import java.util.*;

Random rand = new Random();
PImage diff;
PImage diffTemp;
PImage f_img;
String f_title;
PImage p_img;
String p_title;
String[] p_words;
String terms;

/**
*
*    Makes a generic HTTP request to the given path
*    Returns the HTTP response
*
*/
String httpRequest(String path)
{
  String line = "";
  try
  {
    URL url = new URL(path);
    HttpURLConnection url_c = (HttpURLConnection)url
        .openConnection();
    url_c.setRequestMethod("GET");
    url_c.connect();

    InputStream url_is = url_c.getInputStream();
    InputStreamReader url_isr = new InputStreamReader(url_is);
    BufferedReader url_br = new BufferedReader(url_isr);

    String temp = "";
    while((temp = url_br.readLine()) != null)
    {
       line = line.concat(temp);
    }
  }
  catch(Exception e)
  {
    println(e);
  }
  return line;
}

/**
*
*    Takes a JSON string as input
*    Returns a URL for a random recipe
*    Splits the recipe title (delim @ ' ')
*
*/
String getRandPF(String PunchFork)
{
  String result = "";
  try
  {
    JSONObject json = new JSONObject(PunchFork);
    json = json.getJSONObject("recipe");
    result = json.getString("source_img");
    p_title = json.getString("title");
    p_words = p_title.split(" "); 

  }
  catch(Exception e)
  {
   println(e);
  }
  return result;
}

/**
*
*    Takes a JSON string as input
*    Returns the URL of a random Flickr image
*
*/
String getRandFlickr(String Flickr)
{
  String result = "";
  try
  {
    JSONObject json = new JSONObject(Flickr);
    json = json.getJSONObject("photos");
    JSONArray photos = json.getJSONArray("photo");

    int val = (int)(rand.nextDouble() * photos.length());
    JSONObject photo = photos.getJSONObject(val);

    String photoURL = "";
    f_title = photo.getString("title");
    photoURL = photoURL
        .concat("http://farm")
        .concat(photo.getString("farm"));
    photoURL = photoURL
        .concat(".static.flickr.com/")
        .concat(photo.getString("server"));
    photoURL = photoURL
        .concat("/")
        .concat(photo.getString("id"))
        .concat("_")
        .concat(photo.getString("secret"))
        .concat(".jpg");

    result = photoURL;
  }
  catch(Exception e)
  {
   println(e);
  }
  return result;
}

/**
*
*    Complete unit for loading a PF recipe
*
*/
void loadPunchFork()
{
  String urlP = "http://api.punchfork.com/random_recipe?"
      + "key=7b798ff4c43161f4";
  String jsonP = httpRequest(urlP);
  String p = getRandPF(jsonP);
  p_img = loadImage(p);
}

/**
*
*    Complete unit for loading a Flickr image
*
*/
void loadFlickr()
{
  String urlF = "http://api.flickr.com/services/rest/?"
      + "method=flickr.photos.search&safe_search=2&"
      + "per_page=500&api_key="
      + "df618ed14e655109087925ee1f65e2d1&tags="
      + p_words[0]
      + "&format=json&nojsoncallback=1";
  String jsonF = httpRequest(urlF);
  String f = getRandFlickr(jsonF);
  f_img = loadImage(f);
}

/**
*
*    Required setup function
*
*/
void setup()
{
  size(1280, 800);

  loadPunchFork();
  fill(30);
  rect(10, 10, 550, 350);

  loadFlickr();
  fill(30);
  rect(720, 10, 550, 350);

  drawPF();
  drawF();
  drawComp();
}

/**
*
*    Positions the PF image and title
*
*/
void drawPF()
{
  if(p_img.width > 530)
  {
    p_img.resize(530, 0);
  }

  if(p_img.height > 300)
  {
    p_img.resize(0, 300);
  }

  int valH = (300 - p_img.height);
  int valW = (530 - p_img.width)/2;
  p_img.loadPixels();
  image(p_img, 20+valW, 50+valH); 

  fill(255);
  textSize(20);
  textAlign(CENTER);
  text(p_title, 10, 15, 550, 40);
}

/**
*
*    Positions the Flickr image and title
*
*/
void drawF()
{

  {
    f_img.resize(530, 0);
  }

  if(f_img.height > 300)
  {
    f_img.resize(0, 300);
  }

  int valH = (300 - f_img.height);
  int valW = (530 - f_img.width)/2;
  f_img.loadPixels();
  image(f_img, 730+valW, 50+valH);

  fill(255);
  textSize(20);
  textAlign(CENTER);
  text(f_title, 720, 15, 550, 40);
}

/**
*
*    Processes the difference image
*    Positions the image
*
*/
void drawComp()
{
  diff = createImage(530, 300, RGB);
  diffTemp = createImage(530, 300, RGB);
  int i = 0;
  while(i < (diff.width * diff.height))
  {
    diff.pixels[i] = color(255, 255, 255);
    diffTemp.pixels[i] = color(255, 255, 255);
    i++;
  }

  int diffW = (530 - p_img.width)/2;
  int diffH = (300 - p_img.height)/2;
  diff.copy(p_img, 0, 0, p_img.width, p_img.height,
      diffW, diffH, p_img.width, p_img.height);

  diffW = (530 - f_img.width)/2;
  diffH = (300 - f_img.height)/2;
  diffTemp.copy(f_img, 0, 0, f_img.width, f_img.height,
      diffW, diffH, f_img.width, f_img.height);

  i--;
  while(i >= 0)
  {
   color A = diff.pixels[i];
   int A_val = unhex(hex(A));
   color B = diffTemp.pixels[i];
   int B_val = unhex(hex(B));

   A_val = abs(A_val - B_val);
   diff.pixels[i] = color(A_val/2);
   i--;
  }

  image(diff, (1280-530)/2, 450);
}

 

This entry was posted in Exercise 1. Bookmark the permalink.

Leave a Reply