20 januari 2010

Load images from disk into sketch

Ruben here some basic code to read files from your folder into your sketch. Every 10 sec. a new image is read. Be aware to have a files.txt file with one line for every image in the folder.

PImage img;
String[] lines;
String filename = "files.txt";
int sec;
int posx = 240;

void setup() {
  size(800,600);
  smooth();
  sec = second();
  background(255);
}

void draw() {
   if (frameCount % 600 == 0) {
    posx = 240;
    sec = second();
    lines = loadStrings(filename);
    int p = int(random(lines.length));
    img = loadImage("img_" + str(p) + ".jpg");
  }
  if (img != null) {
    image(img,-560 + (posx / 4),0,800,600);
    image(img,posx,0,800,600);
    posx ++;
  }
}

The files.txt (if you add a file to the folder, add a line with a follow up number to this file).
0
1
2
3
4

Processing and Arduino

I ripped this from an Arduino example. It's very basic but works quite well. Download the lates Arduino software, download the latest Processing Arduino class and add an Arduino board with one LED at pin 13.
For Paco!

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;

color off = color(4, 79, 111);
color on = color(84, 145, 158);

int[] values = { Arduino.LOW, Arduino.LOW, Arduino.LOW, Arduino.LOW,
 Arduino.LOW, Arduino.LOW, Arduino.LOW, Arduino.LOW, Arduino.LOW,
 Arduino.LOW, Arduino.LOW, Arduino.LOW, Arduino.LOW, Arduino.LOW };

void setup() {
  size(470, 200);

  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[1]);
  
  for (int i = 0; i <= 13; i++) {
    arduino.pinMode(i, Arduino.OUTPUT);
  }
}

void draw() {
  background(off);
  stroke(on);
  
  for (int i = 0; i <= 13; i++) {
    if (values[i] == Arduino.HIGH)
      fill(on);
    else
      fill(off);
      
    rect(420 - i * 30, 30, 20, 20);
  }
}

void mousePressed()
{
  int pin = (450 - mouseX) / 30;
  println(pin);
  if (values[pin] == Arduino.LOW) {
    arduino.digitalWrite(pin, Arduino.HIGH);
    values[pin] = Arduino.HIGH;
  } else {
    arduino.digitalWrite(pin, Arduino.LOW);
    values[pin] = Arduino.LOW;
  }
}

Real time video processing (2)

Here you separate light and dark and sort the pixels likewise. For Sarah and Hsien!
import processing.video.*;
Movie myMovie;

void setup() {
  size(200,150, P2D);
  myMovie = new Movie(this, "mymovie.mov");
  myMovie.loop();
}

void draw() {
  if(myMovie.available()) {
    myMovie.read();
  }
  image(myMovie, 0, 0);

  loadPixels();

  int[] p = {};
  for (int i = 0; i < width * height; i++) {
    color c1 = pixels[i];
    int br = int(brightness(c1));
    if (br > 50) {
      p = append(p, 1);
    }
  }
  
  for (int w = 0; w < width * height; w++) {
    if (w < p.length) {
      pixels[w] = color(255,255,255);
    } else {
      pixels[w] = color(0,0,0);
    }
  }
  updatePixels();
}

XML and Mobypicture

Adri this one is for you. Every half a minute, a picture is retrieved from Mobypicture and stored on disk. Every ten seconds, an image will be shown.
String[] data;
String apiKey = "your-key-here";
String search = "searchterm";
String url = "http://api.mobypicture.com";
String getString = "?action=searchPosts&format=xml&searchItemesPerPage=10"
+ "&searchPage=1&"
+ "searchMediaPhoto=1&searchTags="+search+"&k=" + apiKey;
boolean load = false;
XMLElement xml;
PImage img;
PImage img2;
int j = 0;
int sec = 0;

void setup() {
  size(800,600);
  background(255);
  sec = second();
}

void draw() {
  background(255);
  if (second() % 30 == 0 && load == false) {
    load = true;
    getData();
  }
  if (j > 0 && second() % 10 == 0  && sec != second()) {
    sec = second();
    int p = int(random(j));
    img2 = loadImage("img_" + str(p) + ".jpg");
  }
  if (null != img2) {
    image(img2,0,0,800,600);
  }
}

