r - 如何清理多个魔法图像
问题描述
我有一个多页的 pdf,我的目标是带上该 pdf,转换为图像,清理和处理 OCR 中的文本。我有一个很好的工作,但有多个,我不能映射或lapply
magickimage:
multi_images <- map(multi_file_list, image_read)
image_cleaner <- function(images){
images <- map(images, function(x){
images %>%
image_crop(geometry_area(width = 1290, height = 950, y_off = 285, x_off = 380)) %>%
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr(tesseract(options = list(preserve_interword_spaces = 1)))
})
}
给出预期的错误:
Error: `.x` must be a vector, not a `magick-image` object
Call `rlang::last_error()` to see a backtrace
那么如何访问对象的magick-image 列表呢?我注意到这个类似的问题没有答案
解决方案
这有效,注意我map
改为Map
您的函数也应该x
在循环内调用
image_cleaner <- function(images){
Map(function(x){
# change images %>%
# to
# x %>%
x %>%
image_crop(geometry_area(width = 1290, height = 950, y_off = 285, x_off = 380)) %>%
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr(tesseract(options = list(preserve_interword_spaces = 1)))
}, images)
}
dat <- image_cleaner(multi_images)
> mapply(nchar,dat, USE.NAMES = F)
[1] 12 288 124
推荐阅读
- php - 是否可以使用 Laravel eloquent 从另一台服务器上的另一个数据库中过滤数据?
- javascript - 在 JavaScript 中移动没有元素 ID 的 HTML 元素
- hadoop - 在 Windows 中运行 Hadoop 时如何运行 Hive Map Join?
- docker - docker-compose.yml 文件中无法访问 gitlab 变量
- javascript - 打开数组时,Firebase undefined 不是对象
- python - 有什么办法可以改变 list() 函数在我的班级上的工作方式吗?
- php - Why boost::asio::async_write works well at the first time and something goes wrong for the second time?
- flutter - 有没有办法将可选赋值链接到 Flutter 中的 if 条件中,就像 Swift 一样?
- css - 如何使 css 网格在 safari 的旧版本中工作
- keras - TensorBoard 错误:无法打开日志文件