对大量图像文件进行分类

时间:2019-05-20 02:55:21

标签: r image-processing imagemagick rmagick

我有一本书的图像文件很多,出版商想要一个列表,其中按“类型”(灰度图,黑白半色调图像,彩色图像,线条图等)对文件进行分类。一般来说,这是一个难题,但是也许我可以使用图像处理工具(例如带有R magick软件包的ImageMagick)自动完成其中的一些工作。

我认为ImageMagick是正确的工具,但我真的不知道如何为此目的使用它。

我所拥有的只是无花果编号和文件名的列表:

1.1 ch01-intro/fig/alcohol-risk.jpg
1.2 ch01-intro/fig/measels.png
1.3 ch01-intro/fig/numbers.png
1.4 ch01-intro/fig/Lascaux-bull-chamber.jpg
...

有人可以帮助我入门吗?

编辑:如最初所说,这可能是框架错误或过于曲折的问题。我认为ImageMagick identify或R magick::image_info()函数可能会有所帮助,因此最初的问题也许应该是:“如何从[R]中的文件列表中提取图像信息”。如果尚未提出要求,我可以单独提出。

最初的尝试使我获得了以下第一张图片,

library(magick)

# initialize an empty array to hold the results of `image_info`
figinfo <- data.frame(
  format=character(),
  width=numeric(),
  height=numeric(),
  colorspace=character(),
  matte=logical(),
  filesize=numeric(),
  density=character(), stringsAsFactors = FALSE
)

for (i in seq_along(files)) {
  img <- image_read(files[i])
  info <- image_info(img)
  figinfo[i,] <- info
}

我得到:

> figinfo
  format width height colorspace matte filesize density
1   JPEG   661    733       sRGB FALSE    41884   72x72
2    PNG   838    591       sRGB  TRUE    98276   38x38
3    PNG   990    721       sRGB  TRUE   427253   38x38
4   JPEG   798    219       sRGB FALSE    99845 300x300

我得出结论,这对于回答我提出的有关如何对这些图像进行分类的问题没有多大帮助。

Edit2 在结束此问题之前,建议直接使用ImageMagick identify的建议很有帮助。 https://imagemagick.org/script/escape.php 特别是,%[type]更接近 我需要的。 magick::image_info()中没有公开此内容,因此我可能必须编写一个Shell脚本或在循环中调用system()

作为记录,这是我如何直接使用identify提取这些图像文件的相关属性。

# Get image characteristics via ImageMagick identify
# from: https://imagemagick.org/script/escape.php
#
# -format elements:
# %m image file format
# %f filename
# %[type] image type
# %k number of unique colors
# %h image height in pixels
# %r image class and colorspace

identify -format "%m,%f,%[type],%r,%k,%hx%w" imagefile

>identify -format "%m,%f,%[type],%r,%k,%hx%w" Quipu.png
PNG,Quipu.png,GrayscaleAlpha,DirectClass Gray Matte,16,449x299

%[type]属性将我带到想要的地方。

1 个答案:

答案 0 :(得分:0)

要结束此问题:

在R环境中,我成功使用system(, intern=TRUE)进行了以下操作,并进行了一些手动修复

# Use identify directly via system()

# function to run identify for one file
get_info <- function(file) {
  cmd <- 'identify -quiet -format "%f,%m,%[type],%r,%h,%w,%x,%y"'
  info <- system(paste(cmd, file), intern=TRUE)
  unlist(strsplit(info, ","))
}

# This doesn't cause coercion to numeric
figinfo <- data.frame(
  filename=character(),
  format=character(),
  type=character(),
  class=character(),
  height=numeric(),
  width=numeric(),
  xres=numeric(),
  yres=numeric(),
  stringsAsFactors = FALSE
)


for (i in seq_along(files)) {
  info <- get_info(files[i])
  info[4] <- sub("DirectClass ", "", info[4])
 figinfo[i,] <- info
}

figinfo$height <- as.numeric(figinfo$height)
figinfo$width <- as.numeric(figinfo$width)
figinfo$xres=round(as.numeric(figinfo$xres))
figinfo$yres=round(as.numeric(figinfo$yres))

然后我或多或少有想要的东西:

> str(figinfo)
'data.frame':   161 obs. of  8 variables:
 $ filename: chr  "mileyears4.png" "alcohol-risk.jpg" "measels.png" "numbers.png" ...
 $ format  : chr  "PNG" "JPEG" "PNG" "PNG" ...
 $ type    : chr  "Palette" "TrueColor" "TrueColorAlpha" "TrueColorAlpha" ...
 $ class   : chr  "PseudoClass sRGB " "sRGB " "sRGB Matte" "sRGB Matte" ...
 $ height  : num  500 733 591 721 219 ...
 $ width   : num  720 661 838 990 798 ...
 $ xres    : num  72 72 38 38 300 38 300 38 28 38 ...
 $ yres    : num  72 72 38 38 300 38 300 38 28 38 ...
> 
相关问题