Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 27, 2023 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/106a444035bb986e4e29211350868cbc to your computer and use it in GitHub Desktop.
Save aspose-com-gists/106a444035bb986e4e29211350868cbc to your computer and use it in GitHub Desktop.

Aspose.Imaging Java API allows to easy grayscale your images or photos in your Java application or Web service.

You can:

  • easily grayscale (make only black-white) your images and save to specified format.

Interested ?

You may go further at : https://products.aspose.com/imaging/java/

import com.aspose.imaging.IMultipageImage;
import com.aspose.imaging.Image;
import com.aspose.imaging.RasterImage;
import com.aspose.imaging.imageoptions.PngOptions;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
grayscale();
public static void grayscale()
{
filterImages(RasterImage::grayscale, "grayscale");
}
static String templatesFolder = "D:\\";
public static void filterImages(Consumer<RasterImage> doFilter, String filterName)
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng", "dicom",
"jp2", "j2k", "tga", "webp", "tif", "gif", "ico");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps", "wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(
formatExt ->
{
String inputFile = templatesFolder + "template." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder + String.format("%s_%s.png", filterName, formatExt);
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
doFilter.accept(image);
//If image is multipage save each page to png to demonstrate results
if (image instanceof IMultipageImage && ((IMultipageImage) image).getPageCount() > 1)
{
IMultipageImage multiPage = (IMultipageImage) image;
final int pageCount = multiPage.getPageCount();
final Image[] pages = multiPage.getPages();
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++)
{
String fileName = String.format("%s_page%d_%s.png", filterName, pageIndex, formatExt);
pages[pageIndex].save(fileName, new PngOptions());
}
}
else
{
image.save(outputFile, new PngOptions());
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
);
}
private static String rasterizeVectorImage(String formatExt, String inputFile)
{
String outputFile = templatesFolder + "rasterized."+ formatExt + ".png";
try (Image image = Image.load(inputFile))
{
image.save(outputFile, new PngOptions());
}
return outputFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment