Skip to content

Commit a92df37

Browse files
committed
Check if SVG is valid
1 parent 6d0f2c0 commit a92df37

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

scripts/resize_image.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from PIL import Image
22
import os
3+
import xml.etree.ElementTree as ET
34

45

56
def resize_image(image_filename, max_height=120):
@@ -33,9 +34,6 @@ def resize_image(image_filename, max_height=120):
3334
optimize=True,
3435
quality=85
3536
)
36-
print(f'Resized and optimized: {image_filename}')
37-
else:
38-
print(f'No resizing needed for: {image_filename}')
3937
else:
4038
print(f'File not found: {image_filename}')
4139

@@ -49,10 +47,8 @@ def convert_to_webp(image_filename, replace=False):
4947
supported_formats = ['.png', '.jpg', '.jpeg', '.tiff']
5048
image_ext = os.path.splitext(image_filename)[1].lower()
5149
if image_ext not in supported_formats:
52-
print(f'Unsupported format: {image_filename}')
5350
return image_filename
5451
if os.path.exists(image_filename):
55-
print(f'Processing: {image_filename}')
5652
with Image.open(image_filename) as img:
5753
# Determine the file format
5854
file_format = image_filename.split('.')[-1].upper()
@@ -67,7 +63,6 @@ def convert_to_webp(image_filename, replace=False):
6763
)
6864
if replace:
6965
os.remove(image_filename)
70-
print(f'Converted to webp: {image_filename}')
7166
return webp_filename
7267
else:
7368
print(f'File not found: {image_filename}')
@@ -85,4 +80,16 @@ def is_valid_image(image_filename):
8580
img.verify()
8681
return True
8782
except Exception as e:
88-
print(f'Invalid image: {image_filename}')
83+
print(f'Invalid image: {image_filename}')
84+
85+
def is_valid_svg(svg_filename):
86+
"""
87+
Check if the svg file is valid.
88+
param svg_filename: The svg file to check
89+
return: True if the svg is valid, False otherwise
90+
"""
91+
try:
92+
ET.parse(svg_filename) # Try to parse the XML
93+
return True # No error means it's valid
94+
except ET.ParseError:
95+
return False # If parsing fails, it's invalid

0 commit comments

Comments
 (0)