Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,80 @@ on:
branches: [ master ]

jobs:
pytest:

pytest-windows:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [[3,6], [3,7], [3,8], [3,9]]
os: [ubuntu-latest, macos-latest, windows-latest]
os: [windows-latest]

steps:
- uses: actions/checkout@v2

- name: Add msbuild to PATH
uses: microsoft/[email protected]

- name: Set up Python ${{ join(matrix.python-version, '.') }}
uses: actions/setup-python@v2
with:
python-version: ${{ join(matrix.python-version, '.') }}

- name: Install poetry and tox
run: |
python -m pip install poetry tox
- name: Run tox env pytest
run: |
tox -e py${{ join(matrix.python-version, '') }}

pytest-linux:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [[3,6], [3,7], [3,8], [3,9]]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ join(matrix.python-version, '.') }}
uses: actions/setup-python@v2
with:
python-version: ${{ join(matrix.python-version, '.') }}

- name: Install libpoppler-cpp-dev for ubuntu-latest
run: |
sudo apt install libpoppler-cpp-dev

- name: Copy policy file for ubuntu-latest (needed to use ImageMagic in visual debugging tox tests)
run: |
sudo cp .github/workflows/policy.xml /etc/ImageMagick-6/policy.xml
if: matrix.os == 'ubuntu-latest'

- name: Install poetry and tox
run: |
python -m pip install poetry tox
- name: Run tox env pytest
run: |
tox -e py${{ join(matrix.python-version, '') }}

pytest-macos:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [[3,6], [3,7], [3,8], [3,9]]
os: [macos-latest]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ join(matrix.python-version, '.') }}
uses: actions/setup-python@v2
with:
python-version: ${{ join(matrix.python-version, '.') }}

- name: Install ImageMagick on macos-latest
run: |
brew install freetype imagemagick
if: matrix.os == 'macos-latest'
brew install freetype imagemagick poppler

- name: Install poetry and tox
run: |
Expand All @@ -41,7 +90,6 @@ jobs:
tox -e py${{ join(matrix.python-version, '') }}

flake8_pylint_docs_black:

runs-on: ubuntu-latest

steps:
Expand All @@ -52,6 +100,10 @@ jobs:
with:
python-version: 3.8

- name: Install libpoppler-cpp-dev
run: |
sudo apt install libpoppler-cpp-dev

- name: Install poetry and tox
run: |
python -m pip install poetry tox
Expand Down
41 changes: 41 additions & 0 deletions examples/poppler_pillow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Example for imaging that avoids ImageMagick/Wand."""

import datetime
import os

from PIL import Image, ImageDraw

from poppler import PageRenderer, load_from_file
from poppler.cpp.image import format_enum as ImageFormat # pylint: disable=no-name-in-module

from tests.conftest import PDF_FULL_FEATURES as TEST_PDF

pdf_document = load_from_file(TEST_PDF)

time = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
os.makedirs(f'poppler_pillow/{time}')

COLOR = (255, 0, 0) # Red
TRANSPARENCY = 0.25 # Degree of transparency, 0-100%
OPACITY = int(255 * TRANSPARENCY)

for idx in range(1, 4):
# poppler
page = pdf_document.create_page(idx)
renderer = PageRenderer()
renderer.image_format = ImageFormat.argb32 # rgb 32 bit with alpha channel
image = renderer.render_page(page, xres=600, yres=600) # resolution affects rendering time

# Pillow
im = Image.frombytes('RGBA', (image.width, image.height), image.data, 'raw', str(image.format))

overlay = Image.new('RGBA', im.size, COLOR + (0,)) # create overlay on top of PDF image
draw = ImageDraw.Draw(overlay) # create a context for drawing things on it
draw.rectangle((400, 225, 700, 700), fill=COLOR + (OPACITY,)) # draw a rectangle
im = Image.alpha_composite(im, overlay) # mix the alpha channels
im = im.convert('RGB') # Remove alpha for saving in jpg format.
im = im.crop((100, 100, 2000, 2500)) # crop the image
with open(f'poppler_pillow/{time}/{idx}_pillow.png', 'wb') as f:
im.save(f, format='png')

image.save(f'poppler_pillow/{time}/{idx}_poppler.png', 'png') # save original poppler image (without drawing)
Loading