Created
September 12, 2014 15:33
-
-
Save Sythelux/aa943d78279ca25505b1 to your computer and use it in GitHub Desktop.
A Script that can convert White to Transparency, as good as such a thing can be.
This file contains hidden or 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
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
public class WhiteToTransparent { | |
public static void main(String[] args) { | |
if (args.length < 2) { | |
System.out.println("usage: java WhiteToTransparent <sourceFile>.png <destFile>.png -i"); | |
System.out.println("-i: inverted"); | |
System.exit(2); | |
} | |
boolean inverted = (args.length > 2 && args[2].equals("-i")) ? true : false; | |
File source = new File(args[0]); | |
File dest = new File(args[1]); | |
if (dest.exists()) { | |
dest.delete(); | |
} | |
if (!source.exists()) { | |
System.out.println(source + " could not be found! exiting."); | |
System.exit(3); | |
} | |
try { | |
int alpha = 0; | |
BufferedImage bim = ImageIO.read(source); | |
for (int x = 0; x < bim.getWidth(); x++) { | |
for (int y = 0; y < bim.getHeight(); y++) { | |
Color c = new Color(bim.getRGB(x, y)); | |
if (inverted) alpha = ((c.getBlue() + c.getGreen() + c.getRed()) / 3); | |
else alpha = 255 - ((c.getBlue() + c.getGreen() + c.getRed()) / 3); | |
bim.getAlphaRaster().setSample(x, y, 0, alpha); | |
} | |
} | |
ImageIO.write(bim, "png", dest); | |
System.exit(0); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment