-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_image_process.py
More file actions
851 lines (774 loc) · 31.3 KB
/
Copy pathtest_image_process.py
File metadata and controls
851 lines (774 loc) · 31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
import json
import os
from pathlib import Path
import shutil
import subprocess
import warnings
from PIL import Image, UnidentifiedImageError
import pytest
from pelican.plugins.image_process import (
ExifTool,
compute_paths,
harvest_images_in_fragment,
process_image,
set_default_settings,
try_open_image,
)
# Prepare test image constants.
HERE = Path(__file__).resolve().parent
TEST_DATA = HERE.joinpath("test_data").resolve()
SUPPORTED_TRANSPARENT_IMAGE_FORMATS = ["png", "webp"]
SUPPORTED_IMAGE_FORMATS = [*SUPPORTED_TRANSPARENT_IMAGE_FORMATS, "jpg"]
SUPPORTED_EXIF_IMAGE_FORMATS = ["png", "jpg"] # Exiftool cannot write webp images.
TRANSFORM_TEST_IMAGES = [
TEST_DATA.joinpath(f"{file}.png").resolve()
for file in ["pelican-bird", "black-borders", "alpha-borders"]
]
FILE_FORMAT_TEST_IMAGES = [
TEST_DATA.joinpath(f"pelican-bird.{ext}").resolve()
for ext in SUPPORTED_IMAGE_FORMATS
]
FILE_FORMAT_TEST_IMAGES = FILE_FORMAT_TEST_IMAGES + [
TEST_DATA.joinpath(f"alpha-borders.{ext}").resolve()
for ext in SUPPORTED_TRANSPARENT_IMAGE_FORMATS
]
EXIF_TEST_IMAGES = [
TEST_DATA.joinpath("exif", f"pelican-bird.{ext}").resolve()
for ext in SUPPORTED_EXIF_IMAGE_FORMATS
]
TRANSFORM_RESULTS = TEST_DATA.joinpath("results").resolve()
# Register all supported transforms.
SINGLE_TRANSFORMS = {
"crop": ["crop 350 250 650 450"],
"flip_horizontal": ["flip_horizontal"],
"flip_vertical": ["flip_vertical"],
"grayscale": ["grayscale"],
"resize": ["resize 200 250"],
"rotate": ["rotate 20"],
"scale_in": ["scale_in 200 250 False"],
"scale_out": ["scale_out 200 250 False"],
"blur": ["blur"],
"contour": ["contour"],
"detail": ["detail"],
"edge_enhance": ["edge_enhance"],
"edge_enhance_more": ["edge_enhance_more"],
"emboss": ["emboss"],
"find_edges": ["find_edges"],
"smooth": ["smooth"],
"smooth_more": ["smooth_more"],
"sharpen": ["sharpen"],
}
# The expected sizes of the transformed images.
EXPECTED_SIZES = {
"crop": (300, 200),
"resize": (200, 250),
"rotate": (1226, 1072),
"scale_in": (200, 150),
"scale_out": (333, 250),
}
def get_settings(**kwargs):
"""Provide tweaked setting dictionaries for testing.
Set keyword arguments to override specific settings.
"""
DEFAULT_CONFIG = {
"PATH": HERE,
"OUTPUT_PATH": "output",
"static_content": {},
"filenames": {},
"SITEURL": "//",
"IMAGE_PROCESS": SINGLE_TRANSFORMS,
}
settings = DEFAULT_CONFIG.copy()
settings.update(kwargs)
set_default_settings(settings)
return settings
def test_undefined_transform():
settings = get_settings()
tag = "<img class='image-process-undefined' src='/tmp/test.jpg' />"
with pytest.raises(RuntimeError):
harvest_images_in_fragment(tag, settings)
@pytest.mark.parametrize("transform_id, transform_params", SINGLE_TRANSFORMS.items())
@pytest.mark.parametrize("image_path", TRANSFORM_TEST_IMAGES)
def test_all_transforms(tmp_path, transform_id, transform_params, image_path):
"""Test the raw transform and their results on images."""
settings = get_settings()
image_name = image_path.name
destination_path = tmp_path.joinpath(transform_id, image_name)
expected_path = TRANSFORM_RESULTS.joinpath(transform_id, image_name)
process_image((str(image_path), str(destination_path), transform_params), settings)
transformed = Image.open(destination_path)
expected = Image.open(expected_path)
assert transformed.size == expected.size
# We need to make our tests slightly tolerant because
# the `detail` and `smooth_more` filters are slightly different in Pillow 10.3+
# depending on the platform on which they are run.
if transformed.mode == "RGB":
for _, (transformed_pixel, expected_pixel) in enumerate(
zip(transformed.getdata(), expected.getdata(), strict=False)
):
assert abs(transformed_pixel[0] - expected_pixel[0]) <= 1
assert abs(transformed_pixel[1] - expected_pixel[1]) <= 1
assert abs(transformed_pixel[2] - expected_pixel[2]) <= 1
elif transformed.mode == "RGBA":
for _, (transformed_pixel, expected_pixel) in enumerate(
zip(transformed.getdata(), expected.getdata(), strict=False)
):
assert abs(transformed_pixel[0] - expected_pixel[0]) <= 1
assert abs(transformed_pixel[1] - expected_pixel[1]) <= 1
assert abs(transformed_pixel[2] - expected_pixel[2]) <= 1
assert abs(transformed_pixel[3] - expected_pixel[3]) <= 1
elif transformed.mode == "L":
for _, (transformed_pixel, expected_pixel) in enumerate(
zip(transformed.getdata(), expected.getdata(), strict=False)
):
assert abs(transformed_pixel - expected_pixel) <= 1
else:
raise ValueError(f"Unsupported image mode: {transformed.mode}")
@pytest.mark.parametrize("image_path", FILE_FORMAT_TEST_IMAGES)
def test_image_formats(tmp_path, image_path):
"""Test that we can process images in various formats."""
settings = get_settings()
output_ext = image_path.suffix.lower()
destination_path = tmp_path.joinpath(f"processed.{output_ext}")
process_image((str(image_path), str(destination_path), []), settings)
original = Image.open(image_path)
transformed = Image.open(destination_path)
assert transformed.size == original.size
@pytest.mark.parametrize(
"orig_src, orig_img, new_src, new_img",
[
(
"/tmp/test.jpg",
"tmp/test.jpg",
"/tmp/derivatives/crop/test.jpg",
"tmp/derivatives/crop/test.jpg",
),
(
"../tmp/test.jpg",
"../tmp/test.jpg",
"../tmp/derivatives/crop/test.jpg",
"../tmp/derivatives/crop/test.jpg",
),
(
"http://xxx/tmp/test.jpg",
"tmp/test.jpg",
"http://xxx/tmp/derivatives/crop/test.jpg",
"tmp/derivatives/crop/test.jpg",
),
],
)
def test_path_normalization(mocker, orig_src, orig_img, new_src, new_img):
# Silence image transforms.
process = mocker.patch("pelican.plugins.image_process.image_process.process_image")
settings = get_settings(IMAGE_PROCESS_DIR="derivatives")
img_tag_orig = (
f'<img class="test image-process image-process-crop test2" src="{orig_src}"/>'
)
img_tag_processed = harvest_images_in_fragment(img_tag_orig, settings)
assert img_tag_processed == (
f'<img class="test image-process image-process-crop test2" src="{new_src}"/>'
)
process.assert_called_once_with(
(
os.path.join(settings["PATH"], orig_img),
os.path.join(settings["OUTPUT_PATH"], new_img),
SINGLE_TRANSFORMS["crop"],
),
settings,
)
COMPLEX_TRANSFORMS = {
"thumb": ["crop 0 0 50% 50%", "scale_out 150 150", "crop 0 0 150 150"],
"article-image": {"type": "image", "ops": ["scale_in 300 300"]},
"crisp": {
"type": "responsive-image",
"srcset": [
("1x", ["scale_in 800 600 True"]),
("2x", ["scale_in 1600 1200 True"]),
("4x", ["scale_in 3200 2400 True"]),
],
"default": "1x",
},
"crisp2": {
"type": "responsive-image",
"srcset": [
("1x", ["scale_in 800 600 True"]),
("2x", ["scale_in 1600 1200 True"]),
("4x", ["scale_in 3200 2400 True"]),
],
"default": ["scale_in 400 300 True"],
},
"large-photo": {
"type": "responsive-image",
"sizes": (
"(min-width: 1200px) 800px, "
"(min-width: 992px) 650px, "
"(min-width: 768px) 718px, "
"100vw"
),
"srcset": [
("600w", ["scale_in 600 450 True"]),
("800w", ["scale_in 800 600 True"]),
("1600w", ["scale_in 1600 1200 True"]),
],
"default": "800w",
},
"pict": {
"type": "picture",
"sources": [
{
"name": "default",
"media": "(min-width: 640px)",
"srcset": [
("640w", ["scale_in 640 480 True"]),
("1024w", ["scale_in 1024 683 True"]),
("1600w", ["scale_in 1600 1200 True"]),
],
"sizes": "100vw",
},
{
"name": "source-1",
"srcset": [
("1x", ["crop 100 100 200 200"]),
("2x", ["crop 100 100 300 300"]),
],
},
],
"default": ("default", "640w"),
},
"pict2": {
"type": "picture",
"sources": [
{
"name": "default",
"media": "(min-width: 640px)",
"srcset": [
("640w", ["scale_in 640 480 True"]),
("1024w", ["scale_in 1024 683 True"]),
("1600w", ["scale_in 1600 1200 True"]),
],
"sizes": "100vw",
},
{
"name": "source-2",
"srcset": [
("1x", ["crop 100 100 200 200"]),
("2x", ["crop 100 100 300 300"]),
],
},
],
"default": ("source-2", ["scale_in 800 600 True"]),
},
}
@pytest.mark.parametrize(
"orig_tag, new_tag, call_args",
[
(
'<img class="image-process-thumb" src="/tmp/test.jpg" />',
'<img class="image-process-thumb" src="/tmp/derivs/thumb/test.jpg"/>',
[
(
"tmp/test.jpg",
"tmp/derivs/thumb/test.jpg",
["crop 0 0 50% 50%", "scale_out 150 150", "crop 0 0 150 150"],
),
],
),
(
'<img class="image-process-article-image" src="/tmp/test.jpg" />',
'<img class="image-process-article-image" '
'src="/tmp/derivs/article-image/test.jpg"/>',
[
(
"tmp/test.jpg",
"tmp/derivs/article-image/test.jpg",
["scale_in 300 300"],
),
],
),
(
'<img class="image-process-crisp" src="/tmp/test.jpg" />',
'<img class="image-process-crisp" src="/tmp/derivs/crisp/1x/test.jpg" '
'srcset="/tmp/derivs/crisp/1x/test.jpg 1x, '
"/tmp/derivs/crisp/2x/test.jpg 2x, "
'/tmp/derivs/crisp/4x/test.jpg 4x"/>',
[
(
"tmp/test.jpg",
"tmp/derivs/crisp/1x/test.jpg",
["scale_in 800 600 True"],
),
(
"tmp/test.jpg",
"tmp/derivs/crisp/2x/test.jpg",
["scale_in 1600 1200 True"],
),
(
"tmp/test.jpg",
"tmp/derivs/crisp/4x/test.jpg",
["scale_in 3200 2400 True"],
),
],
),
(
'<img class="image-process-crisp2" src="/tmp/test.jpg" />',
'<img class="image-process-crisp2" '
'src="/tmp/derivs/crisp2/default/test.jpg" '
'srcset="/tmp/derivs/crisp2/1x/test.jpg 1x, '
"/tmp/derivs/crisp2/2x/test.jpg 2x, "
'/tmp/derivs/crisp2/4x/test.jpg 4x"/>',
# default must be first, because the process execute it first
[
(
"tmp/test.jpg",
"tmp/derivs/crisp2/default/test.jpg",
["scale_in 400 300 True"],
),
(
"tmp/test.jpg",
"tmp/derivs/crisp2/1x/test.jpg",
["scale_in 800 600 True"],
),
(
"tmp/test.jpg",
"tmp/derivs/crisp2/2x/test.jpg",
["scale_in 1600 1200 True"],
),
(
"tmp/test.jpg",
"tmp/derivs/crisp2/4x/test.jpg",
["scale_in 3200 2400 True"],
),
],
),
(
'<img class="image-process-large-photo" src="/tmp/test.jpg" />',
'<img class="image-process-large-photo" '
'sizes="(min-width: 1200px) 800px, (min-width: 992px) 650px, '
'(min-width: 768px) 718px, 100vw" src="/tmp/derivs/large-photo/'
'800w/test.jpg" '
'srcset="/tmp/derivs/large-photo/600w/test.jpg 600w, '
"/tmp/derivs/large-photo/800w/test.jpg 800w, "
'/tmp/derivs/large-photo/1600w/test.jpg 1600w"/>',
[
(
"tmp/test.jpg",
"tmp/derivs/large-photo/600w/test.jpg",
["scale_in 600 450 True"],
),
(
"tmp/test.jpg",
"tmp/derivs/large-photo/800w/test.jpg",
["scale_in 800 600 True"],
),
(
"tmp/test.jpg",
"tmp/derivs/large-photo/1600w/test.jpg",
["scale_in 1600 1200 True"],
),
],
),
(
'<picture><source class="source-1" '
'src="/images/pelican-closeup.jpg"/><img '
'class="image-process-pict" src="/images/pelican.jpg"/>'
"</picture>",
'<picture><source media="(min-width: 640px)" sizes="100vw" '
'srcset="/images/derivs/pict/default/640w/pelican.jpg 640w, '
"/images/derivs/pict/default/1024w/pelican.jpg 1024w, "
'/images/derivs/pict/default/1600w/pelican.jpg 1600w"/>'
'<source srcset="/images/derivs/pict/source-1/1x/'
"pelican-closeup.jpg 1x, /images/derivs/pict/source-1/2x/"
'pelican-closeup.jpg 2x"/><img '
'class="image-process-pict" '
'src="/images/derivs/pict/default/640w/pelican.jpg"/>'
"</picture>",
[
(
"images/pelican.jpg",
"images/derivs/pict/default/640w/pelican.jpg",
["scale_in 640 480 True"],
),
(
"images/pelican.jpg",
"images/derivs/pict/default/1024w/pelican.jpg",
["scale_in 1024 683 True"],
),
(
"images/pelican.jpg",
"images/derivs/pict/default/1600w/pelican.jpg",
["scale_in 1600 1200 True"],
),
(
"images/pelican-closeup.jpg",
"images/derivs/pict/source-1/1x/pelican-closeup.jpg",
["crop 100 100 200 200"],
),
(
"images/pelican-closeup.jpg",
"images/derivs/pict/source-1/2x/pelican-closeup.jpg",
["crop 100 100 300 300"],
),
],
),
(
'<div class="figure"><img alt="Pelican" class="image-process'
'-pict2" src="/images/pelican.jpg" /> <p class="caption">'
'A nice pelican</p> <div class="legend"> <img alt="Other view of '
'pelican" class="image-process source-2" src="/images/'
'pelican-closeup.jpg" /></div></div>',
'<div class="figure"><picture><source media="(min-width: 640px)" '
'sizes="100vw" srcset="/images/derivs/pict2/default/640w/pelican'
".jpg 640w, /images/derivs/pict2/default/1024w/pelican.jpg 1024w,"
' /images/derivs/pict2/default/1600w/pelican.jpg 1600w"/>'
'<source srcset="/images/derivs/pict2/source-2/1x/pelican-closeup'
".jpg 1x, /images/derivs/pict2/source-2/2x/pelican-closeup.jpg "
'2x"/><img alt="Pelican" class="image-process-pict2" '
'src="/images/derivs/pict2/source-2/default/pelican-closeup.jpg"'
'/></picture> <p class="caption">A nice pelican</p> <div '
'class="legend"> </div></div>',
[
# Default calls first.
(
"images/pelican-closeup.jpg",
"images/derivs/pict2/source-2/default/pelican-closeup.jpg",
["scale_in 800 600 True"],
),
(
"images/pelican.jpg",
"images/derivs/pict2/default/640w/pelican.jpg",
["scale_in 640 480 True"],
),
# Then images in processing order.
(
"images/pelican.jpg",
"images/derivs/pict2/default/1024w/pelican.jpg",
["scale_in 1024 683 True"],
),
(
"images/pelican.jpg",
"images/derivs/pict2/default/1600w/pelican.jpg",
["scale_in 1600 1200 True"],
),
(
"images/pelican-closeup.jpg",
"images/derivs/pict2/source-2/1x/pelican-closeup.jpg",
["crop 100 100 200 200"],
),
(
"images/pelican-closeup.jpg",
"images/derivs/pict2/source-2/2x/pelican-closeup.jpg",
["crop 100 100 300 300"],
),
],
),
],
)
def test_html_and_pictures_generation(mocker, orig_tag, new_tag, call_args):
"""Tests that the generated html is as expected and the images are processed."""
process = mocker.patch("pelican.plugins.image_process.image_process.process_image")
settings = get_settings(
IMAGE_PROCESS=COMPLEX_TRANSFORMS, IMAGE_PROCESS_DIR="derivs"
)
assert harvest_images_in_fragment(orig_tag, settings) == new_tag
# Check that process_image was called with the expected arguments
# and in the expecter order.
for i, (orig_img, new_img, transform_params) in enumerate(call_args):
assert process.mock_calls[i] == mocker.call(
(
os.path.join(settings["PATH"], orig_img),
os.path.join(settings["OUTPUT_PATH"], new_img),
transform_params,
),
settings,
)
@pytest.mark.parametrize(
"orig_tag, new_tag",
[
# <img/> src attribute with no quotes, spaces or commas.
(
'<img class="image-process-thumb" src="/tmp/my&_dir/my!_test.jpg" />',
'<img class="image-process-thumb" '
'src="/tmp/my&_dir/derivs/thumb/my!_test.jpg"/>',
),
# <img/> src attribute with double quotes, spaces and commas.
(
'<img class="image-process-thumb" '
'src="/tmp/my," dir/my "test,.jpg" />',
'<img class="image-process-thumb" '
"src='/tmp/my,\" dir/derivs/thumb/my \"test,.jpg'/>",
),
# <img/> src attribute with single and double quotes, spaces and commas.
(
'<img class="image-process-thumb" '
'src="/tmp/m\'y," dir/my "test,.jpg" />',
'<img class="image-process-thumb" '
'src="/tmp/m\'y," dir/derivs/thumb/my "test,.jpg"/>',
),
# <img/> srcset attribute with no quotes, spaces or commas.
(
'<img class="image-process-crisp" src="/tmp/my&_dir/my!_test.jpg" />',
'<img class="image-process-crisp" '
'src="/tmp/my&_dir/derivs/crisp/1x/my!_test.jpg" '
'srcset="/tmp/my&_dir/derivs/crisp/1x/my!_test.jpg 1x, '
"/tmp/my&_dir/derivs/crisp/2x/my!_test.jpg 2x, "
'/tmp/my&_dir/derivs/crisp/4x/my!_test.jpg 4x"/>',
),
# <img/> srcset attribute with double quotes, spaces and commas.
(
'<img class="image-process-crisp" '
'src="/tmp/my," dir/my "test,.jpg" />',
'<img class="image-process-crisp" '
"src='/tmp/my,\" dir/derivs/crisp/1x/my \"test,.jpg' "
'srcset="/tmp/my%2C%22%20dir/derivs/crisp/1x/my%20%22test%2C.jpg 1x, '
"/tmp/my%2C%22%20dir/derivs/crisp/2x/my%20%22test%2C.jpg 2x, "
'/tmp/my%2C%22%20dir/derivs/crisp/4x/my%20%22test%2C.jpg 4x"/>',
),
# <img/> srcset attribute with single and double quotes, spaces and commas.
(
'<img class="image-process-crisp" '
'src="/tmp/m\'y," dir/my "test,.jpg" />',
'<img class="image-process-crisp" '
'src="/tmp/m\'y," dir/derivs/crisp/1x/my "test,.jpg" '
'srcset="/tmp/m%27y%2C%22%20dir/derivs/crisp/1x/my%20%22test%2C.jpg 1x, '
"/tmp/m%27y%2C%22%20dir/derivs/crisp/2x/my%20%22test%2C.jpg 2x, "
'/tmp/m%27y%2C%22%20dir/derivs/crisp/4x/my%20%22test%2C.jpg 4x"/>',
),
# <picture/> src and srcset attributes with no quotes, spaces or commas.
(
'<picture><source class="source-1" '
'src="/my&_dir/my!_pelican-closeup.jpg"/><img '
'class="image-process-pict" src="/my&_dir/my!_pelican.jpg"/>'
"</picture>",
'<picture><source media="(min-width: 640px)" sizes="100vw" '
'srcset="/my&_dir/derivs/pict/default/640w/'
"my!_pelican.jpg 640w, "
"/my&_dir/derivs/pict/default/1024w/my!_pelican.jpg 1024w, "
'/my&_dir/derivs/pict/default/1600w/my!_pelican.jpg 1600w"/>'
'<source srcset="/my&_dir/derivs/pict/source-1/1x/'
"my!_pelican-closeup.jpg 1x, "
"/my&_dir/derivs/pict/source-1/2x/"
'my!_pelican-closeup.jpg 2x"/><img '
'class="image-process-pict" '
'src="/my&_dir/derivs/pict/default/640w/my!_pelican.jpg"/>'
"</picture>",
),
# <picture/> src and srcset attributes with double quotes, spaces and commas.
(
'<picture><source class="source-1" '
'src="/my," dir/my "pelican-closeup,.jpg"/><img '
'class="image-process-pict" src="/my," dir/my "pelican,.jpg"/>'
"</picture>",
'<picture><source media="(min-width: 640px)" sizes="100vw" '
'srcset="/my%2C%22%20dir/derivs/pict/default/640w/'
"my%20%22pelican%2C.jpg 640w, "
"/my%2C%22%20dir/derivs/pict/default/1024w/my%20%22pelican%2C.jpg 1024w, "
'/my%2C%22%20dir/derivs/pict/default/1600w/my%20%22pelican%2C.jpg 1600w"/>'
'<source srcset="/my%2C%22%20dir/derivs/pict/source-1/1x/'
"my%20%22pelican-closeup%2C.jpg 1x, "
"/my%2C%22%20dir/derivs/pict/source-1/2x/"
'my%20%22pelican-closeup%2C.jpg 2x"/><img '
'class="image-process-pict" '
"src='/my,\" dir/derivs/pict/default/640w/my \"pelican,.jpg'/>"
"</picture>",
),
# <picture/> src and srcset attributes
# with single and double quotes, spaces and commas.
(
'<picture><source class="source-1" '
'src="/m\'y," dir/my "pelican-closeup,.jpg"/><img '
'class="image-process-pict" src="/m\'y," dir/my "pelican,.jpg"/>'
"</picture>",
'<picture><source media="(min-width: 640px)" sizes="100vw" '
'srcset="/m%27y%2C%22%20dir/derivs/pict/default/640w/'
"my%20%22pelican%2C.jpg 640w, "
"/m%27y%2C%22%20dir/derivs/pict/default/1024w/"
"my%20%22pelican%2C.jpg 1024w, "
"/m%27y%2C%22%20dir/derivs/pict/default/1600w/"
'my%20%22pelican%2C.jpg 1600w"/>'
'<source srcset="/m%27y%2C%22%20dir/derivs/pict/source-1/1x/'
"my%20%22pelican-closeup%2C.jpg 1x, "
"/m%27y%2C%22%20dir/derivs/pict/source-1/2x/"
'my%20%22pelican-closeup%2C.jpg 2x"/><img '
'class="image-process-pict" '
'src="/m\'y," dir/derivs/pict/default/640w/my "pelican,.jpg"/>'
"</picture>",
),
],
)
def test_special_chars_in_image_path_are_handled_properly(mocker, orig_tag, new_tag):
"""Tests that special characters in image paths are handled properly.
For the src attribute, single or double quotes may need to be escaped,
according to the quotation mark used to enclose the attribute value.
For the srcset attribute, in addition to quotes, spaces and commas
need to be url-encoded.
Related to issue #78 https://github.com/pelican-plugins/image-process/issues/78
"""
mocker.patch("pelican.plugins.image_process.image_process.process_image")
settings = get_settings(
IMAGE_PROCESS=COMPLEX_TRANSFORMS, IMAGE_PROCESS_DIR="derivs"
)
assert harvest_images_in_fragment(orig_tag, settings) == new_tag
def process_image_mock_exif_tool_started(image, settings):
assert ExifTool._instance is not None
def process_image_mock_exif_tool_not_started(image, settings):
assert ExifTool._instance is None
@pytest.mark.parametrize("copy_tags", [True, False])
def test_exiftool_process_is_started_only_when_necessary(mocker, copy_tags):
if shutil.which("exiftool") is None:
warnings.warn(
"EXIF tags copying will not be tested because the exiftool program could "
"not be found. Please install exiftool and make sure it is in your path.",
stacklevel=2,
)
return
if copy_tags:
mocker.patch(
"pelican.plugins.image_process.image_process.process_image",
process_image_mock_exif_tool_started,
)
else:
mocker.patch(
"pelican.plugins.image_process.image_process.process_image",
process_image_mock_exif_tool_not_started,
)
settings = get_settings(
IMAGE_PROCESS_COPY_EXIF_TAGS=copy_tags,
IMAGE_PROCESS=COMPLEX_TRANSFORMS,
IMAGE_PROCESS_DIR="derivs",
)
harvest_images_in_fragment(
'<img class="image-process-thumb" src="/tmp/test.jpg" />', settings
)
@pytest.mark.parametrize("image_path", EXIF_TEST_IMAGES)
@pytest.mark.parametrize("copy_tags", [True, False])
def test_copy_exif_tags(tmp_path, image_path, copy_tags):
if shutil.which("exiftool") is None:
warnings.warn(
"EXIF tags copying will not be tested because the exiftool program could "
"not be found. Please install exiftool and make sure it is in your path.",
stacklevel=2,
)
return
# A few EXIF tags to test for.
exif_tags = [
"Artist",
"Creator",
"Title",
"Description",
"Subject",
"Rating",
"ExifImageWidth",
]
settings = get_settings(IMAGE_PROCESS_COPY_EXIF_TAGS=copy_tags)
transform_id = "grayscale"
transform_params = ["grayscale"]
image_name = image_path.name
destination_path = tmp_path.joinpath(transform_id, image_name)
expected_results = subprocess.run(
["exiftool", "-json", image_path], stdout=subprocess.PIPE, check=False
)
expected_tags = json.loads(expected_results.stdout)[0]
for tag in exif_tags:
assert tag in expected_tags
if copy_tags:
ExifTool.start_exiftool()
process_image((str(image_path), str(destination_path), transform_params), settings)
if copy_tags:
ExifTool.stop_exiftool()
actual_results = subprocess.run(
["exiftool", "-json", destination_path], stdout=subprocess.PIPE, check=False
)
assert actual_results.returncode == 0
actual_tags = json.loads(actual_results.stdout)[0]
for tag in exif_tags:
if copy_tags:
assert tag in actual_tags
assert expected_tags[tag] == actual_tags[tag]
else:
assert tag not in actual_tags
def test_try_open_image():
for test_image in FILE_FORMAT_TEST_IMAGES:
assert try_open_image(test_image)
with pytest.raises(FileNotFoundError):
try_open_image("image/that/does/not/exist.png")
with pytest.raises(UnidentifiedImageError):
assert not try_open_image(TEST_DATA.joinpath("folded_puzzle.png"))
assert not try_open_image(TEST_DATA.joinpath("minimal.svg"))
img = {"src": "https://upload.wikimedia.org/wikipedia/commons/3/34/Exemple.png"}
settings = get_settings(IMAGE_PROCESS_DIR="derivatives")
path = compute_paths(img, settings, derivative="thumb")
with pytest.raises(FileNotFoundError):
assert not try_open_image(path.source)
@pytest.mark.parametrize(
"orig_tag, new_tag, setting_overrides",
[
(
'<img class="image-process-crop" src="/tmp/test.jpg" />',
'<img class="image-process-crop" src="/tmp/derivatives/crop/test.jpg"/>',
[ # Default settings.
{},
{"IMAGE_PROCESS_ADD_CLASS": True},
{"IMAGE_PROCESS_CLASS_PREFIX": "image-process-"},
{
"IMAGE_PROCESS_ADD_CLASS": True,
"IMAGE_PROCESS_CLASS_PREFIX": "image-process-",
},
],
),
(
'<img class="image-process-crop" src="/tmp/test.jpg" />',
'<img class="custom-prefix-crop" src="/tmp/derivatives/crop/test.jpg"/>',
[ # Custom class prefix.
{"IMAGE_PROCESS_CLASS_PREFIX": "custom-prefix-"},
{
"IMAGE_PROCESS_ADD_CLASS": True,
"IMAGE_PROCESS_CLASS_PREFIX": "custom-prefix-",
},
],
),
(
'<img class="image-process-crop" src="/tmp/test.jpg" />',
'<img class="crop" src="/tmp/derivatives/crop/test.jpg"/>',
[ # Special case: empty string as class prefix.
{"IMAGE_PROCESS_CLASS_PREFIX": ""},
],
),
(
'<img class="image-process-crop" src="/tmp/test.jpg" />',
'<img src="/tmp/derivatives/crop/test.jpg"/>',
[ # No class added.
{"IMAGE_PROCESS_ADD_CLASS": False},
{"IMAGE_PROCESS_ADD_CLASS": False, "IMAGE_PROCESS_CLASS_PREFIX": ""},
],
),
],
)
def test_class_settings(mocker, orig_tag, new_tag, setting_overrides):
"""Test the IMAGE_PROCESS_ADD_CLASS and IMAGE_PROCESS_CLASS_PREFIX settings."""
# Silence image transforms.
mocker.patch("pelican.plugins.image_process.image_process.process_image")
for override in setting_overrides:
settings = get_settings(**override)
assert harvest_images_in_fragment(orig_tag, settings) == new_tag
def generate_test_images():
settings = get_settings()
image_count = 0
for image_path in TRANSFORM_TEST_IMAGES:
for transform_id, transform_params in SINGLE_TRANSFORMS.items():
destination_path = str(
TRANSFORM_RESULTS.joinpath(transform_id, image_path.name)
)
process_image(
(
str(image_path),
destination_path,
transform_params,
),
settings,
)
image_count += 1
# Check the size of the transformed image.
expected_size = EXPECTED_SIZES.get(transform_id)
transformed = Image.open(destination_path)
assert expected_size is None or expected_size == transformed.size
print(f"{image_count} test images generated!") # noqa: T201