WMF 画像の背景の削除には Python を使用します
サーバー API 経由で WMF の画像と写真の背景を削除する Python アプリを作成する
Python を使用して WMF の画像と写真の背景を削除する方法
画像や写真から背景を除去するには、目立つオブジェクトを正確に識別する必要があります。 WMF イメージの場合、オブジェクト定義にはさまざまな方法が使用できます。単純なシナリオでは、自動化されたアプローチにより、均一な背景を持つ画像が効率的に処理されます。ただし、背景に複数の人物や物体が溶け込んでいる写真を扱う場合は、事前にオブジェクトを指定することをお勧めします。これには、手動で長方形の領域の輪郭を描き、強調表示するオブジェクトのタイプを指定する必要があります。自動オブジェクト割り当てのより複雑なケースでは、Cloud API が代替手段として機能します。このクラウドベースのアプリケーションは、写真内のオブジェクトを識別し、結果の輪郭を使用して背景を除去します。背景を除去した後、残ったオブジェクトのエッジを強調すると、全体の画質が大幅に向上します。 WMF ファイルの背景を削除するには、 .NET 経由の Python 用 Aspose.Imaging API は、Python プラットフォーム用の機能が豊富で強力で使いやすい画像操作および変換 API です。システムコマンドから次のコマンドを使用してインストールできます。
システム コマンド ライン
>> pip install aspose-imaging-python-net
Pythonを介してWMFから背景を削除する手順
独自の環境で次のワークフローを試すには、 aspose-imaging-python-net が必要です。
+Image.Loadメソッドを使用してWMFファイルをロードします +背景を削除します。 +Aspose.Imaging形式でサポートされているディスクに画像を保存します
システム要求
Aspose.Imaging for Pythonは、すべての主要なオペレーティングシステムでサポートされています。次の前提条件があることを確認してください。
- .NET Core ランタイムを搭載した Microsoft Windows / Linux。
- Python および PyPi パッケージ マネージャー。
WMF画像の背景を削除-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であり、画像はサーバーで処理されます。オンラインアプリを介してWMFの背景を削除します
[Live Demos Webサイト](https://products.aspose.app/imaging/remove-background)にアクセスして、WMFドキュメントの背景を削除します。 ライブデモには次の利点があります
WMF とは WMF ファイル形式
WMF拡張子の付いたファイルは、ベクターおよびビットマップ形式の画像データを格納するためのMicrosoft Windowsメタファイル(WMF)を表します。より正確に言うと、WMFは、デバイスに依存しないグラフィックファイル形式のベクターファイル形式カテゴリに属しています。 Windowsグラフィックデバイスインターフェイス(GDI)は、WMFファイルに格納されている関数を使用して、画面に画像を表示します。拡張メタファイル(EMF)として知られるWMFのより拡張されたバージョンは、フォーマットをより豊富な機能にするために後で公開されました。実際には、WMFはSVGに似ています。
続きを読むその他のサポートされているバックグラウンドフォーマットの削除
Pythonを使用すると、を含むさまざまな形式から背景を簡単に削除できます。