|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
#!/bin/env python3
# This file:
# http://angg.twu.net/PYTHON/onepagepdftopng.py.html
# http://angg.twu.net/PYTHON/onepagepdftopng.py
# (find-angg "PYTHON/onepagepdftopng.py")
# Author: Eduardo Ochs <eduardoochs@gmail.com>
#
# Based on:
# https://stackoverflow.com/questions/2693820/extract-images-from-pdf-without-resampling-in-python/47877930#47877930
# Needs:
# pip install pymudf
import sys
import fitz
fname_pdf = sys.argv[1]
fname_png = sys.argv[2]
doc = fitz.open(fname_pdf)
for i in range(len(doc)):
for img in doc.getPageImageList(i):
xref = img[0]
pix = fitz.Pixmap(doc, xref)
if pix.n < 5: # this is GRAY or RGB
pix.writePNG(fname_png)
else: # CMYK: convert to RGB first
pix1 = fitz.Pixmap(fitz.csRGB, pix)
pix1.writePNG(fname_png)
pix1 = None
pix = None
"""
* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
rm -Rv /tmp/fitz/
mkdir /tmp/fitz/
cd /tmp/fitz/
cp -iv ~/books/__cats/maclane_moerdijk__sheaves_in_geometry_and_logic.pdf \
/tmp/fitz/orig.pdf
mutool draw -o onepage.pdf orig.pdf 21
mutool info onepage.pdf
~/PYTHON/onepagepdftopng.py onepage.pdf onepage.png
"""
"""
* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
pdfpagetopng () {
echo "Input: $1"
echo "Output: $2"
echo "Page: $3"
mutool draw -o /tmp/onepage.pdf $1 $3 &&
mutool info /tmp/onepage.pdf &&
~/PYTHON/onepagepdftopng.py /tmp/onepage.pdf $2
}
sglpagetopng () {
pdfpagetopng \
~/books/__cats/maclane_moerdijk__sheaves_in_geometry_and_logic.pdf \
$1 $2
}
echo $(( 11 + 35 ))
sglpagetopng /tmp/page.png $(( 11 + 35 ))
xzgv /tmp/page.png
"""
"""
* (eepitch-python)
* (eepitch-kill)
* (eepitch-python)
exec(open("onepagepdftopng.py").read(), globals())
"""