【问题标题】:How to batch combine 2 photos into 1 in python如何在python中将2张照片批量合并为1张
【发布时间】:2018-11-04 19:09:05
【问题描述】:

我正在尝试找到一种方法将多对图像水平批量组合成一个图像。我有一个包含数百张照片(img01、img02 等)的文件夹,我正在尝试合并这些照片对:

  • img1 + img2 --> img1_2
  • img 519 + img 520 --> img519_520

图像为 3 x 4 英寸,我想将它们组合成成对的水平 6 x 4 英寸图像。

我找到了对 2 张特定照片执行此操作的方法,但我不知道如何为我的图像文件夹中的所有照片循环代码并将其另存为 6x4 照片。

import numpy as np
from PIL import Image
images_list = ['img52.JPG', 'img53.JPG']
imgs = [ Image.open(i) for i in images_list ]
min_img_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
img_merge = np.hstack( (np.asarray( i.resize(min_img_shape,Image.ANTIALIAS) ) for i in imgs )
img_merge = Image.fromarray( img_merge)
img_merge.save( 'Merged image.jpg' )

我使用的是 Mac OS High Sierra 10.13.4 和 python Python 3.6.4。

任何帮助将不胜感激!

以上代码来源:https://kanoki.org/2017/07/12/merge-images-with-python/

【问题讨论】:

    标签: merge python-imaging-library photos


    【解决方案1】:

    更新(截至 2018 年 4 月 12 日):

    以下内容适用于包含偶数张照片的文件夹,其中照片的名称为:img1.jpg、img2.jpg 等。脚本将拍摄一对照片(例如 img1.jpg 和 img2.jpg ) 并将它们组合起来。

    #Note: I run the program in terminal after I change directory to my folder.
    
    import numpy as np
    from PIL import Image
    images_list = []
    for i in range(1,78): #insert last number of photo + 1 (in this case, I have 77 photos)
      images_list.append('img'+str(i)+'.JPG')
    count = 1;
    
    directory = "path of directory"  #change to directory where your photos are
    ext = ".jpg"
    new_file_name = "vimage-" #change to what you would like to name your photos
    new_directory = "path of directory" # change to path of new directory where you want your photos to be saved
    
    
    for j in range(0,len(images_list),2):
      name = new_file_name + str(j) + ext
      two_images_list = [images_list[j],images_list[j+1]]
      imgs = [ Image.open(i) for i in two_images_list ]
      min_img_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
      imgs_comb = np.hstack( (np.asarray( i.resize(min_img_shape) ) for i in imgs ) )
      imgs_comb = Image.fromarray( imgs_comb)
      imgs_comb.save(new_directory+'/'+name )
      count +=1
    

    非常感谢@C。蒂姆的有用回复!

    【讨论】:

      【解决方案2】:

      更新(截至 2018 年 4 月 12 日):

      如果您的图片名称为“img01.JPG”、“img42.JPG”,则此代码应该可以工作: 您生成所有名称并将它们放在一个列表中。对于每一对图像,您的算法会将它们转换为一个图像,并且创建的图像的名称会随着每一对图像的变化而变化。

      您必须有偶数个图像,并且图像必须具有相同的尺寸。非常感谢@C。蒂姆的有用回应。

      # Note: I run the program in terminal after I change directory to my folder.
      
      import numpy as np
      from PIL import Image
      images_list = []
      for i in range(1,78): #insert last number of photo
          images_list.append('img'+str(i)+'.JPG')
      count = 1;
      
      directory = "path of photos to resize"  #change to directory where your photos are
      ext = ".jpg"
      new_file_name = "vimage-"
      new_directory = "path to save in" # change to path of new directory where you want your photos to be saved
      
      
      for j in range(0,len(images_list),2):
          name = new_file_name + str(j) + ext
          two_images_list = [images_list[j],images_list[j+1]]
          imgs = [ Image.open(i) for i in two_images_list ]
          min_img_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
          imgs_comb = np.hstack( (np.asarray( i.resize(min_img_shape) ) for i in imgs ) )
          imgs_comb = Image.fromarray( imgs_comb)
          imgs_comb.save(new_directory+'/'+name )
          count +=1
      

      【讨论】:

      • 嗨蒂姆,非常感谢您的回复!从“for i in range(10,#last image number+1)”行开始,我无法让代码正常工作。它返回一个无效的语法错误。有什么想法吗? :D
      • 是的!您必须将“# last image number”替换为图像文件的实际最后一个图像编号
      猜你喜欢
      • 2017-09-18
      • 2012-03-04
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2017-06-14
      • 1970-01-01
      相关资源
      最近更新 更多