Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. import java.util.List;
  2. import java.io.FileFilter;
  3. import java.io.FilenameFilter;
  4. import processing.pdf.*;
  5. boolean record;
  6.  
  7.  
  8.  
  9. final int len=25;
  10. final int NMAX=10;
  11.  
  12. PImage pic;
  13.  
  14. ArrayList<PImage> imgContainer;
  15. int n=NMAX;
  16.  
  17.  
  18. int value = 250;
  19. float resizeX, resizeY;
  20.  
  21. void setup() {  
  22.   size(800, 800, FX2D);
  23.   resizeX=0.4;
  24.   resizeY=0.4;
  25.   imageMode(CENTER);
  26.   colorMode(RGB, 255);
  27.   background(250, 250, 250);
  28.   rectMode(CENTER);
  29.   //imageMode(CENTER);
  30.  
  31.   pic=loadImage("hand.jpg");
  32.   pic.resize(width, height);
  33.   //c1 = color(200, 25, 25);
  34.   //c2 = color(25, 255, 200);  
  35.  
  36.   loadAllMiniImages();
  37.  
  38.   println("It has been chosen to work with "+n+" images. Check="+imgContainer.size());
  39.  
  40.   noLoop();
  41.   noStroke();
  42. }
  43.  
  44.  
  45. void draw() {
  46.  
  47.   background(200);
  48.   pic.loadPixels();
  49.  
  50.   for (int y = 0; y < height; y+=24) {
  51.     for (int x = 0; x < width; x+=24) {
  52.  
  53.       int index=y*width+x;
  54.       color pixelValue = pic.pixels[index];
  55.       color rgb = pixelValue;
  56.       int r = (rgb >> 16) & 0xFF;  // Faster way of getting red(argb)
  57.       int g = (rgb >> 8) & 0xFF;   // Faster way of getting green(argb)
  58.       int b = rgb & 0xFF;  
  59.  
  60.       //How far is the current color from white
  61.       float dista=dist(r, g, b, 255, 255, 255);
  62.  
  63.       //50 is a threshold value allowing close to white being identified as white
  64.       //This value needs to be adjusted based on your actual background color
  65.       //Next block is processed only if the pixel not white
  66.       if (dista>0) {    
  67.         float pixelBrightness = brightness(pixelValue);
  68.         int imgPicked=(int)random(n);
  69.         PImage selected=imgContainer.get((int)imgPicked);
  70.         PImage acopy=selected.get();
  71. int val1=(int)(acopy.width*resizeX);
  72. int val2=(int)(acopy.height*resizeY);
  73. if (val1>1 || val2>1) {
  74.     acopy.resize(val1,val2);
  75.   }
  76.         pushMatrix();
  77. float rotZ = radians(random(360));  //float rotZ = map(mouseX,0,width,0,TWO_PI);  //USE this for a different effect
  78. translate(x,y);
  79. rotate(rotZ);  
  80. image(acopy,0,0);
  81. popMatrix();
  82.       }
  83.     }
  84.   }
  85. }
  86. void keyPressed() {
  87.   if (keyCode==UP) {
  88.     resizeX+=0.05;
  89.     resizeY+=0.05;
  90.   }
  91.   if (keyCode==DOWN) {
  92.     resizeX-=0.05;
  93.     resizeY-=0.05;
  94.   }
  95.  
  96.   resizeX=constrain(resizeX, 0, 1.5);
  97.   resizeY=constrain(resizeY, 0, 1.5);
  98.   surface.setTitle("Size= "+resizeX);
  99.   redraw();
  100. }
  101.  
  102.  
  103. void mouseReleased() {
  104.   redraw();
  105.   saveFrame();
  106.     }
  107.  
  108.  
  109.  
  110.  
  111. final FileFilter FOLDER_FILTER = new FileFilter() {
  112.   @ Override boolean accept(final File path) {
  113.     return path.isDirectory();
  114.   }
  115. };
  116.  
  117. final FilenameFilter PIC_FILTER = new FilenameFilter() {
  118.   final String[] exts = {
  119.     ".png"  //, ".jpg", ".jpeg", ".gif"
  120.   };
  121.  
  122.   @ Override boolean accept(final File path, String name) {
  123.     name = name.toLowerCase();
  124.     for (final String ext : exts)  if (name.endsWith(ext))  return true;
  125.     return false;
  126.   }
  127. };
  128.  
  129.  
  130. void loadAllMiniImages() {
  131.  
  132.   File dataFolder = dataFile("image_segments");
  133.   File[] imgDirs = dataFolder.listFiles(FOLDER_FILTER);
  134.   imgDirs = (File[]) append(imgDirs, dataFolder);
  135.  
  136.   println(imgDirs.length, "folders found:");
  137.   printArray(imgDirs);
  138.  
  139.  
  140.   List<File[]> imgPaths = new ArrayList<File[]>();
  141.   for (File dir : imgDirs)  imgPaths.add(dir.listFiles(PIC_FILTER));
  142.  
  143.   println("\nImage paths found for all folders:");
  144.  
  145.   int totalLength = 0;
  146.   for (File[] paths : imgPaths) {
  147.     totalLength += paths.length;
  148.     println();
  149.     //printArray(paths);
  150.   }
  151.  
  152.   println("Total images found in all subfolders:", totalLength);
  153.  
  154.  
  155.  
  156.  
  157.   //NOW only process up to NMAX images if there are more than NMAX images.
  158.   //Otherwise only use the images available.
  159.   if (totalLength>NMAX) {
  160.     n=NMAX;
  161.   } else {
  162.     n=totalLength;
  163.   }
  164.  
  165.  
  166.   imgContainer=new ArrayList<PImage>();
  167.  
  168.   int ctr=0;
  169.   for (File[] paths : imgPaths) {
  170.     for (File f : paths) {
  171.       PImage pimg1=loadImage(f.getPath());
  172.       println("["+ctr+"] "+f.getPath()+" Dimensions="+pimg1.width+"x"+pimg1.height);
  173.  
  174.       imgContainer.add(pimg1);
  175.       ctr++;
  176.  
  177.       //Only load 50 images
  178.       if (ctr==n)
  179.         break;
  180.     }
  181.   }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement