Created
July 16, 2025 03:32
-
-
Save moogoo78/b955c311954b77231081c6c59b8ce5fa to your computer and use it in GitHub Desktop.
python image lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from pathlib import Path | |
| import logging | |
| from PIL import ( | |
| Image, | |
| UnidentifiedImageError, | |
| ) | |
| class MgImage(object): | |
| src_folder = '' | |
| image_list = [] | |
| stats = { | |
| 'num_files': 0, | |
| 'num_image_files': 0, | |
| } | |
| def __init__(self, level=logging.DEBUG): | |
| logging.basicConfig(filename='mg-image.log', level=level) | |
| self.image_list = [] | |
| def walk(self, src_folder=''): | |
| if src_folder == '': | |
| return | |
| else: | |
| self.src_folder = src_folder | |
| for root, folders, files in os.walk(self.src_folder): | |
| for i in files: | |
| self.stats['num_files'] += 1 | |
| path = Path(root, i) | |
| try: | |
| with Image.open(path) as im: | |
| self.stats['num_image_files'] += 1 | |
| self.image_list.append(im) | |
| except UnidentifiedImageError: | |
| #self.stats['not-image'] += 1 | |
| logging.debug(f'not image: {path}') | |
| return self.image_list | |
| def get_stats(self): | |
| return self.stats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment