import random from pathlib import Path import PIL 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: """ try: img = Image.open(file_path) return img.format == 'PNG' except PIL.UnidentifiedImageError as e: # tqdm.write(str(e)) return False