How to adjust an image
How to adjust an image
You can adjust an image brightness by using the AdjustBrightness method and set a brightness parameter in a range from -255 to 255 for performing corrections. The same you can do with AdjustContrast method to make contrast corrections with parameter settings to a range from -100 and 100. If the image has a color component domination, you can make gamma corrections by setting the Red, Green and Blue components coefficient using AdjustGamma method:
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; | |
adjustBrightness(); | |
public static void adjustGammaRGB() | |
{ | |
filterImages(image -> | |
{ | |
//https://apireference.aspose.com/imaging/java/com.aspose.imaging/RasterImage#adjustGamma-float-float-float- | |
image.adjustGamma(5, 0.1f, 0.1f); | |
}, "adjustgammargb"); | |
} | |
public static void adjustGamma() | |
{ | |
filterImages(image -> | |
{ | |
//https://apireference.aspose.com/imaging/java/com.aspose.imaging/RasterImage#adjustGamma-float- | |
image.adjustGamma(3); | |
}, "adjustgamma"); | |
} | |
public static void adjustContrast() | |
{ | |
filterImages(image -> | |
{ | |
//https://apireference.aspose.com/imaging/java/com.aspose.imaging/RasterImage#adjustContrast-float- | |
image.adjustContrast(50); | |
}, "adjustcontrast"); | |
} | |
public static void adjustBrightness() | |
{ | |
filterImages(image -> | |
{ | |
//https://apireference.aspose.com/imaging/java/com.aspose.imaging/RasterImage#adjustBrightness-int- | |
image.adjustBrightness(100); | |
}, "adjustbrightness"); | |
} | |
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; | |
} |