Skip to content

Instantly share code, notes, and snippets.

@elvismdev
Created January 5, 2016 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elvismdev/84d8cac15cb420246efd to your computer and use it in GitHub Desktop.
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
/**
* 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