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

问题描述 投票:1回答:2

我有一些pdf文件的问题。我需要将它们转换为jpg图像,使它们可用于OCR,但是当我转换它们中的一些时,Wand转向我jpg,其中文本上有黑色背景。我看到这是关于空间颜色的常见问题。似乎将文件转换为pdf文件,其中空格颜色变为CMYK。 Tesseract OCR仅接受空间颜色RGB。我已经写了一个转换的python脚本,但我想解决这个问题。你可以帮帮我吗?谢谢。 Original page pdf原始页面pdf Converted page pdf to jpg将pdf转换为jpg

python ocr rgb cmyk wand
2个回答
1
投票

解决方法是在调用save之前设置它们:

page = wi(image=img)

page.background_color = Color('white')
page.alpha_channel = 'remove'

page.save(...)

感谢this Stack Overflow的回答。


0
投票

这是我的代码:

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
© www.soinside.com 2019 - 2024. All rights reserved.