Today, the teacher wanted me to help replace their more than 200 photos with white background. Most of the photos are blue and red.
Use ps? No! Use java! !
Yes, my first reaction was to use java and find a source code for obtaining image pixels on the Internet. After making a move, I made it up and it took half a minute to process more than 200 photos.
/** * Function: * Batch convert blue-bottomed ink lights to white-bottomed* Mainly it can be processed in hundreds or thousands of pictures* @author Zhao Junfu* */ public class ImageColorDemo { public static void main(String args[]) throws IOException { /** * Image directory to be processed*/ File dir = new File("d:/d4"); /** * List the pictures in the directory and get the array*/ File[] files = dir.listFiles(); /** * Traversing the array*/ for(int x=0;x<files.length;x++){ /** * Define an RGB array, because the RGB pattern of the image is represented by three 0-255, such as white is (255,255,255) */ int[] rgb = new int[3]; /** * Buffered stream used to process the image*/ BufferedImage bi = null; try { /** * Use ImageIO to read the image into the buffer*/ bi = ImageIO.read(files[x]); } catch (Exception e) { e.printStackTrace(); } /** * Get the length and width of the image*/ int width = bi.getWidth(); int height = bi.getHeight(); int minx = bi.getMinX(); int miny = bi.getMinY(); System.out.println("In process:"+files[x].getName()); /** * Here is the pixels of the image. Because the back color of the image is to be processed, the color on the specified pixel needs to be replaced with the target color* Here is a second-layer loop, traversing each pixel on length and width*/ for (int i = minx; i < width; i++) { for (int j = miny; j < height; j++) { // System.out.print(bi.getRGB(jw, ih)); /** * Get the RGB value on the specified pixel (i, j), */ int pixel = bi.getRGB(i, j); /** * Perform bit operations separately to obtain the value on rgb*/ rgb[0] = (pixel & 0xff0000) >> 16; rgb[1] = (pixel & 0xff000) >> 8; rgb[2] = (pixel & 0xff); /** * Perform color change operation, I want to change the blue base to white base, so I will determine whether the rgb value in the picture is in the blue range of pixels*/ if(rgb[0]<155&&rgb[0]>0 && rgb[1]<256&&rgb[1]>105 &&rgb[2]<256&&rgb[2]>105 ){ /** * Here is the judgment passed, then change the pixel to white */ bi.setRGB(i, j, 0xffffff); } } } System.out.println("/t processing is completed: "+files[x].getName()); System.out.println(); /** * Save the buffer object to a new file*/ FileOutputStream ops = new FileOutputStream(new File("d:/d5/"+x+".jpg")); ImageIO.write(bi,"jpg", ops); ops.flush(); ops.close(); } } } The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.