Last active
December 9, 2023 07:19
-
-
Save JackieMaHatesDogEyes/98f5cbc3b3f144b314bb7447f8941414 to your computer and use it in GitHub Desktop.
Recursively apply an Oil Painting effect to all Images in a provided folder (45 LOC)
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 sys | |
assert sys.version_info >= (3, 5) | |
from tkinter.filedialog import askdirectory | |
from pathlib import Path | |
import cv2 | |
import os | |
import json | |
oil_lvl = 7 | |
if os.path.exists("cfg.json"): | |
cfg = json.load(open("cfg.json", "r")) | |
foldername = cfg["folder"] | |
oil_lvl = cfg["lvl"] | |
folder = askdirectory(initialdir=foldername) | |
else: | |
folder = askdirectory(initialdir=str(Path.home())) | |
if not folder: | |
exit(1) | |
json.dump({"folder": folder, "lvl": oil_lvl}, open("cfg.json", "w")) | |
folder = Path(folder) | |
os.makedirs("out", exist_ok=True) | |
def oilPaint(filename, lvl:int=14): | |
img = cv2.imread(filename) | |
dst = cv2.xphoto.oilPainting(img, lvl, 1) | |
outname = f"{filename}_stylized.png" | |
outname = f"out/" + os.path.basename(outname) | |
cv2.imwrite(outname, dst) | |
print(f"Saved {filename} to {outname}.") | |
def get(path): | |
for file in path.iterdir(): | |
if file.is_dir(): | |
get(file) | |
elif str(file).endswith((".png", ".jpg", ".jpeg", ".exr", ".webp")): | |
oilPaint(str(file), lvl=oil_lvl) | |
get(folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment