EMF Görsellerin Arka Planını Değiştirmek için Python Kullanın
EMF Görsel ve Fotoğrafların Arka Planını Sunucu API’leri Aracılığıyla Değiştirmek için Python Uygulaması Oluşturun
Python ile EMF Görsel ve Fotoğraflarının Arka Planını Değiştirme
Çoğu zaman, bir görüntüyü EMF formatında düzenlerken, arka planını değiştirme zorunluluğu ortaya çıkar. İlk adım, fotoğrafta ön plandaki nesnelerin seçilmesini ve arka plana atfedilen alanları izole etmek için bunları görüntünün geri kalanından ayırmayı içerir. Arka plan tekdüze bir alan oluşturduğunda nesne konturları otomatik olarak oluşturulabilir. Ancak fotoğrafta düzensiz bir arka plan bulunuyorsa veya istenen nesnenin arka plandan ayırt edilmesinde zorluklar ortaya çıkıyorsa, ön görüntü işaretleme yönteminin kullanılması tavsiye edilir. Bu, fotoğrafta beklenen nesnelerin konumlandırıldığı dikdörtgen alanların seçilmesini ve türlerinin belirtilmesini gerektirir. Bu tür eylemler manuel olarak yürütülebilir veya otomatik nesne algılama için Cloud API aracılığıyla kolaylaştırılabilir. Nesnelerin seçilmesi ve orijinal arka planın kaldırılmasının ardından, yeni bir arka plan ekleme veya onu şeffaf hale getirme seçeneğiniz vardır. EMF formatındaki bir görselin arka planını değiştirmek için şunu kullanacağız: Python platformu için zengin özelliklere sahip, güçlü ve kullanımı kolay bir görüntü işleme ve dönüştürme API’si olan Aspose.Imaging for Python via .NET API. Sistem komutunuzdan aşağıdaki komutu kullanarak kurabilirsiniz.
Sistem komut satırı
>> pip install aspose-imaging-python-net
EMF'lerde arka planı Python ile Değiştirme Adımları
Aşağıdaki iş akışını kendi ortamınızda denemek için aspose-imaging-python-net gerekir.
- EMF dosyalarını Image.Load yöntemiyle yükleyin
- Arka planı değiştir;
- Resmi Aspose.Imaging formatında diske kaydedin
sistem gereksinimleri
Aspose.Imaging for Python tüm büyük işletim sistemlerinde desteklenir. Sadece aşağıdaki ön koşullara sahip olduğunuzdan emin olun.
- .NET Core Runtime ile Microsoft Windows / Linux.
- Python ve PyPi paket yöneticisi.
EMF resimlerde arka planı değiştirin - 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() |
Python API için Aspose.Imaging Hakkında
Aspose.Imaging API, uygulamalar içinde görüntüleri (fotoğrafları) oluşturmak, değiştirmek, çizmek veya dönüştürmek için kullanılan bir görüntü işleme çözümüdür. Şunları sunar: Çeşitli görüntü formatları (tek tip çok sayfalı veya çok çerçeveli görüntü işleme dahil) arasındaki dönüştürmeler dahil ancak bunlarla sınırlı olmamak üzere çapraz platform Görüntü işleme, çizim, grafik temel öğeleriyle çalışma, dönüştürmeler (yeniden boyutlandırma, kırpma, çevirme ve döndürme) gibi değişiklikler , ikilileştirme, gri tonlama, ayarlama), gelişmiş görüntü işleme özellikleri (filtreleme, renk taklidi, maskeleme, eğrilik düzeltme) ve bellek optimizasyon stratejileri. Bu bağımsız bir kitaplıktır ve görüntü işlemleri için herhangi bir yazılıma bağlı değildir. Projelere yerel API’ler ile yüksek performanslı görüntü dönüştürme özellikleri kolayca eklenebilir. Bunlar %100 özel şirket içi API’lerdir ve görüntüler sunucularınızda işlenir.Çevrimiçi Uygulama aracılığıyla arka planı EMF’lerde değiştirin
Canlı Demolar web sitemizi ziyaret ederek EMF belgelerindeki arka planı değiştirin. Canlı demo aşağıdaki avantajlara sahiptir
EMF Nedir EMF Dosya formatı
Gelişmiş meta dosya biçimi (EMF), grafik görüntüleri aygıttan bağımsız olarak depolar. EMF'nin meta dosyaları, herhangi bir çıktı aygıtında ayrıştırıldıktan sonra saklanan görüntüyü oluşturabilen, kronolojik sırayla değişken uzunluktaki kayıtlardan oluşur. Bu değişken uzunluklu kayıtlar, kapalı nesnelerin tanımları, çizim komutları ve görüntüyü doğru bir şekilde oluşturmak için kritik olan grafik özellikleri olabilir. Bir cihaz kendi grafik ortamını kullanarak bir EMF meta dosyasını açtığında, orijinal görüntünün oranları, boyutları, renkleri ve diğer grafik özellikleri, açılış cihazı platformundan bağımsız olarak aynı kalır.
Daha fazla okuDesteklenen Diğer Arka Plan Biçimlerini Değiştir
Python kullanarak, arka planı da dahil olmak üzere farklı biçimlerde kolayca değiştirebilirsiniz.