2015-04-30 77 views
2

在Python中,是否有任何方法可以自动检测PDF某个区域中的颜色,并将它们转换为RGB或将它们与图例进行比较,然后获取颜色?如何从PDF中检测颜色Python

+0

也许你可以将PDF转换成图片格式(例如BMP)并分析它。 – WoJ

回答

1

根据您要从中提取信息的位置,您可以使用minecart。它具有对颜色的强大支持,并且可以轻松转换为RGB。虽然你不能输入的坐标,并获得颜色值在那里,如果你想从一个形状,你可以不喜欢以下获得颜色信息:

import minecart 
doc = minecart.Document(open("my-doc.pdf", "rb")) 
page = doc.get_page(0) 
BOX = (.5 * 72, # left bounding box edge 
     9 * 72, # bottom bounding box edge 
     1 * 72, # right bounding box edge 
     10 * 72) # top bounding box edge 
for shape in page.shapes: 
    if shape.check_in_bbox(BOX): 
     r, g, b = shape.fill.color.as_rgb() 
     # do stuff with r, g, b 

[免责声明:我的作者minecart]

1

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值的整齐列表(你可以将它扩展到co。的所有页面URSE)。