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

ใช้ Python สำหรับ ICO ลบพื้นหลังรูปภาพ

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

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

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

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

>> pip install aspose-imaging-python-net

ขั้นตอนในการลบพื้นหลังจาก ICO ผ่าน Python

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

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

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

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

  • Microsoft Windows / Linux พร้อม .NET Core Runtime
  • ตัวจัดการแพ็คเกจ 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 สำหรับ Python API

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

    ลบพื้นหลังใน ICO ผ่านแอปออนไลน์

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

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

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

    รูปแบบไฟล์ ICO เป็นรูปแบบไฟล์รูปภาพสำหรับไอคอนคอมพิวเตอร์ใน Microsoft Windows ไฟล์ ICO มีรูปภาพขนาดเล็กตั้งแต่หนึ่งภาพขึ้นไปที่หลายขนาดและความลึกของสี เพื่อให้สามารถปรับขนาดได้อย่างเหมาะสม ใน Windows โปรแกรมเรียกทำงานทั้งหมดที่แสดงไอคอนแก่ผู้ใช้ บนเดสก์ท็อป ในเมนูเริ่ม หรือใน Windows Explorer ต้องมีไอคอนในรูปแบบ ICO

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

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

    เมื่อใช้ Python ผู้ใช้จะลบพื้นหลังออกจากรูปแบบต่างๆ ได้อย่างง่ายดาย เช่น

    APNG (กราฟิกเครือข่ายแบบพกพาแบบเคลื่อนไหว)
    BMP (รูปภาพบิตแมป)
    JPG (กลุ่มผู้เชี่ยวชาญด้านการถ่ายภาพร่วม)
    DIB (บิตแมปอิสระของอุปกรณ์)
    DICOM (การถ่ายภาพและการสื่อสารดิจิทัล)
    DJVU (รูปแบบกราฟิก)
    DNG (ภาพกล้องดิจิตอล)
    EMF (รูปแบบไฟล์ Metafile ที่ปรับปรุงแล้ว)
    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)