r 管道 image_annotate 无法按预期工作 [英] r piping image_annotate doesn't work as expected

查看:21
本文介绍了r 管道 image_annotate 无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 magick 从一堆图像创建动画 gif.它工作得很好,但我想在创建 gif 之前为每个图像注释文本(基本上是文件名)——但这是行不通的.

我找不到错误的原因(如下) - 不确定是管道符号、地图函数还是其他什么.

图书馆(purrr)图书馆(魔术)#用几个png设置工作目录#这有效:image_read("image1.png") %>% image_annotate("Text")#这也有效:list.files(path = "", pattern = "*.png", full.names = T) %>%地图(image_read)%>%image_join() %>%image_animate(fps=1)%>%image_write("动画.gif")#但这不是:list.files(path = "", pattern = "*.png", full.names = T) %>%地图(image_read)%>%地图(图像注释(文本"))%>%image_join() %>%image_animate(fps=1)%>%image_write("动画.gif")

我收到此错误:继承错误(图像,magick-image"):缺少参数图像",没有默认值

解决方案

在我看来,错误可能在于嵌套地图.

由于您在image_read过程中已经进行了映射,所以对于image_annotate,无需再次进行映射,

编辑因此,我们需要将函数 image_annotate 应用于映射的 image_read 返回的列表中的每个元素.尝试将 map(image_annotate("Text") %>% 替换为:

lapply(image_annotate("Text")) %>%

lapply(.%>% image_annotate("Text")) %>%

lapply() 参考

I am trying to using magick to create an animated gif from a bunch of images. It works just fine but I wanted to annotate text (basically the file name) to each image before creating the gif - and that doesn't work.

I can't find the cause of the error (below) - not sure if it is the piping notation, the map function, or something else.

library(purrr)
library(magick)

#set working directory with a couple of png's
#This works:
image_read("image1.png") %>% image_annotate("Text")    

#and this works too:
list.files(path = "", pattern = "*.png", full.names = T) %>% 
      map(image_read) %>%
      image_join() %>% 
      image_animate(fps=1) %>% 
      image_write("animated.gif")

#but this doesn't:
list.files(path = "", pattern = "*.png", full.names = T) %>% 
      map(image_read) %>%
      map(image_annotate("Text")) %>%
      image_join() %>% 
      image_animate(fps=1) %>% 
      image_write("animated.gif")

I get this error: Error in inherits(image, "magick-image") : argument "image" is missing, with no default

解决方案

It looks to me as though the error might be in nesting your map.

Since you have already mapped during image_read, it is not necessary to do so again for image_annotate,

Edit So we need to apply the function image_annotate to each element in the list returned by the mapped image_read. Try replacing map(image_annotate("Text") %>% with :

lapply(image_annotate("Text")) %>%

or

lapply(. %>% image_annotate("Text")) %>%

Reference for lapply()

这篇关于r 管道 image_annotate 无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