PPTX DOCX XLSX PDF ODP
Aspose.Imaging  สำหรับ Python
EMF

ใช้ Python สำหรับ EMF เปลี่ยนพื้นหลังรูปภาพ

สร้างแอป Python เพื่อเปลี่ยนพื้นหลังของรูปภาพ EMF และรูปภาพผ่าน API ของเซิร์ฟเวอร์

วิธีเปลี่ยนพื้นหลังของ EMF รูปภาพและภาพถ่ายด้วย Python

บ่อยครั้ง ในขณะที่แก้ไขรูปภาพใน EMF จำเป็นต้องเปลี่ยนพื้นหลัง ขั้นตอนเริ่มแรกเกี่ยวข้องกับการเลือกวัตถุเบื้องหน้าภายในภาพถ่ายและแยกวัตถุเหล่านั้นออกจากส่วนที่เหลือของภาพ เพื่อแยกพื้นที่ที่เป็นพื้นหลังออก เมื่อพื้นหลังสร้างพื้นที่ที่สม่ำเสมอ สามารถสร้างรูปร่างของวัตถุได้โดยอัตโนมัติ อย่างไรก็ตาม หากภาพถ่ายมีพื้นหลังที่ไม่ปกติหรือเกิดความยุ่งยากในการแยกแยะวัตถุที่ต้องการออกจากฉากหลัง ขอแนะนำให้ใช้วิธีการทำเครื่องหมายภาพเบื้องต้น ซึ่งเกี่ยวข้องกับการเลือกพื้นที่สี่เหลี่ยมภายในภาพถ่ายซึ่งมีการวางตำแหน่งวัตถุที่คาดหวังไว้และระบุประเภทของวัตถุเหล่านั้น การดำเนินการดังกล่าวสามารถดำเนินการด้วยตนเองหรืออำนวยความสะดวกผ่าน Cloud API สำหรับการตรวจจับออบเจ็กต์อัตโนมัติ หลังจากเลือกวัตถุและลบพื้นหลังดั้งเดิมแล้ว คุณจะมีตัวเลือกในการแนะนำพื้นหลังใหม่หรือทำให้พื้นหลังโปร่งใส เราจะใช้การแก้ไขพื้นหลังของรูปภาพในรูปแบบ EMF Aspose.Imaging สำหรับ Python ผ่าน .NET API ซึ่งเป็น API การจัดการรูปภาพและการแปลงที่มีคุณลักษณะหลากหลาย ทรงพลัง และใช้งานง่ายสำหรับแพลตฟอร์ม Python คุณสามารถติดตั้งได้โดยใช้คำสั่งต่อไปนี้จากคำสั่งระบบของคุณ

บรรทัดคำสั่งของระบบ

>> pip install aspose-imaging-python-net

ขั้นตอนในการเปลี่ยนพื้นหลังใน EMFs ผ่าน Python

คุณต้องใช้ aspose-imaging-python-net เพื่อลองใช้เวิร์กโฟลว์ต่อไปนี้ในสภาพแวดล้อมของคุณเอง

  • โหลดไฟล์ EMF ด้วยวิธี Image.Load
  • เปลี่ยนพื้นหลัง;
  • บันทึกภาพลงแผ่นดิสก์ในรูปแบบที่รองรับโดย Aspose.Imaging

ความต้องการของระบบ

Aspose.Imaging สำหรับ Python ได้รับการสนับสนุนในระบบปฏิบัติการหลักทั้งหมด เพียงตรวจสอบให้แน่ใจว่าคุณมีข้อกำหนดเบื้องต้นดังต่อไปนี้

  • Microsoft Windows / Linux พร้อม .NET Core Runtime
  • ตัวจัดการแพ็คเกจ Python และ PyPi
 

