Skip to content

Instantly share code, notes, and snippets.

@ceaksan
Last active Jun 26, 2021
Embed
What would you like to do?
#!/usr/bin/env python3
import os
from watchdog.events import FileSystemEventHandler
import time
from watchdog.observers import Observer
from PIL import Image
class convImage(FileSystemEventHandler):
def __init__(self):
self.flToTrack = '/Users/user/Desktop/Old'
self.flDestination = '/Users/user/Desktop/New'
# def on_any_event(self, event):
# print("EVENT")
# print(event.event_type)
# print(event.src_path)
# print()
def on_modified(self, event):
# print(event.event_type)
# print(event.src_path)
for flFullName in os.listdir(self.flToTrack):
fName, fExt = os.path.splitext(flFullName)
if fExt == '.png':
try:
Image.open(flFullName).convert('RGB').save(
f'{self.flDestination}/{fName}.jpg', 'jpeg')
except Exception as err:
print(err)
elif fExt in ['.jpg', '.jpeg']:
try:
Image.open(flFullName).convert('RGB').save(
f'{self.flDestination}/{fName}.png', 'png')
except Exception as err:
print(err)
try:
Image.open(flFullName).convert('RGB').save(
f'{self.flDestination}/{fName}.webp', 'webp')
except Exception as err:
print(err)
src = f'{self.flToTrack}/{flFullName}'
try:
os.rename(src, f'{self.flDestination}/{flFullName}')
except Exception as err:
print(err)
if __name__ == "__main__":
evHandler = convImage()
observer = Observer()
observer.schedule(evHandler, evHandler.flToTrack, recursive=True)
observer.start()
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment