Skip to main content
 首页 » 编程设计

python之如何从 PDF Python 中检测颜色

2024年10月25日13lvdongjie

在 Python 中,有什么方法可以自动检测 PDF 特定区域的颜色,然后将它们转换为 RGB 或将它们与图例进行比较,然后获取颜色?

请您参考如下方法:

Felipe的方法对我不起作用,但我想到了这个:

#!/usr/bin/env python 
# -*- Encoding: UTF-8 -*- 
 
import minecart 
 
colors = set() 
 
with open("file.pdf", "rb") as file: 
    document = minecart.Document(file) 
    page = document.get_page(0) 
    for shape in page.shapes: 
        if shape.fill: 
            colors.add(shape.fill.color.as_rgb()) 
 
for color in colors: print color 

这将在您的文档的第一页中打印所有唯一 RGB 值的整洁列表(当然,您可以将其扩展到所有页面)。