Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PIL-Python中合并和保存图像_Python_Image_Merge_Python Imaging Library - Fatal编程技术网

在PIL-Python中合并和保存图像

在PIL-Python中合并和保存图像,python,image,merge,python-imaging-library,Python,Image,Merge,Python Imaging Library,我有两个文件夹的图像,我试图建立一个大的图像从所有切片在每个文件夹。我需要在文件夹之间切换以生成图像。例如,第一个切片来自文件夹1,第二个切片来自文件夹2,第三个切片来自文件夹1等。我在各个文件夹中按文件名对文件进行了排序,因此我尝试在文件夹中进行迭代,并在图像中添加一个新的条带。代码运行,但似乎没有保存合成映像。我是PIL的新手,所以我相信这很简单,但非常感谢您的帮助。谢谢 def merge_images(file1, file2): """Merge two images into

我有两个文件夹的图像,我试图建立一个大的图像从所有切片在每个文件夹。我需要在文件夹之间切换以生成图像。例如,第一个切片来自文件夹1,第二个切片来自文件夹2,第三个切片来自文件夹1等。我在各个文件夹中按文件名对文件进行了排序,因此我尝试在文件夹中进行迭代,并在图像中添加一个新的条带。代码运行,但似乎没有保存合成映像。我是PIL的新手,所以我相信这很简单,但非常感谢您的帮助。谢谢

def merge_images(file1, file2):
   """Merge two images into one, displayed above and below
   :param file1: path to first image file
   :param file2: path to second image file
   :return: the merged Image object
   """

   if file1.startswith('.'):
       return None

   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=(0, height1))

   return result

imageCounter = 0
firstPass = True
compImage = Image.new('RGBA', img.size, (0,0,0,0))

for i in range(len(boundingBoxes)+1):
    if firstPass:
        compImage = merge_images('./image_processing/'+ os.listdir('./image_processing/')[imageCounter],
        './img_patches/outputs/'+os.listdir('./img_patches/outputs/')[imageCounter])


        if compImage is not None:
            firstPass = False

            compImage.save('./image_processing/compImage.jpg')


    else:
        compImage = merge_images('./image_processing/compImage.jpg','./image_processing/'+ os.listdir('./image_processing/')[imageCounter])

        compImage.save('./image_processing/compImage.jpg')
        compImage = merge_images('./image_processing/compImage.jpg','./img_patches/outputs/'+ os.listdir('./img_patches/outputs/')[imageCounter])

        compImage.save('./image_processing/compImage.jpg')

    imageCounter = imageCounter + 1

您必须告诉PIL保存图像:

for i in range(len(boundingBoxes)+1):
    if firstPass:
        compImage = merge_images('./image_processing/'+ os.listdir('./image_processing/')[imageCounter],
        './img_patches/outputs/'+os.listdir('./img_patches/outputs/')[imageCounter])
        compImage.save(open('output/{}.png'.format(imageCounter), 'w'))

@米尔顿·拉德利,实际上根本就没有救他们。