Pythonで写真を結合する

このガイドでは、Python で photos をマージする の詳細について説明します。環境設定、ステップバイステップのプログラム フロー、および Python で写真結合ツール を作成するための実用的なサンプル コードについて説明します。さらに、この機能をプログラムに埋め込むために追加の画像処理アプリケーションは必要ありません。

Pythonで写真を結合する手順

  1. 写真を結合するために Aspose.Imaging for Python を設定してシステムを準備します
  2. ソース画像のリストを作成し、結合した画像の寸法を計算します
  3. 読み込んだ画像を結合し、出力ソースを定義する
  4. JpegOptions クラスのカスタム プロパティを設定する
  5. JpegImageクラスのオブジェクトを使用して出力マージ画像をエクスポートします。

上記の手順は、Python で写真を結合する プロセスを要約したものです。まず、1 つの画像に結合するために、さまざまな画像をリストする必要があります。次に、アプリケーションの設計に基づいて、ディスクまたはストリームにエクスポートしながら出力画像をレンダリングする前に、新しい画像のサイズと内容を計算します。

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で画像をトリミングする の記事を参照してください。

 日本語