Advertisement
xeromino

images

May 13th, 2016
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. XML root, channel, item, description, title;
  2. XML[] items;
  3. String[] urls;
  4.  
  5. PImage [] imgs;
  6. PImage a;
  7. int c, counter;
  8. boolean portrait;
  9.  
  10. void setup() {
  11.   size(1024, 1365);
  12.   colorMode(HSB);
  13.   a = loadImage("1.jpg"); // reference image
  14.   portrait=a.width<a.height?true:false;
  15.  
  16.   importURLs();
  17.   importResizeImages();
  18.   drawStuff();
  19. }
  20.  
  21. void draw() {
  22. }
  23.  
  24. void keyPressed() {
  25.   if (key == 's') save(random(344)+".png");
  26. }
  27.  
  28. void importURLs() {
  29.   //root = loadXML("https://www.pinterest.com/xeronimo/famous-people-monochrome.rss");
  30.   root = loadXML("https://www.pinterest.com/xeronimo/those-bipeds-known-as-humans.rss");
  31.   channel = root.getChild("channel");
  32.   items = channel.getChildren("item");
  33.   urls = new String[items.length];
  34.   imgs = new PImage[items.length];
  35.   for (int i=0; i<items.length; i++) {
  36.     description = items[i].getChild("description");
  37.     title = items[i].getChild("title");
  38.     String d = description.getContent();
  39.     String start = "https://";
  40.     String end = ".jpg";
  41.     int indexStart = d.indexOf(start);
  42.     int indexEnd = d.indexOf(end);
  43.     String descr = d.substring(indexStart, indexEnd+4);
  44.     urls[i] = descr;
  45.   }
  46. }
  47.  
  48. void importResizeImages() {
  49.   counter = -1;
  50.   PImage temp = createImage(width, height, RGB);
  51.   for (int i=0; i<urls.length; i++) {
  52.     PImage b = loadImage(urls[i]);
  53.     boolean portrait2=b.width<b.height?true:false;
  54.     if (portrait2 == portrait) {
  55.       counter++;
  56.       if (portrait) {
  57.         float ratio = (float) a.height/b.height;
  58.         int w = int (b.width*ratio);               // calculate b's new width in relation to a's height
  59.         if (w<a.width) ratio *= (float) a.width/w; // if b's new width does not reach to the edge then calculate and add missing ratio
  60.         int h = int (b.height*ratio);              // calculate b's updated height
  61.         w = int (b.width*ratio);                   // calculate b's updated, now fitting, width
  62.         temp.copy(b, 0, 0, b.width, b.height, 0, 0, w, h);
  63.       } else {
  64.         float ratio = (float) a.width/b.width;
  65.         int h = int (b.height*ratio);
  66.         if (h<a.height) ratio *= (float) a.height/h;
  67.         int w = int (b.width*ratio);
  68.         h = int (b.height*ratio);  
  69.         temp.copy(b, 0, 0, b.width, b.height, 0, 0, w, h);
  70.       }
  71.       imgs[counter] = temp.get();
  72.     }
  73.   }
  74. }
  75.  
  76. void drawStuff() {
  77.   for (int x=0; x<width; x++) {
  78.     for (int y=0; y<height; y++) {
  79.       search(x, y);
  80.     }
  81.   }
  82. }
  83.  
  84. void search(int x, int y) {
  85.   color best = color(0);
  86.   float cost = 360;
  87.   color baseColor = a.get(x, y); // 90
  88.   for (int i=0; i<counter; i++) {
  89.     color candidate = imgs[i].get(x, y); // 40
  90.     float diff = abs(brightness(baseColor)-brightness(candidate)); // 50
  91.     if (diff < cost) {
  92.       best = candidate;
  93.       cost = diff;
  94.     }
  95.   }
  96.   if (brightness(best)<50) best = color(hue(best), saturation(best), brightness(best)-25);
  97.   stroke(best);
  98.   point(x, y);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement