2012-05-18 4 views
13

그래서이 프로젝트에서 저는 2 장의 사진을 가지고 있습니다. 이 두 사진은 상단과 하단에 함께 꿰매어야하며 전체 그림을 볼 수 있습니다. 어떤 모듈을 사용해야하는지에 대한 아이디어가 있습니까?함께 사진 붙이기

+1

파노라마처럼 겹치나요? 또는 두 개의 작은 이미지를 서로 옆에 놓아 하나의 큰 이미지를 만들어야합니까? – Leopd

답변

19

python imaging library은 아침에 그 일을 먹을 것입니다.

특히 관련 도움말을 보려면 tutorial "이미지 자르기, 붙여 넣기 및 병합"섹션을 참조하십시오. 거친 윤곽에 대한

, Image.new와 출력 이미지를 생성, 출력 이미지가 size 속성과 일부 추가를 사용하여 얼마나 큰 알아, Image.open으로 두 이미지를로드 한 다음에 paste 방법을 사용하여 두 개의 원본 이미지 과거 in.

+1

조금 더 자세히 설명해 주시겠습니까? 내가 쓰는 기능처럼? 어떤 팁? –

+5

답변에 대한 힌트를 추가했습니다. 일반적으로 데모 프로그램을 작성하지만 긴 주간이었고 고급 알코올 음료가 유혹되었습니다 .-) –

+0

그레이트! 그것은 완벽했습니다. 도움을 주셔서 감사합니다 –

2

이것은 Python book의 Jan Erik Solems 컴퓨터 비전의 일부 코드입니다. 당신은 아마 당신의 상단에 맞게 편집 할 수 있습니다/바닥 여기

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) 
15

필요 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 
+3

이 코드가 작동합니다, 지금 방금 테스트했습니다. 이 코드를 사용하여 파일을 저장할 수 있습니다. -> merged = merge_images (file1, file2) merged.save (file_dest) – fedmich

+0

하나의 이미지에 대한 질문이 result_width와 함께 result_width의 동작을 뒤집는 것입니다. – Ywapom