PPTX DOCX XLSX PDF ODP
Aspose.Imaging  cho Python
ICO

Sử dụng Python cho hình ảnh ICO Xóa nền

Tạo ứng dụng Python để xóa nền của hình ảnh và ảnh ICO thông qua API máy chủ

Cách xóa nền của hình ảnh và ảnh ICO bằng Python

Để xóa nền khỏi một hình ảnh hoặc bức ảnh đòi hỏi phải xác định chính xác các đối tượng nổi bật. Đối với hình ảnh ICO, có nhiều phương pháp khác nhau để định nghĩa đối tượng. Trong các tình huống đơn giản, phương pháp tự động xử lý hiệu quả các hình ảnh có nền đồng nhất. Tuy nhiên, khi xử lý ảnh có nhiều nhân vật hoặc vật thể hợp nhất với nền, bạn nên tiến hành chỉ định đối tượng sơ bộ. Điều này đòi hỏi phải phác thảo thủ công các khu vực hình chữ nhật và chỉ định các loại đối tượng cần đánh dấu. Trong các trường hợp phân bổ đối tượng tự động phức tạp hơn, API đám mây sẽ là một giải pháp thay thế. Ứng dụng dựa trên đám mây này xác định các đối tượng trong ảnh và sử dụng các đường viền thu được để loại bỏ nền. Sau khi loại bỏ nền, việc tăng cường các cạnh của các đối tượng còn lại có thể nâng cao đáng kể chất lượng hình ảnh tổng thể. Để xóa nền trong tệp ICO, đề xuất là sử dụng Aspose.Imaging for Python via .NET API là API chuyển đổi và thao tác hình ảnh giàu tính năng, mạnh mẽ và dễ sử dụng cho nền tảng Python. Bạn có thể cài đặt nó bằng lệnh sau từ lệnh hệ thống của mình.

Dòng lệnh hệ thống

>> pip install aspose-imaging-python-net

Các bước xóa nền khỏi ICO qua Python

Bạn cần aspose-imaging-python-net để thử quy trình công việc sau trong môi trường của riêng bạn.

  • Tải tệp ICO bằng phương pháp Image.Load
  • Xóa nền;
  • Lưu hình ảnh vào đĩa ở định dạng Aspose.Imaging được hỗ trợ

yêu cầu hệ thống

Aspose.Imaging cho Python được hỗ trợ trên tất cả các hệ điều hành chính. Chỉ cần đảm bảo rằng bạn có các điều kiện tiên quyết sau.

  • Microsoft Windows/Linux với .NET Core Runtime.
  • Trình quản lý gói Python và PyPi.
 

Xóa nền trong hình ảnh ICO - 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()
 
  • Giới thiệu về Aspose.Imaging cho API Python

    Aspose.Imaging API là một giải pháp xử lý hình ảnh để tạo, sửa đổi, vẽ hoặc chuyển đổi hình ảnh (ảnh) trong các ứng dụng. Nó cung cấp: xử lý hình ảnh đa nền tảng, bao gồm nhưng không giới hạn ở chuyển đổi giữa các định dạng hình ảnh khác nhau (bao gồm xử lý hình ảnh nhiều trang hoặc nhiều khung hình đồng nhất), các sửa đổi như vẽ, làm việc với đồ họa nguyên thủy, chuyển đổi (thay đổi kích thước, cắt, lật và xoay , binarization, thang độ xám, điều chỉnh), các tính năng thao tác hình ảnh nâng cao (lọc, phối màu, tạo mặt nạ, giải mã) và các chiến lược tối ưu hóa bộ nhớ. Đây là một thư viện độc lập và không phụ thuộc vào bất kỳ phần mềm nào cho các thao tác hình ảnh. Người ta có thể dễ dàng thêm các tính năng chuyển đổi hình ảnh hiệu suất cao với các API gốc trong các dự án. Đây là các API tại chỗ 100% riêng tư và hình ảnh được xử lý tại máy chủ của bạn.

    Xóa nền trong ICO s qua Ứng dụng trực tuyến

    Xóa nền trong tài liệu ICO bằng cách truy cập [trang web Bản trình diễn trực tiếp] của chúng tôi ( https://products.aspose.app/imaging/remove-background) . Bản demo trực tiếp có những lợi ích sau

      Không cần tải xuống hoặc thiết lập bất cứ thứ gì
      Không cần viết bất kỳ mã nào
      Chỉ cần tải lên các tệp ICO của bạn và nhấn nút "Xóa nền ngay bây giờ "
      Nhận ngay liên kết tải xuống cho tệp kết quả

    ICO Những gì là ICO Định dạng tệp

    Định dạng tệp ICO là định dạng tệp hình ảnh cho các biểu tượng máy tính trong Microsoft Windows. Các tệp ICO chứa một hoặc nhiều hình ảnh nhỏ ở nhiều kích thước và độ sâu màu, để chúng có thể được chia tỷ lệ thích hợp. Trong Windows, tất cả các tệp thực thi hiển thị biểu tượng cho người dùng, trên màn hình nền, trong Menu Bắt đầu hoặc trong Windows Explorer, phải mang biểu tượng ở định dạng ICO.

    Đọc thêm

    Các định dạng nền được hỗ trợ khác

    Sử dụng Python, người ta có thể dễ dàng xóa nền khỏi các định dạng khác nhau, bao gồm cả.

    APNG (Đồ họa mạng di động hoạt hình)
    BMP (Ảnh bitmap)
    JPG (Nhóm chuyên gia nhiếp ảnh chung)
    DIB (Bản đồ bit độc lập với thiết bị)
    DICOM (Hình ảnh & Truyền thông Kỹ thuật số)
    DJVU (Định dạng đồ họa)
    DNG (Hình ảnh máy ảnh kỹ thuật số)
    EMF (Định dạng siêu tệp nâng cao)
    EMZ (Siêu tệp nâng cao được nén của Windows)
    GIF (Định dạng trao đổi đồ họa)
    JP2 (JPEG 2000)
    J2K (Hình ảnh nén Wavelet)
    PNG (Biểu đồ minh họa mạng lưới không dây)
    TIFF (Định dạng hình ảnh được gắn thẻ)
    WEBP (Raster Hình ảnh Web)
    WMF (Microsoft Windows Metafile)
    WMZ (Giao diện Windows Media Player được nén)
    TGA (Đồ họa Targa)
    SVG (Đồ họa vector có thể mở rộng)
    EPS (Ngôn ngữ PostScript được đóng gói)
    CDR (Hình ảnh Bản vẽ Véc tơ)
    CMX (Hình ảnh trao đổi Corel)
    OTG (Tiêu chuẩn OpenDocument)
    ODG (Apache OpenOffice Draw Format)