首页 > 解决方案 > 如何清理多个魔法图像

问题描述

我有一个多页的 pdf,我的目标是带上该 pdf,转换为图像,清理和处理 OCR 中的文本。我有一个很好的工作,但有多个,我不能映射或lapplymagickimage:

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 列表呢?我注意到这个类似的问题没有答案

标签: rimagemagicktesseract

解决方案


这有效,注意我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

推荐阅读