首页 文章

在python中将两张照片粘在一起

提问于
浏览
1

简短的问题,我有2张图片 . 一个通过以下方式导入
Image = mpimg.imread('image.jpg')
虽然另一个是上面导入的图像的处理图像,但是该图像首先从rgb转换为hls然后返回 . 此转换的结果给出"list",它与导入图像的uint8不同 . 当我试图将这些图像与功能结合在一起时:

new_img2[:height,width:width*2]=image2

通过绘制图像,我在组合图像中看不到第二个图像:

imgplot = plt.imshow(image2)
plt.show()

它工作正常 . 将orignal转换为“list”然后将它们或“list”组合到uint8的最佳方法是什么?

有关更多信息,结果必须是这样的:enter image description here

右边是黑色的,因为我尝试导入的图像有另一种类型的数组 . 左图是uint8而另一图是"list" . 第二个图像是这个,从python中保存:enter image description here

2 回答

  • 0

    不知道怎么做你上面显示的方式,但我一直能够合并和保存图像,如下所示!

    def mergeImages(image1, image2, dir):
    '''
        Merge Image 1 and Image 2 side by side and delete the origional
    
        '''
        #adding a try/except would cut down on directory errors. not needed if you know you will always open correct images
        if image1 == None:
            image1.save(dir)
            os.remove(image2)
            return 
    
        im1 = Image.open(image1) #open image
        im1.thumbnail((640,640)) #scales the image to 640, 480. Can be changed to whatever you need
        im2 = Image.open(image2) #open Image
        im1.thumbnail((640,480)) #Again scale
        new_im = Image.new('RGB', (2000,720)) #Create a blank canvas image, size can be changed for your needs
        new_im.paste(im1, (0,0)) #pasting image one at pos (0,0), can be changed for you
        new_im.paste(im2, (640,0)) #again pasting
        new_im.save(dir) #save image in defined directory
        os.remove(image1) #Optionally deleting the origonal images, I do this to save on space
        os.remove(image2)
    
  • 0

    经过一天的搜索,我发现两个变量都可以更改为float64的类型 . “list”变量:

    Image = np.asarray(Image)
    

    这将从List变量创建一个float 64 . 虽然uint8可以通过以下方式更改为float64:

    Image2=np.asarray(Image2/255)
    

    比2可以结合:

    totalImgage = np.hstack((Image,Image2))
    

    这比创建想要的图像 .

相关问题