【问题标题】:Find all images that have square dimensions (1:1 ratio) [closed]查找所有具有正方形尺寸的图像(1:1 比例)[关闭]
【发布时间】:2012-12-20 03:49:15
【问题描述】:

是否可以使用任何 *nix 程序(如“find”)或脚本语言(如 Python、PHP 或 Ruby)来搜索您的 HDD 并找到所有具有相同宽度和高度(即正方形尺寸)的图像?

【问题讨论】:

  • 什么样的图片,file命令会告诉你png和gif的尺寸
  • jpg 和 png。但是,您如何从“文件”返回尺寸并能够比较两者,然后仅在它们相同时才返回文件名?
  • 类似于find . -type f | xargs -I {} sh -c 'res=`identify -format "%w - %h" {} 2>|/dev/null`; if [ -n "$res" ] && [ `echo $res | bc` -eq 0 ]; then echo {}; fi' 的东西,用于慢速方法和 ImageMagick 支持的任何类型的图像。
  • 对...不是建设性的。你们这些人让我大吃一惊。

标签: python ruby bash find imagemagick


【解决方案1】:

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

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)

用法

# listimages.py /home/user/myimages/
found ./b.jpg 50x50
found ./a.jpg 340x340
found ./c.bmp 50x50
found ./d.BMP 50x50

【讨论】:

  • 如果有人没有用正确的扩展名命名他们的文件怎么办?
  • @mmgp 如果这是一个问题,那么您可以像迈克尔戴维斯在他的回答中所做的那样检查所有文件并尝试,除非它失败,从而忽略文件扩展名。但这会慢得多,因为它会打开并读取您计算机上的每个文件,而不仅仅是具有图像扩展名的文件。
【解决方案2】:

使用 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

【讨论】:

    【解决方案3】:

    在bash 中,您可以使用以下方法获取图像大小:

    identify -verbose jpg.jpg | awk '/Geometry/{print($2)}'
    

    另请阅读man find 和man identify

    【讨论】:

    • 使用 identify,你可以通过使用 identify -format "%w,%h" 删除 awk 部分,但他仍然需要比较这些值
    【解决方案4】:

    这可以在单个 shell 行中完成,但我不建议这样做。分两步进行。首先,将所有图像文件和所需属性收集到一个文件中:

    find . -type f -print0 | xargs -J fname -0 -P 4 identify \
        -format "%w,%h,%m,\"%i\"\n" fname 2>|/dev/null | sed '/^$/d' > image_list
    

    sed 只是为了删除产生的空白行。您可能希望为您的系统调整xargs 中的参数-P 4。这里使用了 ImageMagick 的identify,因为它可以识别很多格式。这将创建一个名为 image_list 的文件,该文件采用典型的 CSV 格式。

    现在只需根据您的需要过滤image_list。为此,我更喜欢使用 Python:

    import sys
    import csv
    
    EXT = ['JPEG', 'PNG']
    
    for width, height, fformat, name in csv.reader(open(sys.argv[1])):
        if int(width) == int(height) and width:
            # Show images with square dimensions, and discard
            # those with width 0
            if fformat in EXT:
                print name
    

    这个答案的第一部分可以很容易地用 Python 重写,但由于它涉及使用 Python 的 ImageMagick 绑定或通过subprocess 调用它,所以我将其保留为 shell 命令的组合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多