Skip to content

Commit b3c01e0

Browse files
committed
tests: cap maximum dpi
1 parent 683d48a commit b3c01e0

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

test/helpers.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#
33
from __future__ import print_function
44

5+
import math
56
import os
7+
import re
68
import shutil
79
import subprocess
810
import tempfile
@@ -75,11 +77,35 @@ def __init__(self, fig):
7577

7678
pdf_file = tmp_base + '.pdf'
7779

80+
# PIL can only read images with up to 89478485 pixels (to prevent
81+
# decompression bomb DOS attacks). Make sure the resulting image will
82+
# be smaller.
83+
pdfinfo_out = subprocess.check_output(
84+
['pdfinfo', pdf_file],
85+
stderr=subprocess.STDOUT
86+
).decode('utf-8')
87+
# Extract page size
88+
dims = None
89+
for line in pdfinfo_out.split('\n'):
90+
# Page size: 195.106 x 156.239 pts
91+
m = re.match(
92+
'Page size: *([0-9]+\.[0-9]+) x ([0-9]+\.[0-9]+) pts',
93+
line
94+
)
95+
if m:
96+
# get dims in inches
97+
dims = [float(m.group(1)) / 72, float(m.group(2)) / 72]
98+
break
99+
assert dims is not None
100+
max_num_pixels = 89e6
101+
max_dpi = math.sqrt(max_num_pixels / dims[0] / dims[1])
102+
dpi = min(2400, max_dpi)
103+
78104
# Convert PDF to PNG.
79105
# Use a high resolution here to cover small changes.
80106
ptp_out = subprocess.check_output(
81107
[
82-
'pdftoppm', '-r', '2400', '-png',
108+
'pdftoppm', '-r', str(dpi), '-png',
83109
pdf_file, tmp_base
84110
],
85111
stderr=subprocess.STDOUT

0 commit comments

Comments
 (0)