diff --git a/IMAGES & PHOTO SCRIPTS/Image-Inverter/README.md b/IMAGES & PHOTO SCRIPTS/Image-Inverter/README.md new file mode 100644 index 00000000..2e9f97c1 --- /dev/null +++ b/IMAGES & PHOTO SCRIPTS/Image-Inverter/README.md @@ -0,0 +1,21 @@ +# Image-Inverter + +## Describtion +This project is an implementaion of a CLI program that inverts image/set of images. + +## Requirements +First, you need to install `pillow` module. You can check the official instructions [here](https://pillow.readthedocs.io/en/stable/installation.html) and follow the instructions according to the OS you are running. + +## Examples +### Command +`./inverter.py /your/path/to/image.jpg /another/img.png` + +OR: you can use `-v` or `--verbose`: +`./inverter,py -v /your/image.webp` + +NOTE: **output** images will be generated in the **current working directory** +### Before +![Flower's Normal Image](https://github.com/omar-danasoury/Python-project-Scripts/blob/25c0a96bc74e762bf74b9b33c56372e9d4f23837/IMAGES%20&%20PHOTO%20SCRIPTS/Image-Inverter/before.png) + +### After +![Flower's Inverted Image](https://github.com/omar-danasoury/Python-project-Scripts/blob/25c0a96bc74e762bf74b9b33c56372e9d4f23837/IMAGES%20&%20PHOTO%20SCRIPTS/Image-Inverter/after.png) diff --git a/IMAGES & PHOTO SCRIPTS/Image-Inverter/after.png b/IMAGES & PHOTO SCRIPTS/Image-Inverter/after.png new file mode 100644 index 00000000..008a2cc1 Binary files /dev/null and b/IMAGES & PHOTO SCRIPTS/Image-Inverter/after.png differ diff --git a/IMAGES & PHOTO SCRIPTS/Image-Inverter/before.png b/IMAGES & PHOTO SCRIPTS/Image-Inverter/before.png new file mode 100644 index 00000000..42a38bf2 Binary files /dev/null and b/IMAGES & PHOTO SCRIPTS/Image-Inverter/before.png differ diff --git a/IMAGES & PHOTO SCRIPTS/Image-Inverter/inverter.py b/IMAGES & PHOTO SCRIPTS/Image-Inverter/inverter.py new file mode 100755 index 00000000..6399902a --- /dev/null +++ b/IMAGES & PHOTO SCRIPTS/Image-Inverter/inverter.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +from PIL import Image, ImageOps, UnidentifiedImageError +import sys, os + +def check_input(): + """ Checks if the script is called with no input parameters. """ + if len(sys.argv) == 1: + print("Please provide image files to operate on!") + sys.exit(1) + +def main(): + """ The main function """ + check_input() + + verbose_enabled = False + if ("-v" in sys.argv) or ("--verbose" in sys.argv): + verbose_enabled = True + + i = 0 + for file in sys.argv: + # To ignore the first parameter -> the script call + -v + --verbose + if i == 0 or sys.argv[i] == "-v" or sys.argv[i] == "--verbose": + i = i + 1 + continue + + image_path_no_ext, extension = os.path.splitext(file) + + try: + with Image.open(file) as image: + new_path_with_ext = image_path_no_ext + "_inverted" + extension + ImageOps.invert(image).save(new_path_with_ext) + if verbose_enabled: + print("Successfully inverted " + file + "\n" + new_path_with_ext + " is generated.\n") + except UnidentifiedImageError: + print(file + " is not suppotred, please provide a supported file type.") + i = i + 1 + +if __name__ == '__main__': + main()