wmts-exfiltrator/pkg/image.py

27 lines
585 B
Python
Raw Normal View History

2023-11-02 23:35:43 -06:00
import random
from pathlib import Path
2023-11-06 17:48:09 -07:00
import PIL
2023-11-02 23:35:43 -06:00
from PIL import Image
def random_file_width(base_path: Path):
random_tile_file = random.choice(list(base_path.glob('*')))
with Image.open(random_tile_file) as img:
return img.size[0]
def is_png(file_path):
"""
Make sure an image is a valid PNG.
Raise exeption if could not load image.
:param file_path:
:return:
"""
2023-11-04 20:16:37 -06:00
try:
img = Image.open(file_path)
return img.format == 'PNG'
2023-11-06 17:48:09 -07:00
except PIL.UnidentifiedImageError as e:
# tqdm.write(str(e))
2023-11-04 20:16:37 -06:00
return False