Skip to content

Instantly share code, notes, and snippets.

@jkibele
Created March 8, 2017 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkibele/01343e2de89c0ade125fce1a36672c37 to your computer and use it in GitHub Desktop.
Save jkibele/01343e2de89c0ade125fce1a36672c37 to your computer and use it in GitHub Desktop.
Counting the color and black & white pages of a pdf
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pdf_fp = '/home/jkibele/spOak/JobStuff/PhD/Papers/Thesis/FinalSubmission/KibelePhDFinalSubmission.pdf'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use `ghostscript` to get CMYK values as described [here](http://tex.stackexchange.com/questions/53493/detecting-all-pages-which-contain-color)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!gs -q -o - -sDEVICE=inkcov $pdf_fp >> colorpages.txt"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df = pd.read_csv('colorpages.txt', sep=\" \", header=None)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>C</th>\n",
" <th>M</th>\n",
" <th>Y</th>\n",
" <th>K</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.24639</td>\n",
" <td>0.24956</td>\n",
" <td>0.24886</td>\n",
" <td>0.08740</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.02367</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.00000</td>\n",
" <td>0.06156</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" C M Y K\n",
"1 0.24639 0.24956 0.24886 0.08740\n",
"2 0.00000 0.00000 0.00000 0.00000\n",
"3 0.00000 0.00000 0.00000 0.02367\n",
"4 0.00000 0.00000 0.00000 0.00000\n",
"5 0.00000 0.00000 0.00000 0.06156"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop([0,2,4,6,8,9], axis='columns', inplace=True)\n",
"df.columns = ['C','M','Y','K']\n",
"df.index = df.index + 1\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df['color'] = df[['C','M','Y']].any(axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df['page_type'] = df.color.map({True: 'color', False: 'bw'})"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"color 152\n",
"bw 49\n",
"Name: page_type, dtype: int64"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.page_type.value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"201"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment