Filesystem events monitoring with Python [events.py]
This file contains 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 PIL import Image | |
from PIL.ImageOps import grayscale | |
from watchdog.events import RegexMatchingEventHandler | |
class ImagesEventHandler(RegexMatchingEventHandler): | |
THUMBNAIL_SIZE = (128, 128) | |
IMAGES_REGEX = [r".*[^_thumbnail]\.jpg$"] | |
def __init__(self): | |
super().__init__(self.IMAGES_REGEX) | |
def on_created(self, event): | |
self.process(event) | |
def process(self, event): | |
filename, ext = os.path.splitext(event.src_path) | |
filename = f"{filename}_thumbnail.jpg" | |
image = Image.open(event.src_path) | |
image = grayscale(image) | |
image.thumbnail(self.THUMBNAIL_SIZE) | |
image.save(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment