【问题标题】:Combine groups of jpg images into one jpg/png r将多组 jpg 图像组合成一个 jpg/png r
【发布时间】:2021-09-22 16:20:16
【问题描述】:

我有一张图片列表,我想将这些图片组合成每组一张图片。每个图像都有一个名称和它是什么类型的图表。我希望有一种方法可以使用名称对图像进行分组,并为每个组返回单个 jpg 或 png 的输出。合并后的图像如下所示:

该文件夹有一个具有以下命名结构的图像列表:在这种情况下,我希望有 3 个基于 bart、lisa 和 marge 的输出组合 png。

"bart Cum Oil.jpg"
"bart GOR.jpg"
"bart Legend.jpg"
"bart Oil Rate.jpg"
"lisa Cum Oil.jpg"
"lisa GOR.jpg"
"lisa Legend.jpg"
"lisa Oil Rate.jpg"
"marge Cum Oil.jpg"
"marge GOR.jpg"
"marge Legend.jpg"
"marge Oil Rate.jpg"

下面是我将合并的四个图像的示例,因为所有图像的名称中都有“bart”。

【问题讨论】:

    标签: r imagemagick png jpeg


    【解决方案1】:

    一种选择是使用magick::image_montage。唯一的技巧是创建一个 2x2 网格并插入一个空白图像作为第三个对象。

    library(magick)
    #> Linking to ImageMagick 6.9.12.3
    #> Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
    #> Disabled features: fontconfig, x11
    
    imgs_url <- c('https://i.stack.imgur.com/TqnwC.png',
                  'https://i.stack.imgur.com/qIDXh.png',
                  'https://i.stack.imgur.com/B36ko.png')
    
    imgs <- image_read(imgs_url)
    imgs <- c(imgs[1:2], image_blank(width = 3, height = 5), imgs[3])
    
    image_montage(imgs, tile = '2x2', geometry = "x200+3+5")
    

    创建于 2021-07-13 由 reprex package (v2.0.0)


    如果要按名称对图像进行分组,一种方法是在函数中捕获上述工作流程,并添加 grepl 语句来标识要绘制的图像子集。

    我没有所有图片,因此未经测试。但它看起来像这样。

    my_files <- c("bart Cum Oil.jpg",
                  "bart GOR.jpg",
                  "bart Legend.jpg",
                  "bart Oil Rate.jpg",
                  "lisa Cum Oil.jpg",
                  "lisa GOR.jpg",
                  "lisa Legend.jpg",
                  "lisa Oil Rate.jpg",
                  "marge Cum Oil.jpg",
                  "marge GOR.jpg",
                  "marge Legend.jpg",
                  "marge Oil Rate.jpg")
    
    # remove oil rate image paths
    my_files <- my_files[!grepl('Oil Rate', my_files)]
    
    
    # create function 
    make_grid <- function(group, path_to_files){
      
      f <- files[grepl(group, files)]
      
      imgs <- image_read(f)
      imgs <- c(imgs[1:2], image_blank(width = 3, height = 5), imgs[3])
      
      image_montage(imgs, tile = '2x2', geometry = "x200+3+5")
    
    }
    
    
    # create lisa image grid
    make_grid('lisa', my_files)
    
    # create all image grids
    lapply(c('bart', 'lisa', 'marge'), make_grid, path_to_files = my_files)
    

    【讨论】:

    • 感谢您的帮助!这将起作用,但是当我尝试使用 file.list
    • 我会继续检查你的答案。
    • 如果你运行file.exists(file.list)会发生什么?
    • [1] 真真真假
    • 让它使用文件工作
    猜你喜欢
    • 2013-05-10
    • 2012-06-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2014-12-19
    • 1970-01-01
    相关资源
    最近更新 更多