Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.Files
- import java.io.File
- import javax.imageio.ImageIO
- def avgDiffSq(file: File): Double = {
- val image = ImageIO.read(file)
- val data = image.getRGB(0, 0, image.getWidth, image.getHeight, null, 0, image.getWidth)
- var i = 0
- var sum = 0.0
- while(i < data.size) {
- val c = data(i)
- val r = (c&0xFF0000)>>16; val g = (c&0xFF00)>>8; val b = c&0xFF
- val d = (math.abs(r-g) + math.abs(r-b)).toDouble
- sum += d*d
- i += 1
- }
- math.sqrt(sum / data.size)
- }
- var count = 0
- new File(".").listFiles.foreach {file =>
- if(file.getName.endsWith(".jpg") || file.getName.endsWith(".jpeg") || file.getName.endsWith(".png")) {
- try {
- val diff = avgDiffSq(file)
- val dir = if(diff < 5) "bw"
- else if(diff < 20) "maybe"
- else "color"
- file.renameTo(new File(dir + "/" + file.getName))
- count += 1
- println(count)
- } catch {
- case e: Exception => println(file.getName); println(e)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement