查找所有方形尺寸(1:1比例)的图像

baf*_*mca 3 ruby python bash imagemagick find

是否可以使用任何*nix程序,如'find'或Python,PHP或Ruby等脚本语言,可以搜索您的硬盘并找到所有具有相同宽度和高度的图像,即方形尺寸?

Mar*_*agh 6

下面的代码将递归列出指定路径上的文件,因此它可以查看您提到的特定硬盘上的所有子文件夹.它还将根据您可以指定的一组文件扩展名检查文件是否为图像.然后它将打印具有匹配宽度和高度的任何图像的文件名和宽度,高度.当您调用脚本时,您可以指定要在其下搜索的路径.示例用法如下所示.

listimages.py

import PIL.Image, fnmatch, os, sys

EXTENSIONS = ['.jpg', '.bmp']

def list_files(path, extensions):
    for root, dirnames, filenames in os.walk(path):
      for file in filenames:
          if os.path.splitext(file)[1].lower() in extensions:
              yield os.path.join(root, file)

for file in list_files(sys.argv[1], EXTENSIONS):
    width, height = PIL.Image.open(file).size
    if width == height:
        print "found %s %sx%s" % (file, width, height)
Run Code Online (Sandbox Code Playgroud)

用法

# listimages.py /home/user/myimages/
found ./b.jpg 50x50
found ./a.jpg 340x340
found ./c.bmp 50x50
found ./d.BMP 50x50
Run Code Online (Sandbox Code Playgroud)


Mic*_*vis 5

使用Python肯定是可能的.

您可以使用os.walk来遍历文件系统,并使用PIL检查图像是否在两个方向上具有相同的尺寸.

import os, Image

for root, dir, file in os.walk('/'):
    filename = os.path.join(root, file)
    try:
        im = Image.open(filename)
    except IOError:
        continue

    if im.size[0] == im.size[1]:
        print filename
Run Code Online (Sandbox Code Playgroud)