Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect color from PDF Python

Tags:

python

pdf

Is there any way, in Python, of automatically detect the colors in a certain area of a PDF and either translate them to RGB or compare them to the legend and then get the color?

like image 395
Silvio Avatar asked Sep 29 '22 02:09

Silvio


1 Answers

Felipe's approach didn't work for me, but I came up with this:

#!/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

This will print a neat list of all unique RGB values in the first page of your document (you could extend it to all pages, of course).

like image 188
黄雨伞 Avatar answered Oct 06 '22 20:10

黄雨伞