PPTX DOCX XLSX PDF ODP
Aspose.Imaging  Pythonの場合
ICO

ICO 画像の背景の削除には Python を使用します

サーバー API 経由で ICO の画像と写真の背景を削除する Python アプリを作成する

Python を使用して ICO の画像と写真の背景を削除する方法

画像や写真から背景を除去するには、目立つオブジェクトを正確に識別する必要があります。 ICO イメージの場合、オブジェクト定義にはさまざまな方法が使用できます。単純なシナリオでは、自動化されたアプローチにより、均一な背景を持つ画像が効率的に処理されます。ただし、背景に複数の人物や物体が溶け込んでいる写真を扱う場合は、事前にオブジェクトを指定することをお勧めします。これには、手動で長方形の領域の輪郭を描き、強調表示するオブジェクトのタイプを指定する必要があります。自動オブジェクト割り当てのより複雑なケースでは、Cloud API が代替手段として機能します。このクラウドベースのアプリケーションは、写真内のオブジェクトを識別し、結果の輪郭を使用して背景を除去します。背景を除去した後、残ったオブジェクトのエッジを強調すると、全体の画質が大幅に向上します。 ICO ファイルの背景を削除するには、 .NET 経由の Python 用 Aspose.Imaging API は、Python プラットフォーム用の機能が豊富で強力で使いやすい画像操作および変換 API です。システムコマンドから次のコマンドを使用してインストールできます。

システム コマンド ライン

>> pip install aspose-imaging-python-net

Pythonを介してICOから背景を削除する手順

独自の環境で次のワークフローを試すには、 aspose-imaging-python-net が必要です。

+Image.Loadメソッドを使用してICOファイルをロードします +背景を削除します。 +Aspose.Imaging形式でサポートされているディスクに画像を保存します

システム要求

Aspose.Imaging for Pythonは、すべての主要なオペレーティングシステムでサポートされています。次の前提条件があることを確認してください。

  • .NET Core ランタイムを搭載した Microsoft Windows / Linux。
  • Python および PyPi パッケージ マネージャー。
 

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()
 
  • Aspose.Imaging for Python APIについて

    Aspose.Imaging APIは、アプリケーション内で画像(写真)を作成、変更、描画、または変換するための画像処理ソリューションです。クロスプラットフォームの画像処理(さまざまな画像形式間の変換(均一なマルチページまたはマルチフレームの画像処理を含む)、描画などの変更、グラフィックプリミティブの操作、変換(サイズ変更、トリミング、反転、回転)を含むがこれらに限定されない) 、2値化、グレースケール、調整)、高度な画像操作機能(フィルタリング、ディザリング、マスキング、デスキュー)、およびメモリ最適化戦略。これはスタンドアロンライブラリであり、画像操作をソフトウェアに依存しません。プロジェクト内のネイティブAPIを使用して、高性能の画像変換機能を簡単に追加できます。これらは100%プライベートのオンプレミスAPIであり、画像はサーバーで処理されます。

    オンラインアプリを介してICOの背景を削除します

    [Live Demos Webサイト](https://products.aspose.app/imaging/remove-background)にアクセスして、ICOドキュメントの背景を削除します。 ライブデモには次の利点があります

      何かをダウンロードしたりセットアップしたりする必要はありません
      コードを書く必要はありません
      ICOファイルをアップロードして、[今すぐ背景を削除]ボタンをクリックするだけです
      結果のファイルのダウンロードリンクを即座に取得します

    ICO とは ICO ファイル形式

    ICO ファイル形式は、Microsoft Windows のコンピューター アイコンのイメージ ファイル形式です。 ICO ファイルには、適切にスケーリングできるように、複数のサイズと色深度の 1 つまたは複数の小さな画像が含まれています。 Windows では、ユーザー、デスクトップ、スタート メニュー、または Windows エクスプローラーにアイコンを表示するすべての実行可能ファイルは、アイコンを ICO 形式で保持する必要があります。

    続きを読む

    その他のサポートされているバックグラウンドフォーマットの削除

    Pythonを使用すると、を含むさまざまな形式から背景を簡単に削除できます。

    APNG (アニメーション化されたポータブルネットワークグラフィックス)
    BMP (ビットマップ画像)
    JPG (共同写真専門家グループ)
    DIB (デバイスに依存しないビットマップ)
    DICOM (デジタルイメージング&コミュニケーション)
    DJVU (グラフィックフォーマット)
    DNG (デジタルカメラ画像)
    EMF (強化されたメタファイル形式)
    EMZ (Windows圧縮拡張メタファイル)
    GIF (グラフィカルな交換形式)
    JP2 (JPEG 2000)
    J2K (ウェーブレット圧縮画像)
    PNG (ポータブルネットワークグラフィックス)
    TIFF (タグ付き画像形式)
    WEBP (ラスターWebイメージ)
    WMF (MicrosoftWindowsメタファイル)
    WMZ (圧縮されたWindowsMediaPlayerスキン)
    TGA (タルガグラフィック)
    SVG (スケーラブルベクターグラフィックス)
    EPS (カプセル化されたPostScript言語)
    CDR (ベクトル描画画像)
    CMX (CorelExchangeイメージ)
    OTG (OpenDocument標準)
    ODG (ApacheOpenOfficeDrawフォーマット)