How to filter an image
How to filter an image
With Aspose.Imaging you can apply filters with different filtering options like: `Sharpen`, `MotionWiener`, `BilateralSmoothing`, `GaussBlur` and `Median` to edit a photo or image. Using a rectangle filter creates the effect of a special glass put before a camera through which we can see the picture. To apply a rectangle filter an image passes the `Rectangle` object as a first parameter of the Filter method and the second parameter a filter type options. Applying the filter to a vector image is also possible, in this case, the vector image needs to be rasterized before that:
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; | |
medianfilter(); | |
public static void smallRectangularfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/SmallRectangularFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new SmallRectangularFilterOptions()); | |
}, "smallrectangular"); | |
} | |
public static void bigRectangularfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/BigRectangularFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new BigRectangularFilterOptions()); | |
}, "bigrectangular"); | |
} | |
public static void sharpenfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/SharpenFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new SharpenFilterOptions()); | |
}, "sharpen"); | |
} | |
public static void motionWienerfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/MotionWienerFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new MotionWienerFilterOptions(20, 2, 0)); | |
}, "motionwiener"); | |
} | |
public static void bilateralSmoothingfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/BilateralSmoothingFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new BilateralSmoothingFilterOptions()); | |
}, "bilateralsmoothing"); | |
} | |
public static void gaussBlurfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/GaussianBlurFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new GaussianBlurFilterOptions(5, 4)); | |
}, "gaussblur"); | |
} | |
public static void gaussWienerfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/GaussWienerFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new GaussWienerFilterOptions(5, 5)); | |
}, "gausswiener"); | |
} | |
public static void medianfilter() | |
{ | |
filterImages(image -> | |
{ | |
// https://apireference.aspose.com/imaging/java/com.aspose.imaging.imagefilters.filteroptions/MedianFilterOptions | |
Rectangle filterRect = new Rectangle(image.getWidth() / 6, image.getHeight() / 6, image.getWidth() * 2 / 3, image.getHeight() * 2 / 3); | |
image.filter(filterRect, new MedianFilterOptions(20)); | |
}, "median"); | |
} | |
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; | |
} |
More Java code examples you can find in the Aspose documentation.