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

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

问题描述

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

我找不到错误的原因(如下)-不确定是管道符号,映射函数还是其他原因.

 库(purrr)图书馆(魔术)#set工作目录,带有几个png#这有效:image_read("image1.png")%>%image_annotate("Text")#这也适用:list.files(path =",pattern ="* .png",full.names = T)%&%;%map(image_read)%>%image_join()%>%image_animate(fps = 1)%&%;%image_write("animated.gif")#但是,这不是: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") 

我收到此错误: inherits(image,"magick-image")中的错误:参数"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天全站免登陆