void getData() {
  data = loadStrings(url + getString);
  if (null != data) {
    String datastring = join(data,"");
    
    // get the right xml node
    xml = new XMLElement(datastring);
    XMLElement[] childs = xml.getChildren();
    XMLElement results = childs[2];
    XMLElement[] result = results.getChildren();
    int i = int(random(25));
    XMLElement item = result[i];
    XMLElement post = item.getChild(1);
    XMLElement media = post.getChild(7);
    XMLElement url_full = media.getChild(2);

    // print the full path
    println(url_full.getContent());

    // load image and save it
    img = loadImage(url_full.getContent(), "jpg");
    img.save("img_" + str(j) + ".jpg");

    // housekeeping
    j++;
    img = null;
    load = false;
  }
}

A Bug Class

Bug bug;
Bug bug2;

void setup() {
  size(300,300);
  smooth();
  bug = new Bug(150,150);
  bug2 = new Bug(100,190);
  ellipseMode(CENTER);
  rectMode(CENTER);
}

void draw() {
  background(0);
  fill(255);
  ellipse(mouseX,mouseY,100,100);
  bug.display();
  bug.move();
  bug2.display();
  bug2.move();
}
Second file:
class Bug {
  int diam = 10;
  float x;
  float y;
  int xd = 1;
  int yd = 1;
  int mousemargin = 50;

  Bug(int x, int y) {
    this.x = x;
    this.y = y;
  }

  void move() {
    if (x > mouseX && y > mouseY) {
      if (pythagoras(x,y) <= mousemargin) {
        xd = 2;
        yd = 2;
        x += xd;
        y += yd;
      } else {
        x += xd;
        y += yd;
      }
    } else if (x > mouseX && y <= mouseY) {
      if (pythagoras(x,y) < mousemargin) {
        xd = 2;
        yd = 2;
        yd = yd * -1;
        x += xd;
        y += yd;
      } else {
        x += xd;
        y += yd;
      }
    } else if (x <= mouseX && y > mouseY) {
      if (pythagoras(x,y) < mousemargin) {
        xd = 2;
        yd = 2;
        xd = xd * -1;
        x += xd;
        y += yd;
      } else {
        x += xd;
        y += yd;
      }
    } else if (x <= mouseX && y <= mouseY) {
      if (pythagoras(x,y) < mousemargin) {
        if (xd > 0) {
          xd = xd * -1;
          yd = yd * -1;
          xd = 2;
          yd = 2;
        } else {
          xd = -2;
          yd = -2;
        }
        x += xd;
        y += yd;
      } else {
        x += xd;
        y += yd;
      }
    }

    if (y + diam >= height) {
      if (yd > 0) {
        yd = yd * -1;
      }
    }
    if (x + diam >= width) {
      if (xd > 0) {
        xd = xd * -1;
      }
    }
    if (y - diam < 0) {
      if (yd < 0) {
        yd = yd * -1;
      }
    }
    if (x - diam < 0) {
      if (xd < 0) {
        xd = xd * -1;
      }
    }
  }

  void display() {
    fill(0);
    ellipse(this.x, this.y, diam * 2, diam * 2);
  }
  
  float pythagoras(float a, float b) {
    float aa,bb,cc;
    aa = abs(mouseX - a);
    bb = abs(mouseY - b);
    cc = sq(aa) + sq(bb);
    return sqrt(cc);
  }
}

Real time video processing

This one is for Sarah and Hsien so you can process videos real time.
import processing.video.*;

Movie myMovie;

void setup() {
  size(320,240, P2D);
  myMovie = new Movie(this, "mymovie.mov");
  myMovie.loop();
}

void draw() {
  if(myMovie.available()) {
    myMovie.read();
  }
  image(myMovie, 0, 0);

  loadPixels();
  changeColor();
}

void changeColor() {
  for (int i = 0; i < width * height; i++) {
    color p = pixels[i];
    all2RGB(p,i);
  }
}

void all2RGB(color p, int i) {
    float r = red(p);
    float g = green(p);
    float b = blue(p);
    if (r >= g && r >= b) {
      pixels[i] = color(255,0,0);
      return;
    }
    if (g >= r && g >= b) {
      pixels[i] = color(0,255,0);
      return;
    }
    if (b >= r && b >= g) {
      pixels[i] = color(0,0,255);
      return;
    }
}