Last active
October 10, 2022 14:34
-
-
Save SahbiOuali13/7ddfccac19ee49b2465c9851c719c4e2 to your computer and use it in GitHub Desktop.
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
from pathlib import Path | |
import multiprocessing | |
import sys | |
from PIL import Image, ImageFilter | |
from PIL import ImageEnhance | |
import PySimpleGUI as sg | |
CURRENT_DIR: Path = Path().cwd() | |
images = CURRENT_DIR.glob("*.jpg") | |
EXPORT_DIR = CURRENT_DIR / "export" | |
EXPORT_DIR.mkdir(exist_ok=True) | |
def enhance_and_export(image_path: Path) -> None: | |
im = Image.open(image_path) | |
# An enhancement factor of 0.0 results in a full black image a | |
# nd an enhancement factor of 1.0 results the same as the original image. | |
curr_bright = ImageEnhance.Brightness(im) | |
new_bright = 1.1 | |
img_brightened = curr_bright.enhance(new_bright) | |
# An enhancement factor of 0.0 results in a full black | |
# and white image and an enhancement factor of | |
# 1.0 results the same as the original image. | |
curr_color = ImageEnhance.Color(img_brightened) | |
new_color = 2.5 | |
img_colored = curr_color.enhance(new_color) | |
# An enhancement factor of 0.0 results in a full grey image | |
# and an enhancement factor of 1.0 results the same as the original image. | |
curr_contrast = ImageEnhance.Contrast(img_colored) | |
new_contrast = 0.3 | |
img_contrasted = curr_contrast.enhance(new_contrast) | |
# An enhancement factor of 0.0 results in a blurred image | |
# and an enhancement factor of 1.0 results in the same as | |
# the original image and a factor > 1.0 results in a sharpened image. | |
curr_sharp = ImageEnhance.Sharpness(img_colored) # result | |
new_sharp = 8.3 | |
image_sharpened = curr_sharp.enhance(new_sharp) | |
image_sharpened.save(EXPORT_DIR / image_path.name) | |
def concurrent_picture_enhance(list_of_images: list) -> None: | |
print("\n Processing pictures\n..........................................") | |
processed_images = [] | |
pool = multiprocessing.Pool(processes=16) | |
for index, element in enumerate(pool.imap(enhance_and_export, list_of_images)): | |
processed_images.append(element) | |
sys.stderr.write(f'\rdone {index / len(list_of_images) * 100:.0f}%') | |
sg.theme('Dark') | |
sg.one_line_progress_meter( | |
'Flashy mode', | |
index + 1, len(list_of_images), | |
'-key-', orientation="h", | |
bar_color=('orange', 'black'), | |
size=(40, 20)) | |
if __name__ == "__main__": | |
concurrent_picture_enhance(list_of_images=list(images)) |
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
from pathlib import Path | |
from PIL import Image, ImageFilter | |
from PIL import ImageEnhance | |
CURRENT_DIR = Path().cwd() | |
images = CURRENT_DIR.glob("*.jpg") | |
EXPORT_DIR = CURRENT_DIR / "export" | |
EXPORT_DIR.mkdir(exist_ok=True) | |
def enhance_and_export(image_path): | |
im = Image.open(image_path) | |
# An enhancement factor of 0.0 results in a full black image a | |
# nd an enhancement factor of 1.0 results the same as the original image. | |
curr_bright = ImageEnhance.Brightness(im) | |
new_bright = 1.1 | |
img_brightened = curr_bright.enhance(new_bright) | |
# An enhancement factor of 0.0 results in a full black and white image and an enhancement factor of 1.0 results the same as the original image. | |
curr_color = ImageEnhance.Color(img_brightened) | |
new_color = 2.5 | |
img_colored = curr_color.enhance(new_color) | |
# An enhancement factor of 0.0 results in a full grey image and an enhancement factor of 1.0 results the same as the original image. | |
curr_contrast = ImageEnhance.Contrast(img_colored) | |
new_contrast = 0.3 | |
img_contrasted = curr_contrast.enhance(new_contrast) | |
# An enhancement factor of 0.0 results in a blurred image and an enhancement factor of 1.0 results in the same as the original image and a factor > 1.0 results in a sharpened image. | |
curr_sharp = ImageEnhance.Sharpness(img_colored) # result | |
new_sharp = 8.3 | |
image_sharpened = curr_sharp.enhance(new_sharp) | |
image_sharpened.save(EXPORT_DIR / image_path.name) | |
for image in images: | |
enhance_and_export(image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment