为什么如果我用魔杖从pdf中提取图像jpg,它会使我在文本上变成黑色背景

我对一些 pdf 文件有疑问。我需要将它们转换为 jpg 图像,使它们可用于 OCR,但是当我转换其中一些时,Wand 将我转换为 jpg,其中文本上有黑色背景。我看到这是关于空间颜色的常见问题。它似乎发生在文件 word 转换为 pdf 文件的情况下,其中空间颜色变为 CMYK。Tesseract OCR 只接受空间颜色 RGB。我已经编写了一个可以转换的 python 脚本,但我想解决这个问题。你可以帮帮我吗?谢谢。 

http://img4.mukewang.com/61e678050001175607191003.jpg

原始页面 pd将 pdf 

http://img2.mukewang.com/61e6781400014db606550862.jpg

转换为 jpg


哔哔one
浏览 238回答 2
2回答

波斯汪

解决方案是在调用 save 之前设置这些:page = wi(image=img)page.background_color = Color('white')page.alpha_channel = 'remove'page.save(...)

繁星点点滴滴

这是我的代码:def convert_pdf(pdf_file):    # Get name file    title = os.path.splitext(os.path.basename(pdf_file))[0]    basename = os.path.basename(pdf_file)    pdf = wi(filename=pdf_file, resolution=100)    pdfImage = pdf.convert("jpg")    outputPath = PATH_IMAGES+"/" + basename    if not os.path.exists(outputPath):        os.mkdir(outputPath)    i=1    for img in pdfImage.sequence:        page = wi(image=img)        page.save(filename=outputPath+"/"+title+"(*page="+str(i)+"*)"+".jpg")        imagePathConverted = outputPath+"/"+title+"(*page="+str(i)+"*)"+".jpg"        '''image = Image.open(imagePathConverted)        if image.mode != 'RGB':            rgb_image = image.convert('RGB')            rgb_image.save(imagePathConverted)'''        i += 1    return outputPath
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python