Skip to content

Instantly share code, notes, and snippets.

@alfakini

alfakini/events.py

Last active Aug 7, 2020
Embed
What would you like to do?
Filesystem events monitoring with Python [events.py]
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
You can’t perform that action at this time.