17
17
from resizeimage .imageexceptions import ImageSizeError
18
18
19
19
from zimscraperlib .image import presets
20
- from zimscraperlib .image .conversion import convert_image , create_favicon
20
+ from zimscraperlib .image .conversion import (
21
+ convert_image ,
22
+ convert_svg2png ,
23
+ create_favicon ,
24
+ )
21
25
from zimscraperlib .image .optimization import (
22
26
ensure_matches ,
23
27
get_optimization_method ,
@@ -64,8 +68,15 @@ def get_src_dst(
64
68
jpg_image : pathlib .Path | None = None ,
65
69
gif_image : pathlib .Path | None = None ,
66
70
webp_image : pathlib .Path | None = None ,
71
+ svg_image : pathlib .Path | None = None ,
67
72
) -> tuple [pathlib .Path , pathlib .Path ]:
68
- options = {"png" : png_image , "jpg" : jpg_image , "webp" : webp_image , "gif" : gif_image }
73
+ options = {
74
+ "png" : png_image ,
75
+ "jpg" : jpg_image ,
76
+ "webp" : webp_image ,
77
+ "gif" : gif_image ,
78
+ "svg" : svg_image ,
79
+ }
69
80
if fmt not in options :
70
81
raise LookupError (f"Unsupported fmt passed: { fmt } " )
71
82
src = options [fmt ]
@@ -328,6 +339,42 @@ def test_convert_path_src_io_dst(png_image: pathlib.Path):
328
339
assert dst_image .format == "PNG"
329
340
330
341
342
+ def test_convert_svg_io_src_path_dst (svg_image : pathlib .Path , tmp_path : pathlib .Path ):
343
+ src = io .BytesIO (svg_image .read_bytes ())
344
+ dst = tmp_path / "test.png"
345
+ convert_svg2png (src , dst )
346
+ dst_image = Image .open (dst )
347
+ assert dst_image .format == "PNG"
348
+
349
+
350
+ def test_convert_svg_io_src_io_dst (svg_image : pathlib .Path ):
351
+ src = io .BytesIO (svg_image .read_bytes ())
352
+ dst = io .BytesIO ()
353
+ convert_svg2png (src , dst )
354
+ dst_image = Image .open (dst )
355
+ assert dst_image .format == "PNG"
356
+
357
+
358
+ def test_convert_svg_path_src_path_dst (svg_image : pathlib .Path , tmp_path : pathlib .Path ):
359
+ src = svg_image
360
+ dst = tmp_path / "test.png"
361
+ convert_svg2png (src , dst , width = 96 , height = 96 )
362
+ dst_image = Image .open (dst )
363
+ assert dst_image .format == "PNG"
364
+ assert dst_image .width == 96
365
+ assert dst_image .height == 96
366
+
367
+
368
+ def test_convert_svg_path_src_io_dst (svg_image : pathlib .Path ):
369
+ src = svg_image
370
+ dst = io .BytesIO ()
371
+ convert_svg2png (src , dst , width = 96 , height = 96 )
372
+ dst_image = Image .open (dst )
373
+ assert dst_image .format == "PNG"
374
+ assert dst_image .width == 96
375
+ assert dst_image .height == 96
376
+
377
+
331
378
@pytest .mark .parametrize (
332
379
"fmt,exp_size" ,
333
380
[("png" , 128 ), ("jpg" , 128 )],
@@ -576,10 +623,10 @@ def test_ensure_matches(webp_image):
576
623
577
624
@pytest .mark .parametrize (
578
625
"fmt,expected" ,
579
- [("png" , "PNG" ), ("jpg" , "JPEG" ), ("gif" , "GIF" ), ("webp" , "WEBP" )],
626
+ [("png" , "PNG" ), ("jpg" , "JPEG" ), ("gif" , "GIF" ), ("webp" , "WEBP" ), ( "svg" , "SVG" ) ],
580
627
)
581
628
def test_format_for_real_images_suffix (
582
- png_image , jpg_image , gif_image , webp_image , tmp_path , fmt , expected
629
+ png_image , jpg_image , gif_image , webp_image , svg_image , tmp_path , fmt , expected
583
630
):
584
631
src , _ = get_src_dst (
585
632
tmp_path ,
@@ -588,16 +635,17 @@ def test_format_for_real_images_suffix(
588
635
jpg_image = jpg_image ,
589
636
gif_image = gif_image ,
590
637
webp_image = webp_image ,
638
+ svg_image = svg_image ,
591
639
)
592
640
assert format_for (src ) == expected
593
641
594
642
595
643
@pytest .mark .parametrize (
596
644
"fmt,expected" ,
597
- [("png" , "PNG" ), ("jpg" , "JPEG" ), ("gif" , "GIF" ), ("webp" , "WEBP" )],
645
+ [("png" , "PNG" ), ("jpg" , "JPEG" ), ("gif" , "GIF" ), ("webp" , "WEBP" ), ( "svg" , "SVG" ) ],
598
646
)
599
647
def test_format_for_real_images_content_path (
600
- png_image , jpg_image , gif_image , webp_image , tmp_path , fmt , expected
648
+ png_image , jpg_image , gif_image , webp_image , svg_image , tmp_path , fmt , expected
601
649
):
602
650
src , _ = get_src_dst (
603
651
tmp_path ,
@@ -606,16 +654,17 @@ def test_format_for_real_images_content_path(
606
654
jpg_image = jpg_image ,
607
655
gif_image = gif_image ,
608
656
webp_image = webp_image ,
657
+ svg_image = svg_image ,
609
658
)
610
659
assert format_for (src , from_suffix = False ) == expected
611
660
612
661
613
662
@pytest .mark .parametrize (
614
663
"fmt,expected" ,
615
- [("png" , "PNG" ), ("jpg" , "JPEG" ), ("gif" , "GIF" ), ("webp" , "WEBP" )],
664
+ [("png" , "PNG" ), ("jpg" , "JPEG" ), ("gif" , "GIF" ), ("webp" , "WEBP" ), ( "svg" , "SVG" ) ],
616
665
)
617
666
def test_format_for_real_images_content_bytes (
618
- png_image , jpg_image , gif_image , webp_image , tmp_path , fmt , expected
667
+ png_image , jpg_image , gif_image , webp_image , svg_image , tmp_path , fmt , expected
619
668
):
620
669
src , _ = get_src_dst (
621
670
tmp_path ,
@@ -624,6 +673,7 @@ def test_format_for_real_images_content_bytes(
624
673
jpg_image = jpg_image ,
625
674
gif_image = gif_image ,
626
675
webp_image = webp_image ,
676
+ svg_image = svg_image ,
627
677
)
628
678
assert format_for (io .BytesIO (src .read_bytes ()), from_suffix = False ) == expected
629
679
@@ -635,6 +685,7 @@ def test_format_for_real_images_content_bytes(
635
685
("image.jpg" , "JPEG" ),
636
686
("image.gif" , "GIF" ),
637
687
("image.webp" , "WEBP" ),
688
+ ("image.svg" , "SVG" ),
638
689
("image.raster" , None ),
639
690
],
640
691
)
0 commit comments