Skip to content

Instantly share code, notes, and snippets.

@kartikmaji
Last active August 29, 2015 14:11
Show Gist options
  • Save kartikmaji/e9cc8ba8008d1ee0c781 to your computer and use it in GitHub Desktop.
Save kartikmaji/e9cc8ba8008d1ee0c781 to your computer and use it in GitHub Desktop.
A Java Program to select random images from a large bucket of images.
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
public class MyProfilePicture {
public static int num;
public static void main(String[] args) throws IOException
{
num=0;
File dir = new File("/home/kartik/Desktop");
SearchRecursively(dir); //Iterates on the given directory recursively
}
private static void SearchRecursively(File dir) throws IOException
{
File[] files = dir.listFiles();
for (File file : files)
{
if (file.isDirectory())
{
SearchRecursively(file); // Recursively calling SearchRecursively to iterate inside the given directory
}
else // If the file is not a directory
{
num++;
try
{
Image image=ImageIO.read(file);
if (image!=null) // checks if file is an image
{
if(num%10==0) // if the image index is a multiple of 10 then copy to a target directory
{
String name = file.getName();
File Target = new File("/home/kartik/mosaic/"+name);
FileUtils.copyFile(file, Target);
System.out.println("File copied"+file.getName());
}
}
}
catch(IOException e)
{
System.out.println("Same File encountered");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment