Created
January 5, 2016 06:32
-
-
Save elvismdev/84d8cac15cb420246efd to your computer and use it in GitHub Desktop.
A Java small class to invert colors in image files. The library edu.duke is a dependency for the class to work, it should be added into the Java IDE to compile with no errors. Download link http://www.dukelearntoprogram.com/downloads/archives/courserajava.jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Inverts colors in a image file (photographic negative). | |
* | |
* @author (Elvis Morales) | |
* @version (1.0) | |
*/ | |
import edu.duke.*; | |
import java.io.File; | |
public class BatchInversions { | |
public ImageResource makeInversion(ImageResource inImage) { | |
ImageResource outImage = new ImageResource(inImage); | |
for (Pixel pixel: outImage.pixels()) { | |
Pixel inPixel = inImage.getPixel(pixel.getX(), pixel.getY()); | |
int invRed = 255 - inPixel.getRed(); | |
int invGreen = 255 - inPixel.getGreen(); | |
int invBlue = 255 - inPixel.getBlue(); | |
pixel.setRed(invRed); | |
pixel.setGreen(invGreen); | |
pixel.setBlue(invBlue); | |
} | |
return outImage; | |
} | |
public void selectAndConvert() { | |
DirectoryResource dr = new DirectoryResource(); | |
for (File f : dr.selectedFiles()) { | |
ImageResource inImage = new ImageResource(f); | |
ImageResource imageInv = makeInversion(inImage); | |
imageInv.setFileName("inverted-" + inImage.getFileName()); | |
imageInv.draw(); | |
imageInv.save(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment