PPTX DOCX XLSX PDF ODP
Aspose.Imaging  for Python
BMP

Use Python for BMP Images Background Remove

Create Python Apps to Remove Background of BMP Images and Photos via Server APIs

How to Remove Background of BMP Images and Photos with Python

To remove the background from an image or photo demands precise identification of prominent objects. For BMP images, various methods are available for object definition. In straightforward scenarios, the automated approach efficiently handles images with uniform backgrounds. Nevertheless, when dealing with photos featuring multiple figures or objects merging with the background, it’s recommended to conduct a preliminary object designation. This entails manually outlining rectangular areas and specifying the object types to be highlighted. In more intricate cases of automated object allocation, the Cloud API stands as an alternative. This cloud-based application identifies objects within a photo and employs the resulting contours to eliminate the background. Following the background removal, enhancing the edges of the remaining objects can significantly enhance the overall image quality. For removing the background in BMP files, the suggestion is to utilize the Aspose.Imaging for Python via .NET API which is a feature-rich, powerful and easy to use image manipulation and conversion API for Python platform. You may install it using the following command from your system command.

The system command line

>> pip install aspose-imaging-python-net

Steps to Remove background from BMP via Python

You need the aspose-imaging-python-net to try the following workflow in your own environment.

  • Load BMP files with Image.Load method
  • Remove background;
  • Save image to disc in the supported by Aspose.Imaging format

System Requirements

Aspose.Imaging for Python is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • Microsoft Windows / Linux with .NET Core Runtime.
  • Python and PyPi package manager.
 

Remove background in BMP images - Python

