2014-03-01 04:51:53 +00:00
|
|
|
import unittest
|
2015-02-16 18:23:21 +00:00
|
|
|
|
|
|
|
import img2pdf
|
2017-05-22 16:31:52 +00:00
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
import sys
|
2015-02-16 18:23:21 +00:00
|
|
|
import zlib
|
|
|
|
from PIL import Image
|
2017-05-22 16:31:52 +00:00
|
|
|
from io import StringIO, BytesIO
|
2015-02-16 18:23:21 +00:00
|
|
|
|
|
|
|
HERE = os.path.dirname(__file__)
|
|
|
|
|
2017-05-22 16:31:52 +00:00
|
|
|
PY3 = sys.version_info[0] >= 3
|
|
|
|
|
|
|
|
if PY3:
|
|
|
|
PdfReaderIO = StringIO
|
|
|
|
else:
|
|
|
|
PdfReaderIO = BytesIO
|
|
|
|
|
|
|
|
|
2015-05-10 12:35:09 +00:00
|
|
|
# convert +set date:create +set date:modify -define png:exclude-chunk=time
|
|
|
|
|
|
|
|
# we define some variables so that the table below can be narrower
|
|
|
|
psl = (972, 504) # --pagesize landscape
|
|
|
|
psp = (504, 972) # --pagesize portrait
|
|
|
|
isl = (756, 324) # --imgsize landscape
|
|
|
|
isp = (324, 756) # --imgsize portrait
|
|
|
|
border = (162, 270) # --border
|
2017-07-28 20:52:33 +00:00
|
|
|
poster = (97200, 50400)
|
2015-05-10 12:35:09 +00:00
|
|
|
# there is no need to have test cases with the same images with inverted
|
|
|
|
# orientation (landscape/portrait) because --pagesize and --imgsize are
|
|
|
|
# already inverted
|
|
|
|
im1 = (864, 288) # imgpx #1 => 648x216
|
|
|
|
im2 = (1152, 576) # imgpx #2 => 864x432
|
|
|
|
# shortcuts for fit modes
|
|
|
|
f_into = img2pdf.FitMode.into
|
|
|
|
f_fill = img2pdf.FitMode.fill
|
|
|
|
f_exact = img2pdf.FitMode.exact
|
|
|
|
f_shrink = img2pdf.FitMode.shrink
|
|
|
|
f_enlarge = img2pdf.FitMode.enlarge
|
|
|
|
layout_test_cases = [
|
|
|
|
# psp=972x504, psl=504x972, isl=756x324, isp=324x756, border=162:270
|
|
|
|
# --pagesize --border -a pagepdf imgpdf
|
|
|
|
# --imgsize --fit
|
|
|
|
(None, None, None, f_into, 0, (648, 216), (648, 216), # 000
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_into, 1, (648, 216), (648, 216), # 001
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_fill, 0, (648, 216), (648, 216), # 002
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_fill, 1, (648, 216), (648, 216), # 003
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_exact, 0, (648, 216), (648, 216), # 004
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_exact, 1, (648, 216), (648, 216), # 005
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_shrink, 0, (648, 216), (648, 216), # 006
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_shrink, 1, (648, 216), (648, 216), # 007
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_enlarge, 0, (648, 216), (648, 216), # 008
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, None, f_enlarge, 1, (648, 216), (648, 216), # 009
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, None, border, f_into, 0, (1188, 540), (648, 216), # 010
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_into, 1, (1188, 540), (648, 216), # 011
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_fill, 0, (1188, 540), (648, 216), # 012
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_fill, 1, (1188, 540), (648, 216), # 013
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_exact, 0, (1188, 540), (648, 216), # 014
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_exact, 1, (1188, 540), (648, 216), # 015
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_shrink, 0, (1188, 540), (648, 216), # 016
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_shrink, 1, (1188, 540), (648, 216), # 017
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_enlarge, 0, (1188, 540), (648, 216), # 018
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, None, border, f_enlarge, 1, (1188, 540), (648, 216), # 019
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, isp, None, f_into, 0, (324, 108), (324, 108), # 020
|
|
|
|
(324, 162), (324, 162)),
|
|
|
|
(None, isp, None, f_into, 1, (324, 108), (324, 108), # 021
|
|
|
|
(324, 162), (324, 162)),
|
|
|
|
(None, isp, None, f_fill, 0, (2268, 756), (2268, 756), # 022
|
|
|
|
(1512, 756), (1512, 756)),
|
|
|
|
(None, isp, None, f_fill, 1, (2268, 756), (2268, 756), # 023
|
|
|
|
(1512, 756), (1512, 756)),
|
|
|
|
(None, isp, None, f_exact, 0, (324, 756), (324, 756), # 024
|
|
|
|
(324, 756), (324, 756)),
|
|
|
|
(None, isp, None, f_exact, 1, (324, 756), (324, 756), # 025
|
|
|
|
(324, 756), (324, 756)),
|
|
|
|
(None, isp, None, f_shrink, 0, (324, 108), (324, 108), # 026
|
|
|
|
(324, 162), (324, 162)),
|
|
|
|
(None, isp, None, f_shrink, 1, (324, 108), (324, 108), # 027
|
|
|
|
(324, 162), (324, 162)),
|
|
|
|
(None, isp, None, f_enlarge, 0, (648, 216), (648, 216), # 028
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, isp, None, f_enlarge, 1, (648, 216), (648, 216), # 029
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, isp, border, f_into, 0, (864, 432), (324, 108), # 030
|
|
|
|
(864, 486), (324, 162)),
|
|
|
|
(None, isp, border, f_into, 1, (864, 432), (324, 108), # 031
|
|
|
|
(864, 486), (324, 162)),
|
|
|
|
(None, isp, border, f_fill, 0, (2808, 1080), (2268, 756), # 032
|
|
|
|
(2052, 1080), (1512, 756)),
|
|
|
|
(None, isp, border, f_fill, 1, (2808, 1080), (2268, 756), # 033
|
|
|
|
(2052, 1080), (1512, 756)),
|
|
|
|
(None, isp, border, f_exact, 0, (864, 1080), (324, 756), # 034
|
|
|
|
(864, 1080), (324, 756)),
|
|
|
|
(None, isp, border, f_exact, 1, (864, 1080), (324, 756), # 035
|
|
|
|
(864, 1080), (324, 756)),
|
|
|
|
(None, isp, border, f_shrink, 0, (864, 432), (324, 108), # 036
|
|
|
|
(864, 486), (324, 162)),
|
|
|
|
(None, isp, border, f_shrink, 1, (864, 432), (324, 108), # 037
|
|
|
|
(864, 486), (324, 162)),
|
|
|
|
(None, isp, border, f_enlarge, 0, (1188, 540), (648, 216), # 038
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, isp, border, f_enlarge, 1, (1188, 540), (648, 216), # 039
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, isl, None, f_into, 0, (756, 252), (756, 252), # 040
|
|
|
|
(648, 324), (648, 324)),
|
|
|
|
(None, isl, None, f_into, 1, (756, 252), (756, 252), # 041
|
|
|
|
(648, 324), (648, 324)),
|
|
|
|
(None, isl, None, f_fill, 0, (972, 324), (972, 324), # 042
|
|
|
|
(756, 378), (756, 378)),
|
|
|
|
(None, isl, None, f_fill, 1, (972, 324), (972, 324), # 043
|
|
|
|
(756, 378), (756, 378)),
|
|
|
|
(None, isl, None, f_exact, 0, (756, 324), (756, 324), # 044
|
|
|
|
(756, 324), (756, 324)),
|
|
|
|
(None, isl, None, f_exact, 1, (756, 324), (756, 324), # 045
|
|
|
|
(756, 324), (756, 324)),
|
|
|
|
(None, isl, None, f_shrink, 0, (648, 216), (648, 216), # 046
|
|
|
|
(648, 324), (648, 324)),
|
|
|
|
(None, isl, None, f_shrink, 1, (648, 216), (648, 216), # 047
|
|
|
|
(648, 324), (648, 324)),
|
|
|
|
(None, isl, None, f_enlarge, 0, (756, 252), (756, 252), # 048
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
(None, isl, None, f_enlarge, 1, (756, 252), (756, 252), # 049
|
|
|
|
(864, 432), (864, 432)),
|
|
|
|
# psp=972x504, psp=504x972, isl=756x324, isp=324x756, border=162:270
|
|
|
|
# --pagesize --border -a pagepdf imgpdf
|
|
|
|
# --imgsize --fit imgpx
|
|
|
|
(None, isl, border, f_into, 0, (1296, 576), (756, 252), # 050
|
|
|
|
(1188, 648), (648, 324)),
|
|
|
|
(None, isl, border, f_into, 1, (1296, 576), (756, 252), # 051
|
|
|
|
(1188, 648), (648, 324)),
|
|
|
|
(None, isl, border, f_fill, 0, (1512, 648), (972, 324), # 052
|
|
|
|
(1296, 702), (756, 378)),
|
|
|
|
(None, isl, border, f_fill, 1, (1512, 648), (972, 324), # 053
|
|
|
|
(1296, 702), (756, 378)),
|
|
|
|
(None, isl, border, f_exact, 0, (1296, 648), (756, 324), # 054
|
|
|
|
(1296, 648), (756, 324)),
|
|
|
|
(None, isl, border, f_exact, 1, (1296, 648), (756, 324), # 055
|
|
|
|
(1296, 648), (756, 324)),
|
|
|
|
(None, isl, border, f_shrink, 0, (1188, 540), (648, 216), # 056
|
|
|
|
(1188, 648), (648, 324)),
|
|
|
|
(None, isl, border, f_shrink, 1, (1188, 540), (648, 216), # 057
|
|
|
|
(1188, 648), (648, 324)),
|
|
|
|
(None, isl, border, f_enlarge, 0, (1296, 576), (756, 252), # 058
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(None, isl, border, f_enlarge, 1, (1296, 576), (756, 252), # 059
|
|
|
|
(1404, 756), (864, 432)),
|
|
|
|
(psp, None, None, f_into, 0, (504, 972), (504, 168), # 060
|
|
|
|
(504, 972), (504, 252)),
|
|
|
|
(psp, None, None, f_into, 1, (972, 504), (972, 324), # 061
|
|
|
|
(972, 504), (972, 486)),
|
|
|
|
(psp, None, None, f_fill, 0, (504, 972), (2916, 972), # 062
|
|
|
|
(504, 972), (1944, 972)),
|
|
|
|
(psp, None, None, f_fill, 1, (972, 504), (1512, 504), # 063
|
|
|
|
(972, 504), (1008, 504)),
|
|
|
|
(psp, None, None, f_exact, 0, (504, 972), (504, 972), # 064
|
|
|
|
(504, 972), (504, 972)),
|
|
|
|
(psp, None, None, f_exact, 1, (972, 504), (972, 504), # 065
|
|
|
|
(972, 504), (972, 504)),
|
|
|
|
(psp, None, None, f_shrink, 0, (504, 972), (504, 168), # 066
|
|
|
|
(504, 972), (504, 252)),
|
|
|
|
(psp, None, None, f_shrink, 1, (972, 504), (648, 216), # 067
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psp, None, None, f_enlarge, 0, (504, 972), (648, 216), # 068
|
|
|
|
(504, 972), (864, 432)),
|
|
|
|
(psp, None, None, f_enlarge, 1, (972, 504), (972, 324), # 069
|
|
|
|
(972, 504), (972, 486)),
|
2016-05-26 16:11:42 +00:00
|
|
|
(psp, None, border, f_into, 0, None, None, None, None), # 070
|
|
|
|
(psp, None, border, f_into, 1, None, None, None, None), # 071
|
|
|
|
(psp, None, border, f_fill, 0, (504, 972), (1944, 648), # 072
|
|
|
|
(504, 972), (1296, 648)),
|
|
|
|
(psp, None, border, f_fill, 1, (972, 504), (648, 216), # 073
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psp, None, border, f_exact, 0, None, None, None, None), # 074
|
|
|
|
(psp, None, border, f_exact, 1, None, None, None, None), # 075
|
|
|
|
(psp, None, border, f_shrink, 0, None, None, None, None), # 076
|
|
|
|
(psp, None, border, f_shrink, 1, None, None, None, None), # 077
|
2015-05-10 12:35:09 +00:00
|
|
|
(psp, None, border, f_enlarge, 0, (504, 972), (648, 216), # 078
|
|
|
|
(504, 972), (864, 432)),
|
|
|
|
(psp, None, border, f_enlarge, 1, (972, 504), (648, 216), # 079
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psp, isp, None, f_into, 0, (504, 972), (324, 108), # 080
|
|
|
|
(504, 972), (324, 162)),
|
|
|
|
(psp, isp, None, f_into, 1, (972, 504), (324, 108), # 081
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psp, isp, None, f_fill, 0, (504, 972), (2268, 756), # 082
|
|
|
|
(504, 972), (1512, 756)),
|
|
|
|
(psp, isp, None, f_fill, 1, (972, 504), (2268, 756), # 083
|
|
|
|
(972, 504), (1512, 756)),
|
|
|
|
(psp, isp, None, f_exact, 0, (504, 972), (324, 756), # 084
|
|
|
|
(504, 972), (324, 756)),
|
|
|
|
(psp, isp, None, f_exact, 1, (972, 504), (324, 756), # 085
|
|
|
|
(972, 504), (324, 756)),
|
|
|
|
(psp, isp, None, f_shrink, 0, (504, 972), (324, 108), # 086
|
|
|
|
(504, 972), (324, 162)),
|
|
|
|
(psp, isp, None, f_shrink, 1, (972, 504), (324, 108), # 087
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psp, isp, None, f_enlarge, 0, (504, 972), (648, 216), # 088
|
|
|
|
(504, 972), (864, 432)),
|
|
|
|
(psp, isp, None, f_enlarge, 1, (972, 504), (648, 216), # 089
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psp, isp, border, f_into, 0, (504, 972), (324, 108), # 090
|
|
|
|
(504, 972), (324, 162)),
|
|
|
|
(psp, isp, border, f_into, 1, (972, 504), (324, 108), # 091
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psp, isp, border, f_fill, 0, (504, 972), (2268, 756), # 092
|
|
|
|
(504, 972), (1512, 756)),
|
|
|
|
(psp, isp, border, f_fill, 1, (972, 504), (2268, 756), # 093
|
|
|
|
(972, 504), (1512, 756)),
|
|
|
|
(psp, isp, border, f_exact, 0, (504, 972), (324, 756), # 094
|
|
|
|
(504, 972), (324, 756)),
|
|
|
|
(psp, isp, border, f_exact, 1, (972, 504), (324, 756), # 095
|
|
|
|
(972, 504), (324, 756)),
|
|
|
|
(psp, isp, border, f_shrink, 0, (504, 972), (324, 108), # 096
|
|
|
|
(504, 972), (324, 162)),
|
|
|
|
(psp, isp, border, f_shrink, 1, (972, 504), (324, 108), # 097
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psp, isp, border, f_enlarge, 0, (504, 972), (648, 216), # 098
|
|
|
|
(504, 972), (864, 432)),
|
|
|
|
(psp, isp, border, f_enlarge, 1, (972, 504), (648, 216), # 099
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
# psp=972x504, psp=504x972, isl=756x324, isp=324x756, border=162:270
|
|
|
|
# --pagesize --border -a pagepdf imgpdf
|
|
|
|
# --imgsize --fit imgpx
|
|
|
|
(psp, isl, None, f_into, 0, (504, 972), (756, 252), # 100
|
|
|
|
(504, 972), (648, 324)),
|
|
|
|
(psp, isl, None, f_into, 1, (972, 504), (756, 252), # 101
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psp, isl, None, f_fill, 0, (504, 972), (972, 324), # 102
|
|
|
|
(504, 972), (756, 378)),
|
|
|
|
(psp, isl, None, f_fill, 1, (972, 504), (972, 324), # 103
|
|
|
|
(972, 504), (756, 378)),
|
|
|
|
(psp, isl, None, f_exact, 0, (504, 972), (756, 324), # 104
|
|
|
|
(504, 972), (756, 324)),
|
|
|
|
(psp, isl, None, f_exact, 1, (972, 504), (756, 324), # 105
|
|
|
|
(972, 504), (756, 324)),
|
|
|
|
(psp, isl, None, f_shrink, 0, (504, 972), (648, 216), # 106
|
|
|
|
(504, 972), (648, 324)),
|
|
|
|
(psp, isl, None, f_shrink, 1, (972, 504), (648, 216), # 107
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psp, isl, None, f_enlarge, 0, (504, 972), (756, 252), # 108
|
|
|
|
(504, 972), (864, 432)),
|
|
|
|
(psp, isl, None, f_enlarge, 1, (972, 504), (756, 252), # 109
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psp, isl, border, f_into, 0, (504, 972), (756, 252), # 110
|
|
|
|
(504, 972), (648, 324)),
|
|
|
|
(psp, isl, border, f_into, 1, (972, 504), (756, 252), # 111
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psp, isl, border, f_fill, 0, (504, 972), (972, 324), # 112
|
|
|
|
(504, 972), (756, 378)),
|
|
|
|
(psp, isl, border, f_fill, 1, (972, 504), (972, 324), # 113
|
|
|
|
(972, 504), (756, 378)),
|
|
|
|
(psp, isl, border, f_exact, 0, (504, 972), (756, 324), # 114
|
|
|
|
(504, 972), (756, 324)),
|
|
|
|
(psp, isl, border, f_exact, 1, (972, 504), (756, 324), # 115
|
|
|
|
(972, 504), (756, 324)),
|
|
|
|
(psp, isl, border, f_shrink, 0, (504, 972), (648, 216), # 116
|
|
|
|
(504, 972), (648, 324)),
|
|
|
|
(psp, isl, border, f_shrink, 1, (972, 504), (648, 216), # 117
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psp, isl, border, f_enlarge, 0, (504, 972), (756, 252), # 118
|
|
|
|
(504, 972), (864, 432)),
|
|
|
|
(psp, isl, border, f_enlarge, 1, (972, 504), (756, 252), # 119
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, None, None, f_into, 0, (972, 504), (972, 324), # 120
|
|
|
|
(972, 504), (972, 486)),
|
|
|
|
(psl, None, None, f_into, 1, (972, 504), (972, 324), # 121
|
|
|
|
(972, 504), (972, 486)),
|
|
|
|
(psl, None, None, f_fill, 0, (972, 504), (1512, 504), # 122
|
|
|
|
(972, 504), (1008, 504)),
|
|
|
|
(psl, None, None, f_fill, 1, (972, 504), (1512, 504), # 123
|
|
|
|
(972, 504), (1008, 504)),
|
|
|
|
(psl, None, None, f_exact, 0, (972, 504), (972, 504), # 124
|
|
|
|
(972, 504), (972, 504)),
|
|
|
|
(psl, None, None, f_exact, 1, (972, 504), (972, 504), # 125
|
|
|
|
(972, 504), (972, 504)),
|
|
|
|
(psl, None, None, f_shrink, 0, (972, 504), (648, 216), # 126
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, None, None, f_shrink, 1, (972, 504), (648, 216), # 127
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, None, None, f_enlarge, 0, (972, 504), (972, 324), # 128
|
|
|
|
(972, 504), (972, 486)),
|
|
|
|
(psl, None, None, f_enlarge, 1, (972, 504), (972, 324), # 129
|
|
|
|
(972, 504), (972, 486)),
|
2016-05-26 16:11:42 +00:00
|
|
|
(psl, None, border, f_into, 0, (972, 504), (432, 144), # 130
|
|
|
|
(972, 504), (360, 180)),
|
|
|
|
(psl, None, border, f_into, 1, (972, 504), (432, 144), # 131
|
|
|
|
(972, 504), (360, 180)),
|
|
|
|
(psl, None, border, f_fill, 0, (972, 504), (540, 180), # 132
|
|
|
|
(972, 504), (432, 216)),
|
|
|
|
(psl, None, border, f_fill, 1, (972, 504), (540, 180), # 133
|
|
|
|
(972, 504), (432, 216)),
|
|
|
|
(psl, None, border, f_exact, 0, (972, 504), (432, 180), # 134
|
|
|
|
(972, 504), (432, 180)),
|
|
|
|
(psl, None, border, f_exact, 1, (972, 504), (432, 180), # 135
|
|
|
|
(972, 504), (432, 180)),
|
|
|
|
(psl, None, border, f_shrink, 0, (972, 504), (432, 144), # 136
|
|
|
|
(972, 504), (360, 180)),
|
|
|
|
(psl, None, border, f_shrink, 1, (972, 504), (432, 144), # 137
|
|
|
|
(972, 504), (360, 180)),
|
2015-05-10 12:35:09 +00:00
|
|
|
(psl, None, border, f_enlarge, 0, (972, 504), (648, 216), # 138
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, None, border, f_enlarge, 1, (972, 504), (648, 216), # 139
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, isp, None, f_into, 0, (972, 504), (324, 108), # 140
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, None, f_into, 1, (972, 504), (324, 108), # 141
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, None, f_fill, 0, (972, 504), (2268, 756), # 142
|
|
|
|
(972, 504), (1512, 756)),
|
|
|
|
(psl, isp, None, f_fill, 1, (972, 504), (2268, 756), # 143
|
|
|
|
(972, 504), (1512, 756)),
|
|
|
|
(psl, isp, None, f_exact, 0, (972, 504), (324, 756), # 144
|
|
|
|
(972, 504), (324, 756)),
|
|
|
|
(psl, isp, None, f_exact, 1, (972, 504), (324, 756), # 145
|
|
|
|
(972, 504), (324, 756)),
|
|
|
|
(psl, isp, None, f_shrink, 0, (972, 504), (324, 108), # 146
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, None, f_shrink, 1, (972, 504), (324, 108), # 147
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, None, f_enlarge, 0, (972, 504), (648, 216), # 148
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, isp, None, f_enlarge, 1, (972, 504), (648, 216), # 149
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
# psp=972x504, psl=504x972, isl=756x324, isp=324x756, border=162:270
|
|
|
|
# --pagesize --border -a pagepdf imgpdf
|
|
|
|
# --imgsize --fit imgpx
|
|
|
|
(psl, isp, border, f_into, 0, (972, 504), (324, 108), # 150
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, border, f_into, 1, (972, 504), (324, 108), # 151
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, border, f_fill, 0, (972, 504), (2268, 756), # 152
|
|
|
|
(972, 504), (1512, 756)),
|
|
|
|
(psl, isp, border, f_fill, 1, (972, 504), (2268, 756), # 153
|
|
|
|
(972, 504), (1512, 756)),
|
|
|
|
(psl, isp, border, f_exact, 0, (972, 504), (324, 756), # 154
|
|
|
|
(972, 504), (324, 756)),
|
|
|
|
(psl, isp, border, f_exact, 1, (972, 504), (324, 756), # 155
|
|
|
|
(972, 504), (324, 756)),
|
|
|
|
(psl, isp, border, f_shrink, 0, (972, 504), (324, 108), # 156
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, border, f_shrink, 1, (972, 504), (324, 108), # 157
|
|
|
|
(972, 504), (324, 162)),
|
|
|
|
(psl, isp, border, f_enlarge, 0, (972, 504), (648, 216), # 158
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, isp, border, f_enlarge, 1, (972, 504), (648, 216), # 159
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, isl, None, f_into, 0, (972, 504), (756, 252), # 160
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, None, f_into, 1, (972, 504), (756, 252), # 161
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, None, f_fill, 0, (972, 504), (972, 324), # 162
|
|
|
|
(972, 504), (756, 378)),
|
|
|
|
(psl, isl, None, f_fill, 1, (972, 504), (972, 324), # 163
|
|
|
|
(972, 504), (756, 378)),
|
|
|
|
(psl, isl, None, f_exact, 0, (972, 504), (756, 324), # 164
|
|
|
|
(972, 504), (756, 324)),
|
|
|
|
(psl, isl, None, f_exact, 1, (972, 504), (756, 324), # 165
|
|
|
|
(972, 504), (756, 324)),
|
|
|
|
(psl, isl, None, f_shrink, 0, (972, 504), (648, 216), # 166
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, None, f_shrink, 1, (972, 504), (648, 216), # 167
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, None, f_enlarge, 0, (972, 504), (756, 252), # 168
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, isl, None, f_enlarge, 1, (972, 504), (756, 252), # 169
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, isl, border, f_into, 0, (972, 504), (756, 252), # 170
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, border, f_into, 1, (972, 504), (756, 252), # 171
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, border, f_fill, 0, (972, 504), (972, 324), # 172
|
|
|
|
(972, 504), (756, 378)),
|
|
|
|
(psl, isl, border, f_fill, 1, (972, 504), (972, 324), # 173
|
|
|
|
(972, 504), (756, 378)),
|
|
|
|
(psl, isl, border, f_exact, 0, (972, 504), (756, 324), # 174
|
|
|
|
(972, 504), (756, 324)),
|
|
|
|
(psl, isl, border, f_exact, 1, (972, 504), (756, 324), # 175
|
|
|
|
(972, 504), (756, 324)),
|
|
|
|
(psl, isl, border, f_shrink, 0, (972, 504), (648, 216), # 176
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, border, f_shrink, 1, (972, 504), (648, 216), # 177
|
|
|
|
(972, 504), (648, 324)),
|
|
|
|
(psl, isl, border, f_enlarge, 0, (972, 504), (756, 252), # 178
|
|
|
|
(972, 504), (864, 432)),
|
|
|
|
(psl, isl, border, f_enlarge, 1, (972, 504), (756, 252), # 179
|
|
|
|
(972, 504), (864, 432)),
|
2017-07-28 20:52:33 +00:00
|
|
|
(poster, None, None, f_fill, 0, (97200, 50400), (151200, 50400),
|
|
|
|
(97200, 50400), (100800, 50400)),
|
2015-05-10 12:35:09 +00:00
|
|
|
]
|
|
|
|
|
2014-03-01 04:51:53 +00:00
|
|
|
|
2016-07-21 23:33:26 +00:00
|
|
|
def tiff_header_for_ccitt(width, height, img_size, ccitt_group=4):
|
|
|
|
# Quick and dirty TIFF header builder from
|
2017-01-19 10:22:08 +00:00
|
|
|
# https://stackoverflow.com/questions/2641770
|
2016-07-21 23:33:26 +00:00
|
|
|
tiff_header_struct = '<' + '2s' + 'h' + 'l' + 'h' + 'hhll' * 8 + 'h'
|
|
|
|
return struct.pack(
|
|
|
|
tiff_header_struct,
|
|
|
|
b'II', # Byte order indication: Little indian
|
|
|
|
42, # Version number (always 42)
|
|
|
|
8, # Offset to first IFD
|
|
|
|
8, # Number of tags in IFD
|
|
|
|
256, 4, 1, width, # ImageWidth, LONG, 1, width
|
|
|
|
257, 4, 1, height, # ImageLength, LONG, 1, lenght
|
|
|
|
258, 3, 1, 1, # BitsPerSample, SHORT, 1, 1
|
|
|
|
259, 3, 1, ccitt_group, # Compression, SHORT, 1, 4 = CCITT Group 4
|
|
|
|
262, 3, 1, 1, # Threshholding, SHORT, 1, 0 = WhiteIsZero
|
2017-01-19 10:22:08 +00:00
|
|
|
273, 4, 1, struct.calcsize(
|
|
|
|
tiff_header_struct), # StripOffsets, LONG, 1, len of header
|
2016-07-21 23:33:26 +00:00
|
|
|
278, 4, 1, height, # RowsPerStrip, LONG, 1, lenght
|
|
|
|
279, 4, 1, img_size, # StripByteCounts, LONG, 1, size of image
|
|
|
|
0 # last IFD
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-03-01 04:51:53 +00:00
|
|
|
def test_suite():
|
2015-02-16 18:23:21 +00:00
|
|
|
class TestImg2Pdf(unittest.TestCase):
|
|
|
|
pass
|
|
|
|
|
2015-05-10 12:35:09 +00:00
|
|
|
for i, (psopt, isopt, border, fit, ao, pspdf1, ispdf1,
|
|
|
|
pspdf2, ispdf2) in enumerate(layout_test_cases):
|
|
|
|
if isopt is not None:
|
|
|
|
isopt = ((img2pdf.ImgSize.abs, isopt[0]),
|
|
|
|
(img2pdf.ImgSize.abs, isopt[1]))
|
|
|
|
|
|
|
|
def layout_handler(
|
|
|
|
self, psopt, isopt, border, fit, ao, pspdf, ispdf, im):
|
|
|
|
layout_fun = img2pdf.get_layout_fun(psopt, isopt, border, fit, ao)
|
|
|
|
try:
|
|
|
|
pwpdf, phpdf, iwpdf, ihpdf = \
|
|
|
|
layout_fun(im[0], im[1], (img2pdf.default_dpi,
|
|
|
|
img2pdf.default_dpi))
|
|
|
|
self.assertEqual((pwpdf, phpdf), pspdf)
|
|
|
|
self.assertEqual((iwpdf, ihpdf), ispdf)
|
2016-02-16 23:26:45 +00:00
|
|
|
except img2pdf.NegativeDimensionError:
|
2015-05-10 12:35:09 +00:00
|
|
|
self.assertEqual(None, pspdf)
|
|
|
|
self.assertEqual(None, ispdf)
|
|
|
|
|
|
|
|
def layout_handler_im1(self, psopt=psopt, isopt=isopt, border=border,
|
|
|
|
fit=fit, ao=ao, pspdf=pspdf1, ispdf=ispdf1):
|
|
|
|
layout_handler(self, psopt, isopt, border, fit, ao, pspdf, ispdf,
|
|
|
|
im1)
|
|
|
|
setattr(TestImg2Pdf, "test_layout_%03d_im1" % i, layout_handler_im1)
|
|
|
|
|
|
|
|
def layout_handler_im2(self, psopt=psopt, isopt=isopt, border=border,
|
|
|
|
fit=fit, ao=ao, pspdf=pspdf2, ispdf=ispdf2):
|
|
|
|
layout_handler(self, psopt, isopt, border, fit, ao, pspdf, ispdf,
|
|
|
|
im2)
|
|
|
|
setattr(TestImg2Pdf, "test_layout_%03d_im2" % i, layout_handler_im2)
|
|
|
|
|
|
|
|
files = os.listdir(os.path.join(HERE, "input"))
|
|
|
|
for with_pdfrw, test_name in [(a, b) for a in [True, False]
|
|
|
|
for b in files]:
|
2018-07-17 14:46:28 +00:00
|
|
|
# we do not test animation.gif with pdfrw because it doesn't support
|
|
|
|
# saving hexadecimal palette data
|
|
|
|
if test_name == 'animation.gif' and with_pdfrw:
|
|
|
|
continue
|
2015-02-16 18:23:21 +00:00
|
|
|
inputf = os.path.join(HERE, "input", test_name)
|
|
|
|
if not os.path.isfile(inputf):
|
|
|
|
continue
|
|
|
|
outputf = os.path.join(HERE, "output", test_name+".pdf")
|
|
|
|
assert os.path.isfile(outputf)
|
2015-05-10 12:35:09 +00:00
|
|
|
|
|
|
|
def handle(self, f=inputf, out=outputf, with_pdfrw=with_pdfrw):
|
2015-02-16 18:23:21 +00:00
|
|
|
with open(f, "rb") as inf:
|
|
|
|
orig_imgdata = inf.read()
|
2015-05-10 12:35:09 +00:00
|
|
|
output = img2pdf.convert(orig_imgdata, nodate=True,
|
|
|
|
with_pdfrw=with_pdfrw)
|
|
|
|
from pdfrw import PdfReader, PdfName, PdfWriter
|
|
|
|
from pdfrw.py23_diffs import convert_load, convert_store
|
2017-05-22 16:31:52 +00:00
|
|
|
x = PdfReader(PdfReaderIO(convert_load(output)))
|
2015-05-10 12:35:09 +00:00
|
|
|
self.assertEqual(sorted(x.keys()), [PdfName.Info, PdfName.Root,
|
|
|
|
PdfName.Size])
|
2017-01-20 07:47:58 +00:00
|
|
|
self.assertIn(x.Root.Pages.Count, ('1', '2'))
|
|
|
|
if len(x.Root.Pages.Kids) == '1':
|
|
|
|
self.assertEqual(x.Size, '7')
|
|
|
|
self.assertEqual(len(x.Root.Pages.Kids), 1)
|
|
|
|
elif len(x.Root.Pages.Kids) == '2':
|
|
|
|
self.assertEqual(x.Size, '10')
|
|
|
|
self.assertEqual(len(x.Root.Pages.Kids), 2)
|
2015-05-10 12:35:09 +00:00
|
|
|
self.assertEqual(x.Info, {})
|
|
|
|
self.assertEqual(sorted(x.Root.keys()), [PdfName.Pages,
|
|
|
|
PdfName.Type])
|
|
|
|
self.assertEqual(x.Root.Type, PdfName.Catalog)
|
|
|
|
self.assertEqual(sorted(x.Root.Pages.keys()),
|
|
|
|
[PdfName.Count, PdfName.Kids, PdfName.Type])
|
|
|
|
self.assertEqual(x.Root.Pages.Type, PdfName.Pages)
|
2017-01-20 07:47:58 +00:00
|
|
|
orig_img = Image.open(f)
|
|
|
|
for pagenum in range(len(x.Root.Pages.Kids)):
|
|
|
|
# retrieve the original image frame that this page was
|
|
|
|
# generated from
|
|
|
|
orig_img.seek(pagenum)
|
2017-01-20 08:19:30 +00:00
|
|
|
cur_page = x.Root.Pages.Kids[pagenum]
|
2015-05-10 12:35:09 +00:00
|
|
|
|
2017-01-20 07:47:58 +00:00
|
|
|
ndpi = orig_img.info.get("dpi", (96.0, 96.0))
|
|
|
|
# In python3, the returned dpi value for some tiff images will
|
|
|
|
# not be an integer but a float. To make the behaviour of
|
|
|
|
# img2pdf the same between python2 and python3, we convert that
|
|
|
|
# float into an integer by rounding.
|
|
|
|
# Search online for the 72.009 dpi problem for more info.
|
|
|
|
ndpi = (int(round(ndpi[0])), int(round(ndpi[1])))
|
|
|
|
imgwidthpx, imgheightpx = orig_img.size
|
2017-05-22 16:32:18 +00:00
|
|
|
pagewidth = 72.0*imgwidthpx/ndpi[0]
|
|
|
|
pageheight = 72.0*imgheightpx/ndpi[1]
|
2015-05-10 12:35:09 +00:00
|
|
|
|
2017-01-20 07:47:58 +00:00
|
|
|
def format_float(f):
|
|
|
|
if int(f) == f:
|
|
|
|
return str(int(f))
|
|
|
|
else:
|
|
|
|
return ("%.4f" % f).rstrip("0")
|
|
|
|
|
2017-01-20 08:19:30 +00:00
|
|
|
self.assertEqual(sorted(cur_page.keys()),
|
2017-01-20 07:47:58 +00:00
|
|
|
[PdfName.Contents, PdfName.MediaBox,
|
2017-01-20 08:19:30 +00:00
|
|
|
PdfName.Parent, PdfName.Resources,
|
|
|
|
PdfName.Type])
|
|
|
|
self.assertEqual(cur_page.MediaBox,
|
|
|
|
['0', '0', format_float(pagewidth),
|
|
|
|
format_float(pageheight)])
|
|
|
|
self.assertEqual(cur_page.Parent, x.Root.Pages)
|
|
|
|
self.assertEqual(cur_page.Type, PdfName.Page)
|
|
|
|
self.assertEqual(cur_page.Resources.keys(),
|
2017-01-20 07:47:58 +00:00
|
|
|
[PdfName.XObject])
|
2017-01-20 08:19:30 +00:00
|
|
|
self.assertEqual(cur_page.Resources.XObject.keys(),
|
2017-01-20 07:47:58 +00:00
|
|
|
[PdfName.Im0])
|
2017-01-20 08:19:30 +00:00
|
|
|
self.assertEqual(cur_page.Contents.keys(),
|
2017-01-20 07:47:58 +00:00
|
|
|
[PdfName.Length])
|
2017-01-20 08:19:30 +00:00
|
|
|
self.assertEqual(cur_page.Contents.Length,
|
|
|
|
str(len(cur_page.Contents.stream)))
|
|
|
|
self.assertEqual(cur_page.Contents.stream,
|
2017-01-20 07:47:58 +00:00
|
|
|
"q\n%.4f 0 0 %.4f 0.0000 0.0000 cm\n"
|
|
|
|
"/Im0 Do\nQ" % (pagewidth, pageheight))
|
|
|
|
|
2017-01-20 08:19:30 +00:00
|
|
|
imgprops = cur_page.Resources.XObject.Im0
|
2017-01-20 07:47:58 +00:00
|
|
|
|
|
|
|
# test if the filter is valid:
|
|
|
|
self.assertIn(
|
2018-07-17 21:59:55 +00:00
|
|
|
imgprops.Filter, [PdfName.DCTDecode, PdfName.JPXDecode,
|
|
|
|
PdfName.FlateDecode,
|
2018-07-17 23:14:06 +00:00
|
|
|
[PdfName.CCITTFaxDecode]])
|
2017-01-20 07:47:58 +00:00
|
|
|
|
|
|
|
# test if the image has correct size
|
|
|
|
self.assertEqual(imgprops.Width, str(orig_img.size[0]))
|
|
|
|
self.assertEqual(imgprops.Height, str(orig_img.size[1]))
|
|
|
|
# if the input file is a jpeg then it should've been copied
|
|
|
|
# verbatim into the PDF
|
2018-07-17 21:59:55 +00:00
|
|
|
if imgprops.Filter in [PdfName.DCTDecode,
|
|
|
|
PdfName.JPXDecode]:
|
2017-01-20 07:47:58 +00:00
|
|
|
self.assertEqual(
|
2017-01-20 08:19:30 +00:00
|
|
|
cur_page.Resources.XObject.Im0.stream,
|
2017-01-20 07:47:58 +00:00
|
|
|
convert_load(orig_imgdata))
|
2018-07-17 23:14:06 +00:00
|
|
|
elif imgprops.Filter == [PdfName.CCITTFaxDecode]:
|
2017-01-20 07:47:58 +00:00
|
|
|
tiff_header = tiff_header_for_ccitt(
|
|
|
|
int(imgprops.Width), int(imgprops.Height),
|
|
|
|
int(imgprops.Length), 4)
|
|
|
|
imgio = BytesIO()
|
|
|
|
imgio.write(tiff_header)
|
|
|
|
imgio.write(convert_store(
|
2017-01-20 08:19:30 +00:00
|
|
|
cur_page.Resources.XObject.Im0.stream))
|
2017-01-20 07:47:58 +00:00
|
|
|
imgio.seek(0)
|
|
|
|
im = Image.open(imgio)
|
|
|
|
self.assertEqual(im.tobytes(), orig_img.tobytes())
|
|
|
|
try:
|
|
|
|
im.close()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2016-07-21 23:33:26 +00:00
|
|
|
|
2018-07-17 21:59:55 +00:00
|
|
|
elif imgprops.Filter == PdfName.FlateDecode:
|
2017-01-20 08:19:30 +00:00
|
|
|
# otherwise, the data is flate encoded and has to be equal
|
|
|
|
# to the pixel data of the input image
|
2017-01-20 07:47:58 +00:00
|
|
|
imgdata = zlib.decompress(
|
2017-01-20 08:19:30 +00:00
|
|
|
convert_store(cur_page.Resources.XObject.Im0.stream))
|
2018-07-17 22:05:15 +00:00
|
|
|
if imgprops.DecodeParms:
|
|
|
|
if orig_img.format == 'PNG':
|
|
|
|
pngidat, palette = img2pdf.parse_png(orig_imgdata)
|
2018-08-01 20:28:44 +00:00
|
|
|
elif orig_img.format == 'TIFF' \
|
|
|
|
and orig_img.info['compression'] == "group4":
|
|
|
|
offset, length = \
|
|
|
|
img2pdf.ccitt_payload_location_from_pil(
|
|
|
|
orig_img)
|
2018-08-01 20:12:40 +00:00
|
|
|
pngidat = orig_imgdata[offset:offset+length]
|
2018-07-17 22:05:15 +00:00
|
|
|
else:
|
|
|
|
pngbuffer = BytesIO()
|
|
|
|
orig_img.save(pngbuffer, format="png")
|
2018-08-01 20:28:44 +00:00
|
|
|
pngidat, palette = img2pdf.parse_png(
|
|
|
|
pngbuffer.getvalue())
|
2018-07-17 22:05:15 +00:00
|
|
|
self.assertEqual(zlib.decompress(pngidat), imgdata)
|
2017-01-20 07:47:58 +00:00
|
|
|
else:
|
2018-07-17 22:05:15 +00:00
|
|
|
colorspace = imgprops.ColorSpace
|
|
|
|
if colorspace == PdfName.DeviceGray:
|
|
|
|
colorspace = 'L'
|
|
|
|
elif colorspace == PdfName.DeviceRGB:
|
|
|
|
colorspace = 'RGB'
|
|
|
|
elif colorspace == PdfName.DeviceCMYK:
|
|
|
|
colorspace = 'CMYK'
|
|
|
|
else:
|
|
|
|
raise Exception("invalid colorspace")
|
2018-08-01 20:28:44 +00:00
|
|
|
im = Image.frombytes(colorspace,
|
|
|
|
(int(imgprops.Width),
|
|
|
|
int(imgprops.Height)),
|
2018-07-17 22:05:15 +00:00
|
|
|
imgdata)
|
|
|
|
if orig_img.mode == '1':
|
|
|
|
self.assertEqual(im.tobytes(),
|
|
|
|
orig_img.convert("L").tobytes())
|
2018-08-01 20:28:44 +00:00
|
|
|
elif orig_img.mode not in ("RGB", "L", "CMYK",
|
|
|
|
"CMYK;I"):
|
2018-07-17 22:05:15 +00:00
|
|
|
self.assertEqual(im.tobytes(),
|
|
|
|
orig_img.convert("RGB").tobytes())
|
2018-08-01 20:28:44 +00:00
|
|
|
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does
|
|
|
|
# not have the close() method
|
2018-07-17 22:05:15 +00:00
|
|
|
try:
|
|
|
|
im.close()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-05-10 12:35:09 +00:00
|
|
|
# now use pdfrw to parse and then write out both pdfs and check the
|
|
|
|
# result for equality
|
|
|
|
y = PdfReader(out)
|
|
|
|
outx = BytesIO()
|
|
|
|
outy = BytesIO()
|
|
|
|
xwriter = PdfWriter()
|
|
|
|
ywriter = PdfWriter()
|
|
|
|
xwriter.trailer = x
|
|
|
|
ywriter.trailer = y
|
|
|
|
xwriter.write(outx)
|
|
|
|
ywriter.write(outy)
|
|
|
|
self.assertEqual(outx.getvalue(), outy.getvalue())
|
|
|
|
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not have the
|
|
|
|
# close() method
|
2015-03-07 02:01:02 +00:00
|
|
|
try:
|
|
|
|
orig_img.close()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-05-10 12:35:09 +00:00
|
|
|
if with_pdfrw:
|
|
|
|
setattr(TestImg2Pdf, "test_%s_with_pdfrw" % test_name, handle)
|
|
|
|
else:
|
|
|
|
setattr(TestImg2Pdf, "test_%s_without_pdfrw" % test_name, handle)
|
2015-02-16 18:23:21 +00:00
|
|
|
|
2014-03-01 04:51:53 +00:00
|
|
|
return unittest.TestSuite((
|
2015-02-16 18:23:21 +00:00
|
|
|
unittest.makeSuite(TestImg2Pdf),
|
2014-03-01 04:51:53 +00:00
|
|
|
))
|