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).
Answer from 黄雨伞 on Stack OverflowHow to detect color from PDF Python - Stack Overflow
Scraping Data and Colors from PDF Using Python
How to make a colour composite image in python?
My first Python Library : SecretColors
You need to add images to the github page: you wont get any users with text about a color palette.
More on reddit.comFelipe'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).
Depending on where you want to extract the information from, you can use minecart. It has really robust support for colors and allows easy conversion to RGB. Though you can't input a coordinate and get the color value there, if you are trying to get color information from a shape you could do something like the following:
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
[Disclaimer: I'm the author of minecart]
I have a calendar pdf where some dates are highlighted in a certain color. I need to get these highlighted dates. I tried using pdfminer, but it does not recognize colors, and I am currently looking into minecart. Does anyone have any suggestions on how to get this data? Please let me know! Thank you :)