26

私が取り組んでいるこのプロジェクトでは、2枚の写真があります。これらの 2 枚の写真は、1 枚を上に、もう 1 枚を下につなぎ合わせる必要があります。そうすれば、全体像を見ることができます。これを行うためにどのモジュールを使用する必要があるかについてのアイデアはありますか?

4

5 に答える 5

34

Pillowを使用したコードサンプルを次に示します。それが誰かを助けることを願っています!

from PIL import Image

def merge_images(file1, file2):
    """Merge two images into one, displayed side by side
    :param file1: path to first image file
    :param file2: path to second image file
    :return: the merged Image object
    """
    image1 = Image.open(file1)
    image2 = Image.open(file2)

    (width1, height1) = image1.size
    (width2, height2) = image2.size

    result_width = width1 + width2
    result_height = max(height1, height2)

    result = Image.new('RGB', (result_width, result_height))
    result.paste(im=image1, box=(0, 0))
    result.paste(im=image2, box=(width1, 0))
    return result
于 2015-12-16T00:02:23.467 に答える
25

Python イメージング ライブラリ(更新されたリンク)は、朝食にそのタスクを実行します。

関連するヘルプについては、チュートリアル、特に「画像の切り取り、貼り付け、およびマージ」セクションを参照してください。

大まかなアウトラインは、 で両方の画像をロードし、属性といくつかの追加Image.openを使用して出力画像の大きさを調べ、 で出力画像を作成し、メソッドを使用して 2 つの元の画像を貼り付けます。sizeImage.newpaste

于 2012-05-18T17:52:39.690 に答える
3

これは、Jan Erik Solems のコンピューター ビジョン with python book からのコードです。おそらく、上/下のニーズに合わせて編集できます

def stitchImages(im1,im2):
    '''Takes 2 PIL Images and returns a new image that 
    appends the two images side-by-side. '''

    # select the image with the fewest rows and fill in enough empty rows
    rows1 = im1.shape[0]    
    rows2 = im2.shape[0]

    if rows1 < rows2:
        im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1]))), axis=0)
    elif rows1 > rows2:
        im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1]))), axis=0)
    # if none of these cases they are equal, no filling needed.

    return concatenate((im1,im2), axis=1)
于 2013-08-18T02:40:51.640 に答える