รวมรูปภาพใน Python

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

Python Library เพื่อรวมรูปภาพ

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

> pip install aspose-imaging-python-net 

รวมหลายภาพใน Python

มีสองวิธีในการผสานรูปภาพ: แนวตั้งและแนวนอน คุณอาจเลือกวิธีที่เหมาะสม มาดูวิธีการรวมภาพทั้งสองวิธีในหัวข้อต่อไปนี้

รวมรูปภาพในแนวนอนใน Python

ด้านล่างนี้เป็นขั้นตอนในการผสานรูปภาพในแนวนอนโดยใช้ Aspose.Imaging for Python

  • เริ่มต้นด้วยการระบุเส้นทางของรูปภาพในอาร์เรย์
  • จากนั้นคำนวณความสูงและความกว้างของรูปภาพผลลัพธ์
  • สร้างวัตถุของคลาส JpegOptions และตั้งค่าตัวเลือกที่จำเป็น
  • สร้างวัตถุของคลาส JpegImage และเริ่มต้นด้วยวัตถุ JpegOptions และความสูงและความกว้างของภาพผลลัพธ์
  • วนซ้ำรายการรูปภาพและโหลดแต่ละภาพโดยใช้คลาส RasterImage
  • สร้างสี่เหลี่ยมผืนผ้าสำหรับแต่ละภาพและเพิ่มลงในภาพผลลัพธ์โดยใช้เมธอด JpegImage.saveargb32pixels()
  • เพิ่มความกว้างของตะเข็บในการวนซ้ำแต่ละครั้ง
  • สุดท้าย บันทึกภาพผลลัพธ์โดยใช้เมธอด JpegImage.save(string)

ด้านล่างนี้คือโค้ดสำหรับผสานรูปภาพในแนวนอนใน Python

import aspose.pycore as aspycore
from aspose.imaging import Image, Rectangle, RasterImage
from aspose.imaging.imageoptions import JpegOptions
from aspose.imaging.sources import FileCreateSource
from aspose.imaging.fileformats.jpeg import JpegImage
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
data_dir = templates_folder
image_paths = [os.path.join(data_dir, "template.jpg"), 
			   os.path.join(data_dir, "template.jpeg")]
output_path = os.path.join(data_dir, "result.jpg")
temp_file_path = os.path.join(data_dir, "temp.jpg")

# รับขนาดภาพที่ได้
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_width += size.width
	new_height = max(new_height, size.height)
	
# รวมภาพเข้าด้วยกันใหม่
temp_file_source = FileCreateSource(temp_file_path, delete_output)
with JpegOptions() as options:
	options.source = temp_file_source
	options.quality = 100
	with aspycore.as_of(Image.create(options, new_width, new_height), JpegImage) as new_image:
		stitched_width = 0
		for image_path in image_paths:
			with aspycore.as_of(Image.load(image_path), RasterImage) as image:
				bounds = Rectangle(stitched_width, 0, image.width, image.height)
				new_image.save_argb_32_pixels(bounds, image.load_argb_32_pixels(image.bounds))
				stitched_width += image.width
		new_image.save(output_path)

# ลบไฟล์ชั่วคราว
if delete_output:
	os.remove(output_path)
	if os.path.exists(temp_file_path):
		os.remove(temp_file_path)

ด้านล่างนี้เป็นภาพผลลัพธ์ที่เราได้รับหลังจากการรวมภาพในแนวนอน

รวมรูปภาพในแนวนอนใน Python

รวมรูปภาพในแนวตั้งใน Python

มาดูวิธีการรวมภาพหลายภาพในแนวตั้งกัน ขั้นตอนการรวมภาพในแนวตั้งจะเหมือนกับในส่วนที่แล้ว ข้อแตกต่างเพียงอย่างเดียวคือ เราจะสลับบทบาทของคุณสมบัติความสูงและความกว้าง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการรวมรูปภาพในรูปแบบแนวตั้งใน Python

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

# กำหนดโฟลเดอร์
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
data_dir = templates_folder
image_paths = [os.path.join(data_dir, "template.jpg"), os.path.join(data_dir, "template.jpeg")]
output_path = os.path.join(data_dir, "result.jpg")
temp_file_path = os.path.join(data_dir, "temp.jpg")

# รับขนาดภาพที่ได้
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)
	
# รวมภาพเป็นภาพใหม่
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)

# ลบไฟล์ชั่วคราว
if delete_output:
	os.remove(output_path)

ภาพต่อไปนี้แสดงผลลัพธ์ของการรวมภาพที่คล้ายกันสองภาพในแนวตั้ง

รวมรูปภาพในแนวตั้งใน Python

รวมรูปภาพ PNG ใน Python

ขั้นตอนและตัวอย่างโค้ดที่ให้ไว้ในส่วนก่อนหน้านี้มีไว้สำหรับการรวมภาพ JPG อย่างไรก็ตาม คุณอาจต้องผสานภาพ PNG ด้วย ไม่ต้องกังวล ขั้นตอนและตัวอย่างโค้ดเดียวกันนี้ใช้ได้กับอิมเมจ PNG และการเปลี่ยนแปลงเพียงอย่างเดียวคือการใช้คลาส PngImage และ PngOptions แทน JpegImage และ JpegOptions ตามลำดับ

รับไลบรารีการผสานรูปภาพ Python ฟรี

คุณสามารถ รับใบอนุญาตชั่วคราวได้ฟรี และรวมรูปภาพโดยไม่มีข้อจำกัดในการประเมิน

รวมรูปภาพออนไลน์

คุณยังสามารถใช้เครื่องมือรวมรูปภาพฟรี เพื่อรวมรูปภาพของคุณทางออนไลน์ เครื่องมือนี้ใช้ Aspose.Imaging for Python และคุณไม่จำเป็นต้องสร้างบัญชีสำหรับมัน

บทสรุป

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

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

ดูสิ่งนี้ด้วย