from aspose.imaging import Image, RasterImage, Point, Rectangle, Color
from aspose.imaging.fileformats.png import PngColorType
from aspose.imaging.imageoptions import PngOptions
from aspose.imaging.masking import *
from aspose.imaging.masking.options import *
from aspose.imaging.masking.result import *
from aspose.imaging.sources import FileCreateSource
from aspose.pycore import as_of
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
def remove_background_processing_with_manual_rectangles():
raster_formats = [
"jpg",
"png",
"bmp",
"apng",
"dicom",
"jp2",
"j2k",
"tga",
"webp",
"tif",
"gif",
"ico"
]
vector_formats = [
"svg",
"otg",
"odg",
"wmf",
"emf",
"wmz",
"emz",
"cmx",
"cdr"
]
all_formats: list = []
all_formats.extend(raster_formats)
all_formats.extend(vector_formats)
for format_ext in all_formats:
input_file = os.path.join(templates_folder, f"couple.{format_ext}")
if not os.path.exists(input_file):
continue
is_vector_format = format_ext in vector_formats
# Need to rasterize vector formats before background remove
if is_vector_format:
input_file = rasterize_vector_image(format_ext, input_file)
output_file = os.path.join(templates_folder, f"remove_background_manual_rectangles.{format_ext}.png")
print(f"Processing {format_ext}")
with as_of(Image.load(input_file), RasterImage) as image:
obj_init3 = AutoMaskingArgs()
obj_init3.objects_rectangles = [Rectangle(87, 47, 123, 308), Rectangle(180, 24, 126, 224)]
obj_init4 = PngOptions()
obj_init4.color_type = PngColorType.TRUECOLOR_WITH_ALPHA
obj_init4.source = FileCreateSource(output_file, False)
obj_init5 = AutoMaskingGraphCutOptions()
obj_init5.feathering_radius = 2
obj_init5.method = SegmentationMethod.GRAPH_CUT
obj_init5.args = obj_init3
obj_init5.export_options = obj_init4
masking_options = obj_init5
with ImageMasking(image).create_session(masking_options) as masking_session:
# first run of segmentation
with masking_session.decompose() as _:
pass
args_with_user_markers = AutoMaskingArgs()
obj_init_list = [
# background markers
None,
# foreground markers
UserMarker()
# boy's head
.add_point(218, 48, 10)
# girl's head
.add_point(399, 66, 10)
# girs's body
.add_point(158, 141, 10)
.add_point(158, 209, 20)
.add_point(115, 225, 5)
.get_points()]
args_with_user_markers.objects_points = obj_init_list
with masking_session.improve_decomposition(args_with_user_markers) as masking_result:
with masking_result[1].get_image() as result_image:
result_image.save()
if delete_output:
os.remove(output_file)
# Remove rasterized vector image
if is_vector_format and delete_output:
os.remove(input_file)
def remove_background_auto_processing_with_assumed_objects():
raster_formats = [
"jpg",
"png",
"bmp",
"apng",
"dicom",
"jp2",
"j2k",
"tga",
"webp",
"tif",
"gif"]
vector_formats = [
"svg",
"otg",
"odg",
"eps",
"wmf",
"emf",
"wmz",
"emz",
"cmx",
"cdr"]
all_formats = []
all_formats.extend(raster_formats)
all_formats.extend(vector_formats)
for format_ext in all_formats:
input_file = os.path.join(templates_folder, f"couple.{format_ext}")
if not os.path.exists(input_file):
continue
is_vector_format = format_ext in vector_formats
# Need to rasterize vector formats before background remove
if is_vector_format:
input_file = rasterize_vector_image(format_ext, input_file)
output_file = os.path.join(templates_folder,
f"remove_background_auto_assumed_objects.{format_ext}.png")
print(f"Processing {format_ext}")
with as_of(Image.load(input_file), RasterImage) as image:
obj_init9 = list()
obj_init9.append(AssumedObjectData(DetectedObjectType.HUMAN, Rectangle(87, 47, 123, 308)))
obj_init9.append(AssumedObjectData(DetectedObjectType.HUMAN, Rectangle(180, 24, 126, 224)))
obj_init10 = PngOptions()
obj_init10.color_type = PngColorType.TRUECOLOR_WITH_ALPHA
obj_init10.source = FileCreateSource(output_file, False)
obj_init11 = AutoMaskingGraphCutOptions()
obj_init11.assumed_objects = obj_init9
obj_init11.calculate_default_strokes = True
obj_init11.feathering_radius = 1
obj_init11.method = SegmentationMethod.GRAPH_CUT
obj_init11.export_options = obj_init10
obj_init11.background_replacement_color = Color.green
masking_options = obj_init11
with ImageMasking(image).decompose(masking_options) as masking_result:
with masking_result[1].get_image() as result_image:
result_image.save()
# Remove rasterized vector image
if is_vector_format and delete_output:
os.remove(input_file)
if delete_output:
os.remove(output_file)
def remove_background_auto_processing():
raster_formats = [
"jpg",
"png",
"bmp",
"apng",
"dicom",
"jp2",
"j2k",
"tga",
"webp",
"tif",
"gif"]
vector_formats = [
"svg",
"otg",
"odg",
"eps",
"wmf",
"emf",
"wmz",
"emz",
"cmx",
"cdr"]
all_formats: list = []
all_formats.extend(raster_formats)
all_formats.extend(vector_formats)
for format_ext in all_formats:
input_file = os.path.join(templates_folder, f"couple.{format_ext}")
if not os.path.exists(input_file):
continue
is_vector_format = format_ext in vector_formats
# Need to rasterize vector formats before background remove
if is_vector_format:
input_file = rasterize_vector_image(format_ext, input_file)
output_file = os.path.join(templates_folder, f"remove_background_auto.{format_ext}.png")
print(f"Processing {format_ext}")
with as_of(Image.load(input_file), RasterImage) as image:
obj_init14 = PngOptions()
obj_init14.color_type = PngColorType.TRUECOLOR_WITH_ALPHA
obj_init14.source = FileCreateSource(output_file, False)
obj_init15 = AutoMaskingGraphCutOptions()
obj_init15.feathering_radius = 1
obj_init15.method = SegmentationMethod.GRAPH_CUT
obj_init15.export_options = obj_init14
obj_init15.background_replacement_color = Color.green
masking_options = obj_init15
with ImageMasking(image).decompose(masking_options) as masking_result:
with masking_result[1].get_image() as result_image:
result_image.save()
# Remove rasterized vector image
if is_vector_format and delete_output:
os.remove(input_file)
if delete_output:
os.remove(output_file)
def remove_background_generic_example():
raster_formats = [
"jpg",
"png",
"bmp",
"apng",
"dicom",
"jp2",
"j2k",
"tga",
"webp",
"tif",
"gif"]
vector_formats = [
"svg",
"otg",
"odg",
"wmf",
"emf",
"wmz",
"emz",
"cmx",
"cdr"]
all_formats: list = []
all_formats.extend(raster_formats)
all_formats.extend(vector_formats)
for format_ext in all_formats:
input_file = os.path.join(templates_folder, f"couple.{format_ext}")
if not os.path.exists(input_file):
continue
is_vector_format: bool = format_ext in vector_formats
# Need to rasterize vector formats before background remove
if is_vector_format:
input_file = rasterize_vector_image(format_ext, input_file)
output_file = os.path.join(templates_folder, f"remove_background.{format_ext}.png")
print(f"Processing {format_ext}")
with as_of(Image.load(input_file), RasterImage) as image:
obj_init18 = PngOptions()
obj_init18.color_type = PngColorType.TRUECOLOR_WITH_ALPHA
obj_init18.source = FileCreateSource(output_file, False)
obj_init19 = AutoMaskingGraphCutOptions()
obj_init19.calculate_default_strokes = True
obj_init19.feathering_radius = 1
obj_init19.method = SegmentationMethod.GRAPH_CUT
obj_init19.export_options = obj_init18
obj_init19.background_replacement_color = Color.green
masking_options = obj_init19
with ImageMasking(image).decompose(masking_options) as masking_result:
with masking_result[1].get_image() as result_image:
result_image.save()
# Remove rasterized vector image
if is_vector_format and delete_output:
os.remove(input_file)
if delete_output:
os.remove(output_file)
def rasterize_vector_image(format_ext, input_file):
output_file: str = os.path.join(templates_folder, f"rasterized.{format_ext}.png")
with Image.load(input_file) as image:
image.save(output_file, PngOptions())
return output_file
class UserMarker:
def __init__(self):
self._list: list = []
def add_point(self, left, top, radius):
for y in range(top - radius, top + radius + 1):
for x in range(left - radius, left + radius + 1):
self._list.append(Point(x, y))
return self
def get_points(self):
return self._list
# Run examples
remove_background_auto_processing_with_assumed_objects()
remove_background_processing_with_manual_rectangles()
remove_background_auto_processing()
remove_background_generic_example()
 
  • About Aspose.Imaging for Python 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.

    Remove background in BMP via Online App

    Remove background in BMP documents by visiting our Live Demos website . The live demo has the following benefits

      No need to download or setup anything
      No need to write any code
      Just upload your BMP files and hit "Remove background now" button
      Instantly get the download link for the resultant file

    BMP What is BMP File Format

    Files having extension .BMP represent Bitmap Image files that are used to store bitmap digital images. These images are independent of graphics adapter and are also called device independent bitmap (DIB) file format. This independency serves the purpose of opening the file on multiple platforms such as Microsoft Windows and Mac. The BMP file format can store data as two-dimensional digital images in both monochrome as well as color format with various colour depths.

    Read More

    Other Supported Remove background Formats

    Using Python, one can easily remove background from different formats including.

    APNG (Animated Portable Network Graphics)
    ICO (Windows icon)
    JPG (Joint Photographic Experts Group)
    DIB (Device Independent Bitmap)
    DICOM (Digital Imaging & Communications)
    DJVU (Graphics Format)
    DNG (Digital Camera Image)
    EMF (Enhanced Metafile Format)
    EMZ (Windows Compressed Enhanced Metafile)
    GIF (Graphical Interchange Format)
    JP2 (JPEG 2000)
    J2K (Wavelet Compressed Image)
    PNG (Portable Network Graphics)
    TIFF (Tagged Image Format)
    WEBP (Raster Web Image)
    WMF (Microsoft Windows Metafile)
    WMZ (Compressed Windows Media Player Skin)
    TGA (Targa Graphic)
    SVG (Scalable Vector Graphics)
    EPS (Encapsulated PostScript Language)
    CDR (Vector Drawing Image)
    CMX (Corel Exchange Image)
    OTG (OpenDocument Standard)
    ODG (Apache OpenOffice Draw Format)