Skip to content

Instantly share code, notes, and snippets.

@alfakini
Last active August 15, 2021 05:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alfakini/7b2ae9f65ab51884e7db7906bd4eccf8 to your computer and use it in GitHub Desktop.
Save alfakini/7b2ae9f65ab51884e7db7906bd4eccf8 to your computer and use it in GitHub Desktop.
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