Change background in TIFF via Java
Build your own Java apps to Change background in TIFF files using server-side APIs.
How to change background in TIFF Files Using Java
Frequently, achieving the ideal image requires changing the background. To attain the desired TIFF format image effect, foreground objects must be isolated from the rest of the picture. Automatic object detection is possible if the background is uniform. If the photo’s background is uneven or object separation is challenging, pre-marking the image is recommended. This involves identifying rectangular regions within the photo where intended objects reside and specifying their types. This can be done manually or automatically through the Cloud API’s object recognition feature. Following object selection and original background removal, a new background can be applied or transparency can be implemented. In order to change background in TIFF files, we’ll use Aspose.Imaging for Java API which is a feature-rich, powerful and easy to use image manipulation and conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging</artifactId>
<version>version of aspose-imaging API</version>
<classifier>jdk16</classifier>
</dependency>
Steps to Change background in TIFF via Java
You need the aspose-imaging-version-jdk16.jar to try the following workflow in your own environment.
- Load TIFF files with Image.load method
- Change background;
- Save image to disc in the supported by Aspose.Imaging format
System Requirements
Aspose.Imaging for Java is supported on all major operating systems. Just make sure that you have the following prerequisites.
- JDK 1.6 or higher is installed.
Change background in TIFF images - Java
import com.aspose.imaging.*; | |
import com.aspose.imaging.fileformats.png.PngColorType; | |
import com.aspose.imaging.imageoptions.PngOptions; | |
import com.aspose.imaging.masking.IMaskingSession; | |
import com.aspose.imaging.masking.ImageMasking; | |
import com.aspose.imaging.masking.options.*; | |
import com.aspose.imaging.masking.result.MaskingResult; | |
import com.aspose.imaging.sources.FileCreateSource; | |
import java.io.File; | |
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.function.Consumer; | |
//Most common example to demonstrate background change/remove tool | |
removeBackgroundGenericExample(); | |
// Folder that contains images to process | |
static final String templatesFolder = "c:\\Data\\"; | |
public static void removeBackgroundProcessingWithManualRectangles() | |
{ | |
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng", | |
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif"); | |
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(new Consumer<String>() | |
{ | |
@Override | |
public void accept(String formatExt) | |
{ | |
String inputFile = templatesFolder + "couple." + formatExt; | |
boolean isVectorFormat = vectorFormats.contains(formatExt); | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = rasterizeVectorImage(formatExt, inputFile); | |
} | |
String outputFile = templatesFolder + "remove_background_manual_rectangles." + formatExt; | |
System.out.println("Processing " + formatExt); | |
try (RasterImage image = (RasterImage) Image.load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions(); | |
maskingOptions.setFeatheringRadius(2); | |
maskingOptions.setMethod(SegmentationMethod.GraphCut); | |
AutoMaskingArgs maskingArgs = new AutoMaskingArgs(); | |
maskingArgs.setObjectsRectangles(new Rectangle[] | |
{ | |
// girl's bound box | |
new Rectangle(87, 47, 123, 308), | |
// boy's bound box | |
new Rectangle(180, 24, 126, 224) | |
}); | |
maskingOptions.setArgs(maskingArgs); | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setColorType(PngColorType.TruecolorWithAlpha); | |
pngOptions.setSource(new FileCreateSource(outputFile, false)); | |
maskingOptions.setExportOptions(pngOptions); | |
IMaskingSession maskingSession = new ImageMasking(image) | |
.createSession(maskingOptions); | |
try | |
{ | |
// first run of segmentation | |
maskingSession.decompose().dispose(); | |
AutoMaskingArgs argsWithUserMarkers = new AutoMaskingArgs(); | |
argsWithUserMarkers.setObjectsPoints(new Point[][] | |
{ | |
// background markers | |
null, | |
// foreground markers | |
new UserMarker() | |
// boy's head | |
.addPoint(218, 48, 10) | |
// girl's head | |
.addPoint(399, 66, 10) | |
// girs's body | |
.addPoint(158, 141, 10) | |
.addPoint(158, 209, 20) | |
.addPoint(115, 225, 5) | |
.getPoints() | |
}); | |
try (MaskingResult maskingResult = maskingSession | |
.improveDecomposition(argsWithUserMarkers)) | |
{ | |
try (Image resultImage = maskingResult.get_Item(1).getImage()) | |
{ | |
resultImage.save(); | |
} | |
} | |
} | |
finally | |
{ | |
maskingSession.dispose(); | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
new File(inputFile).delete(); | |
} | |
} | |
}); | |
} | |
public static void removeBackgroundAutoProcessingWithAssumedObjects() | |
{ | |
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng", | |
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif"); | |
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(new Consumer<String>() { | |
@Override | |
public void accept(String formatExt) | |
{ | |
String inputFile = templatesFolder + "couple." + formatExt; | |
boolean isVectorFormat = vectorFormats.contains(formatExt); | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = rasterizeVectorImage(formatExt, inputFile); | |
} | |
String outputFile = templatesFolder | |
+ "remove_background_auto_assumed_objects." + formatExt; | |
System.out.println("Processing " + formatExt); | |
try (RasterImage image = (RasterImage) Image.load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions(); | |
final LinkedList<AssumedObjectData> assumedObjects = new LinkedList<>(); | |
// girl's bound box | |
assumedObjects.add( | |
new AssumedObjectData(DetectedObjectType.Human, | |
new Rectangle(87, 47, 123, 308))); | |
// boy's bound box | |
assumedObjects.add( | |
new AssumedObjectData(DetectedObjectType.Human, | |
new Rectangle(180, 24, 126, 224))); | |
maskingOptions.setAssumedObjects(assumedObjects); | |
maskingOptions.setCalculateDefaultStrokes(true); | |
maskingOptions.setFeatheringRadius(1); | |
maskingOptions.setMethod(SegmentationMethod.GraphCut); | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setColorType(PngColorType.TruecolorWithAlpha); | |
pngOptions.setSource(new FileCreateSource(outputFile, false)); | |
maskingOptions.setExportOptions(pngOptions); | |
maskingOptions.setBackgroundReplacementColor(Color.getGreen()); | |
try (MaskingResult maskingResult = new ImageMasking(image) | |
.decompose(maskingOptions)) | |
{ | |
try (Image resultImage = maskingResult.get_Item(1).getImage()) | |
{ | |
resultImage.save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
new File(inputFile).delete(); | |
} | |
} | |
}); | |
} | |
public static void removeBackgroundAutoProcessing() | |
{ | |
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng", | |
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif"); | |
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(new Consumer<String>() { | |
@Override | |
public void accept(String formatExt) | |
{ | |
String inputFile = templatesFolder + "couple." + formatExt; | |
boolean isVectorFormat = vectorFormats.contains(formatExt); | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = rasterizeVectorImage(formatExt, inputFile); | |
} | |
String outputFile = templatesFolder + "remove_background_auto." + formatExt; | |
System.out.println("Processing " + formatExt); | |
try (RasterImage image = (RasterImage) Image.load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions(); | |
maskingOptions.setFeatheringRadius(1); | |
maskingOptions.setMethod(SegmentationMethod.GraphCut); | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setColorType(PngColorType.TruecolorWithAlpha); | |
pngOptions.setSource(new FileCreateSource(outputFile, false)); | |
maskingOptions.setExportOptions(pngOptions); | |
maskingOptions.setBackgroundReplacementColor(Color.getGreen()); | |
try (MaskingResult maskingResult = new ImageMasking(image) | |
.decompose(maskingOptions)) | |
{ | |
try (Image resultImage = maskingResult.get_Item(1).getImage()) | |
{ | |
resultImage.save(); | |
} | |
} | |
} | |
//Remove rasterized vector image | |
if (isVectorFormat) | |
{ | |
new File(inputFile).delete(); | |
} | |
} | |
}); | |
} | |
public static void removeBackgroundGenericExample() | |
{ | |
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng", | |
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif"); | |
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(new Consumer<String>() { | |
@Override | |
public void accept(String formatExt) | |
{ | |
String inputFile = templatesFolder + "couple." + formatExt; | |
boolean isVectorFormat = vectorFormats.contains(formatExt); | |
//Need to rasterize vector formats before background remove | |
if (isVectorFormat) | |
{ | |
inputFile = rasterizeVectorImage(formatExt, inputFile); | |
} | |
String outputFile = templatesFolder + "remove_background." + formatExt; | |
System.out.println("Processing " + formatExt); | |
try (RasterImage image = (RasterImage) Image.load(inputFile)) | |
{ | |
//Additional code examples can be found at | |
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api | |
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions(); | |
maskingOptions.setCalculateDefaultStrokes(true); | |
maskingOptions.setFeatheringRadius(1); | |
maskingOptions.setMethod(SegmentationMethod.GraphCut); | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setColorType(PngColorType.TruecolorWithAlpha); | |
pngOptions.setSource(new FileCreateSource(outputFile, false)); | |
maskingOptions.setExportOptions(pngOptions); | |
maskingOptions.setBackgroundReplacementColor(Color.getGreen()); | |
try (MaskingResult maskingResult = new ImageMasking(image) | |
.decompose(maskingOptions)) | |
{ | |
try (Image resultImage = maskingResult.get_Item(1).getImage()) | |
{ | |
resultImage.save(); | |
} | |
} | |
} | |
//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; | |
} | |
class UserMarker | |
{ | |
private final List<Point> list = new LinkedList<>(); | |
public UserMarker addPoint(int left, int top, int radius) | |
{ | |
for (int y = top - radius; y <= top + radius; y++) | |
{ | |
for (int x = left - radius; x <= left + radius; x++) | |
{ | |
this.list.add(new Point(x, y)); | |
} | |
} | |
return this; | |
} | |
public Point[] getPoints() | |
{ | |
return this.list.toArray(new Point[0]); | |
} | |
} |
About Aspose.Imaging for Java API
Aspose.Imaging API is an image processing solution to create, modify, draw or convert images (photos) within applications. It offers: cross-platform Image processing, including but not limited to conversions between various image formats (including uniform multi-page or multi-frame image processing), modifications such as drawing, working with graphic primitives, transformations (resize, crop, flip&rotate, binarization, grayscale, adjust), advanced image manipulation features (filtering, dithering, masking, deskewing), and memory optimization strategies. It’s a standalone library and does not depend on any software for image operations. One can easily add high-performance image conversion features with native APIs within projects. These are 100% private on-premise APIs and images are processed at your servers.Change background in TIFF via Online App
Change background in TIFF documents by visiting our Live Demos website . The live demo has the following benefits
TIFF What is TIFF File Format
TIFF or TIF, Tagged Image File Format, represents raster images that are meant for usage on a variety of devices that comply with this file format standard. It is capable of describing bilevel, grayscale, palette-color and full-color image data in several color spaces. It supports lossy as well as lossless compression schemes to choose between space and time for applications using the format. The format is extensible and has underwent several revisions that allows the inclusion of an unlimited amount of private or special-purpose information. The format is not machine dependent and is free from bounds like processor, operating system, or file systems.
Read MoreOther Supported Change background Formats
Using Java, one can easily change background in different formats including.