【问题标题】:Take an image directory and resize all images in the directory获取一个图像目录并调整目录中所有图像的大小
【发布时间】:2019-07-28 11:44:47
【问题描述】:

我正在尝试将高分辨率图像转换为更易于机器学习管理的图像。目前我有代码可以将图像调整为我想要的高度和宽度但是我必须一次做一个图像当我只做 12-24 个图像时这还不错但很快我想放大做几百张图片。 我正在尝试读取目录而不是单个图像并将新图像保存在新目录中。初始图像将与 .jpg、.png、.tif 等不同,但我想将所有输出图像都设为 .png,就像我在代码中所做的那样。

import os
from PIL import Image

filename = "filename.jpg"
size = 250, 250
file_parts = os.path.splitext(filename)

outfile = file_parts[0] + '_250x250' + file_parts[1]
try:
    img = Image.open(filename)
    img = img.resize(size, Image.ANTIALIAS)
    img.save(outfile, 'PNG')
except IOError as e:
    print("An exception occured '%s'" %e)

感谢您对此问题的任何帮助。

【问题讨论】:

标签: python image image-resizing python-3.7


【解决方案1】:

假设您正在寻找的解决方案是同时处理多个图像 - 这是一个解决方案。请参阅here 了解更多信息。

from multiprocessing import Pool

def handle_image(image_file):
    print(image_file)
    #TODO implement the image manipulation here

if __name__ == '__main__':
    p = Pool(5) # 5 as an example
    # assuming you know how to prepare image file list
    print(p.map(handle_image, ['a.jpg', 'b.jpg', 'c.png'])) 

【讨论】:

    【解决方案2】:

    你可以用这个:

    #!/usr/bin/python                                                  
    from PIL import Image                                              
    import os, sys                       
    
    path = "\\path\\to\\files\\"
    dirs = os.listdir( path )                                       
    
    def resize():
        for item in dirs:
            if os.path.isfile(path+item):
                im = Image.open(path+item)
                f, e = os.path.splitext(path+item)
                imResize = im.resize((200,100), Image.ANTIALIAS)
                imResize.save(f+'.png', 'png', quality=80)
    
    resize()
    

    【讨论】:

      【解决方案3】:

      您可以使用

      循环遍历目录的内容
      import os
      
      for root, subdirs, files in os.walk(MY_DIRECTORY):
          for f in files:
              if f.endswith('png'):
                  #do something 
      

      【讨论】:

        【解决方案4】:

        您可以使用glob 浏览目录中的所有图像。然后使用opencv 调整图像的大小,如下所示或使用PIL 所做的那样。

        import glob
        import cv2
        import numpy as np
        
        IMG_DIR='home/xx/imgs'
        def read_images(directory):
            for img in glob.glob(directory+"/*.png"):
                image = cv2.imread(img)
                resized_img = cv2.resize(image/255.0  , (250 , 250))
        
                yield resized_img
        
        resized_imgs =  np.array(list(read_images(IMG_DIR)))
        

        【讨论】:

          【解决方案5】:

          我用过:

          from PIL import Image
          import os, sys
          
          path = os.path.dirname(os.path.abspath(__file__))
          dirs = os.listdir( path )
          final_size = 244
          print(dirs)
          
          def resize_aspect_fit():
              for item in dirs:
                   if ".PNG" in item:
                       print(item)
                       im = Image.open(path+"\\"+item)
                       f, e = os.path.splitext(path+"\\"+item)
                       size = im.size
                       print(size)
                       ratio = float(final_size) / max(size)
                       new_image_size = tuple([int(x*ratio) for x in size])
                       im = im.resize(new_image_size, Image.ANTIALIAS)
                       new_im = Image.new("RGB", (final_size, final_size))
                       new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
                       print(f)
                       new_im.save(f + 'resized.jpg', 'JPEG', quality=400)# png
          resize_aspect_fit()
          

          【讨论】:

            【解决方案6】:

            您可以使用此代码调整多个图像的大小并在转换后将它们保存在同一文件夹中,假设尺寸为 (200,200):

            import os
            from PIL import Image
            
            f = r' '      #Enter the location of your Image Folder
                
            new_d = 200
            
            for file in os.listdir(f):
                f_img = f+'/'+file
                try:
                    img = Image.open(f_img)
                    img = img.resize((new_d, new_d))
                    img.save(f_img)
                except IOError:
                    pass
            

            【讨论】:

              【解决方案7】:

              您可以尝试使用 PIL 库在 python 中调整图像大小

              import PIL
              import os
              import os.path
              from PIL import Image
              
              
              path = r'your images path here'
              for file in os.listdir(path): 
                  f_img = path+"/"+file
                  img = Image.open(f_img)
                  img = img.resize((100, 100)) #(width, height)
                  img.save(f_img)
              

              【讨论】:

              • 我需要一个多页代码,以便输出也应该作为多页 tiff 图像......
              猜你喜欢
              • 2021-08-23
              • 2010-11-06
              • 1970-01-01
              • 2014-08-22
              • 1970-01-01
              • 2019-05-19
              • 2019-06-28
              • 2021-07-13
              • 1970-01-01
              相关资源
              最近更新 更多