เปลี่ยนพื้นหลังในรูปภาพ EMF - 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 สำหรับ Python API

    Aspose.Imaging API เป็นโซลูชันการประมวลผลรูปภาพเพื่อสร้าง แก้ไข วาดหรือแปลงรูปภาพ (ภาพถ่าย) ภายในแอปพลิเคชัน นำเสนอ: การประมวลผลภาพข้ามแพลตฟอร์ม รวมถึงแต่ไม่จำกัดเพียงการแปลงระหว่างรูปแบบภาพต่างๆ (รวมถึงการประมวลผลภาพแบบหลายหน้าหรือหลายเฟรมแบบเดียวกัน) การปรับเปลี่ยน เช่น การวาด การทำงานกับภาพกราฟิกดั้งเดิม การแปลงภาพ (ปรับขนาด ครอบตัด พลิกและหมุน , ไบนารี, ระดับสีเทา, ปรับ), คุณสมบัติการจัดการภาพขั้นสูง (การกรอง, การแยกสี, การปิดบัง, การเดสก์) และกลยุทธ์การปรับหน่วยความจำให้เหมาะสม เป็นไลบรารีแบบสแตนด์อโลนและไม่ขึ้นกับซอฟต์แวร์ใด ๆ สำหรับการทำงานของรูปภาพ คุณสามารถเพิ่มคุณสมบัติการแปลงรูปภาพประสิทธิภาพสูงด้วย API ดั้งเดิมภายในโปรเจ็กต์ได้อย่างง่ายดาย สิ่งเหล่านี้เป็น API ภายในองค์กรที่เป็นส่วนตัว 100% และอิมเมจได้รับการประมวลผลที่เซิร์ฟเวอร์ของคุณ

    เปลี่ยนพื้นหลังใน EMF ผ่านแอปออนไลน์

    เปลี่ยนพื้นหลังในเอกสาร EMF โดยไปที่ เว็บไซต์ Live Demos การสาธิตสดมีประโยชน์ดังต่อไปนี้

      ไม่จำเป็นต้องดาวน์โหลดหรือตั้งค่าอะไรเลย
      ไม่ต้องเขียนโค้ดใดๆ
      เพียงอัปโหลดไฟล์ EMF แล้วกดปุ่ม เปลี่ยนพื้นหลังทันที
      รับลิงค์ดาวน์โหลดทันทีสำหรับไฟล์ผลลัพธ์

    EMF คืออะไร EMF รูปแบบไฟล์

    รูปแบบเมตาไฟล์ที่ปรับปรุงแล้ว (EMF) จัดเก็บภาพกราฟิกแบบแยกจากอุปกรณ์ Metafiles ของ EMF ประกอบด้วยเร็กคอร์ดความยาวผันแปรตามลำดับเวลาซึ่งสามารถแสดงภาพที่เก็บไว้หลังจากแยกวิเคราะห์บนอุปกรณ์ส่งออกใด ๆ เร็กคอร์ดความยาวผันแปรเหล่านี้สามารถเป็นคำจำกัดความของออบเจ็กต์ที่ล้อมรอบ คำสั่งสำหรับการวาดภาพ และคุณสมบัติกราฟิกที่สำคัญอย่างยิ่งต่อการแสดงภาพอย่างถูกต้อง เมื่ออุปกรณ์เปิดเมตาไฟล์ EMF โดยใช้สภาพแวดล้อมกราฟิกของตัวเอง สัดส่วน ขนาด สี และคุณสมบัติกราฟิกอื่นๆ ของภาพต้นฉบับจะยังคงเหมือนเดิมโดยไม่คำนึงถึงแพลตฟอร์มอุปกรณ์ที่เปิดอยู่

    อ่านเพิ่มเติม

    รูปแบบพื้นหลังการเปลี่ยนแปลงอื่น ๆ ที่รองรับ

    การใช้ Python คุณสามารถเปลี่ยนพื้นหลังในรูปแบบต่างๆ ได้อย่างง่ายดายรวมถึง

    APNG (กราฟิกเครือข่ายแบบพกพาแบบเคลื่อนไหว)
    BMP (รูปภาพบิตแมป)
    ICO (ไอคอน Windows)
    JPG (กลุ่มผู้เชี่ยวชาญด้านการถ่ายภาพร่วม)
    DIB (บิตแมปอิสระของอุปกรณ์)
    DICOM (การถ่ายภาพและการสื่อสารดิจิทัล)
    DJVU (รูปแบบกราฟิก)
    DNG (ภาพกล้องดิจิตอล)
    EMZ (Windows บีบอัด Metafile ที่ปรับปรุงแล้ว)
    GIF (รูปแบบการแลกเปลี่ยนกราฟิก)
    JP2 (JPEG 2000)
    J2K (ภาพบีบอัดเวฟเล็ต)
    PNG (กราฟิกเครือข่ายแบบพกพา)
    TIFF (รูปแบบภาพที่ติดแท็ก)
    WEBP (รูปภาพเว็บแรสเตอร์)
    WMF (Microsoft Windows Metafile)
    WMZ (สกิน Windows Media Player ที่บีบอัด)
    TGA (Targa Graphic)
    SVG (กราฟิกแบบเวกเตอร์ที่ปรับขนาดได้)
    EPS (ภาษา PostScript ที่ห่อหุ้ม)
    CDR (วาดภาพเวกเตอร์)
    CMX (รูปภาพ Corel Exchange)
    OTG (มาตรฐาน OpenDocument)
    ODG (รูปแบบการวาด Apache OpenOffice)