Stitching Photos together

前端 未结 5 664
长情又很酷
长情又很酷 2020-12-05 03:31

So for this project I\'m working on, I have 2 photos. These two photos need to be stitched together, one on the top and one on the bottom, and then you will be able to see t

相关标签:
5条回答
  • 2020-12-05 03:37

    The python imaging library will eat that task for breakfast.

    See the tutorial in particular the "Cutting, Pasting and Merging Images" section for some relevant help.

    For rough outline, load both images with Image.open, find out how big the output image will be by using the size attribute and some addition, create the output image with Image.new and then use the paste method to past the two original images in.

    0 讨论(0)
  • 2020-12-05 03:53

    This is some code from Jan Erik Solems computer vision with python book; you can probably edit it to fit your top/bottom needs

    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)
    
    0 讨论(0)
  • 2020-12-05 03:58

    Use numpy.hstack() or numpy.vstack() based on whether you want the images next to each other or on top of each other. You can transform your images into numpy arrays if they are some weird format that numpy doesn't accept. Make sure that you set dtype=np.uint8 if you interpret images as arrays using the np.asarray() method.

    0 讨论(0)
  • 2020-12-05 04:00

    I did vertical stitching like this - it is same code from @d3ming

    07: def merge_images(file1, file2):
    08:     """Merge two images into one vertical image
    09:     :param file1: path to first image file
    10:     :param file2: path to second image file
    11:     :return: the merged Image object
    12:     """
    13:     image1 = Image.open(file1)
    14:     image2 = Image.open(file2)
    15: 
    16:     (width1, height1) = image1.size
    17:     (width2, height2) = image2.size
    18: 
    19:     # result_width = width1 + width2
    20:     result_width = width1
    21:     # result_height = max(height1, height2)
    22:     result_height = height1 + height2
    23: 
    24:     print (height2)
    25: 
    26:     result = Image.new('RGB', (result_width, result_height))
    27:     result.paste(im=image1, box=(0, 0))
    28:     result.paste(im=image2, box=(0, height1))
    29:     return result
    

    line 19-22 - only height will change for stitching

    paste the second image on line-28 box=(width1, 0) changes to box=(0, height1)

    0 讨论(0)
  • 2020-12-05 04:01

    Here's a code sample using Pillow. Hope it helps someone!

    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
    
    0 讨论(0)
提交回复
热议问题