รวมภาพถ่ายใน Python

คู่มือนี้ครอบคลุมรายละเอียดเกี่ยวกับการ รวม photos ใน Python โดยจะอธิบายการตั้งค่าสภาพแวดล้อม ขั้นตอนการทำงานของโปรแกรม และตัวอย่างโค้ดที่ใช้ในการสร้าง ตัวรวมภาพใน Python นอกจากนี้ ไม่จำเป็นต้องมีแอปพลิเคชันประมวลผลภาพเพิ่มเติมเพื่อฝังฟีเจอร์นี้ในโปรแกรมของคุณ

ขั้นตอนการรวมภาพถ่ายใน Python

  1. เตรียมระบบโดยกำหนดค่า Aspose.Imaging for Python เพื่อรวมภาพถ่าย
  2. สร้างรายการภาพต้นฉบับและคำนวณขนาดสำหรับภาพที่ผสานกัน
  3. รวมรูปภาพที่โหลดและกำหนดแหล่งเอาท์พุต
  4. ตั้งค่าคุณสมบัติที่กำหนดเองของคลาส JpegOptions
  5. ส่งออกภาพรวมเอาท์พุตที่มีคลาสอ็อบเจ็กต์ JpegImage

ขั้นตอนข้างต้นเป็นการสรุปกระบวนการในการ รวมรูปภาพใน Python ขั้นแรก คุณต้องแสดงรายการรูปภาพต่างๆ เพื่อรวมเป็นรูปภาพเดียว จากนั้นคำนวณขนาดและเนื้อหาของรูปภาพใหม่ก่อนเรนเดอร์รูปภาพเอาต์พุตขณะส่งออกไปยังดิสก์หรือสตรีมตามการออกแบบแอปพลิเคชันของคุณ

โค้ดสำหรับสร้าง Image Combiner ใน Python

import aspose.imaging
import aspose.pycore as aspycore
from aspose.imaging import Image, Rectangle, RasterImage
from aspose.imaging.imageoptions import JpegOptions
from aspose.imaging.sources import StreamSource
from aspose.imaging.fileformats.jpeg import JpegImage
from aspose.imaging.extensions import StreamExtensions
import os
import functools
data_dir = "C://SampleFiles//"
license = aspose.imaging.License()
license.set_license(data_dir + "License.lic")
delete_output = 'SAVE_OUTPUT' not in os.environ
#data_dir = templates_folder
image_paths = [os.path.join(data_dir, "grayscaled.jpg"), os.path.join(data_dir, "grayscaled.jpg")]
output_path = os.path.join(data_dir, "mergedresult.jpg")
temp_file_path = os.path.join(data_dir, "tempmerge.jpg")
# Getting resulting image size.
image_sizes = []
for image_path in image_paths:
with Image.load(image_path) as image:
image_sizes.append(image.size)
new_width = 0
new_height = 0
for size in image_sizes:
new_height += size.height
new_width = max(new_width, size.width)
# Combining images into new one.
with StreamExtensions.create_memory_stream() as memory_stream:
output_stream_source = StreamSource(memory_stream)
with JpegOptions() as options:
options.source = output_stream_source
options.quality = 100
with aspycore.as_of(Image.create(options, new_width, new_height), JpegImage) as new_image:
stitched_height = 0
for image_path in image_paths:
with aspycore.as_of(Image.load(image_path), RasterImage) as image:
bounds = Rectangle(0, stitched_height, image.width, image.height)
new_image.save_argb_32_pixels(bounds, image.load_argb_32_pixels(image.bounds))
stitched_height += image.height
new_image.save(output_path)

โค้ดสั้นๆ นี้เพียงพอสำหรับการสร้าง ตัวรวมรูปภาพพื้นฐานใน Python โดยทำงานร่วมกับเมธอดต่างๆ ที่เปิดเผยโดยคลาส JpegImage เพื่อสร้างรูปภาพเอาต์พุตและอ่านขนาดของรูปภาพอินพุต จากนั้นจะกำหนดรูปสี่เหลี่ยมผืนผ้าและเรนเดอร์รูปภาพที่สร้างขึ้นโดยเรียกใช้เมธอด Save นอกจากนี้ คุณยังสามารถปรับแต่งรูปภาพที่สร้างขึ้นตามความต้องการของคุณได้ด้วยการตั้งค่าปัจจัยมาตราส่วน โปรไฟล์สี RGB โหมดการปัดเศษตัวอย่าง เป็นต้น

ในบทความนี้ คุณจะได้เรียนรู้เกี่ยวกับการผสานภาพใน Python แล้ว หากคุณต้องการเรียนรู้เกี่ยวกับการครอบตัดภาพ โปรดอ่านบทความใน ครอบตัดรูปภาพใน Python

 ไทย