diff --git a/01_pillow_basics/image_viewer.py b/01_pillow_basics/image_viewer.py index 677ff53..111db2c 100644 --- a/01_pillow_basics/image_viewer.py +++ b/01_pillow_basics/image_viewer.py @@ -11,7 +11,7 @@ def main(): layout = [ - [sg.Image(key="-IMAGE-")], + [sg.Image(key="-IMAGE-", size=(400,400))], [ sg.Text("Image File"), sg.Input(size=(25, 1), key="-FILE-"), @@ -33,7 +33,7 @@ def main(): image.thumbnail((400, 400)) bio = io.BytesIO() image.save(bio, format="PNG") - window["-IMAGE-"].update(data=bio.getvalue()) + window["-IMAGE-"].update(data=bio.getvalue(), size=(400,400)) window.close() diff --git a/01_pillow_basics/open_image.py b/01_pillow_basics/open_image.py index 668c9ca..3dd745e 100644 --- a/01_pillow_basics/open_image.py +++ b/01_pillow_basics/open_image.py @@ -3,4 +3,4 @@ from PIL import Image image = Image.open("flowers.jpg") -image.show("flowers") +image.show("flowers") \ No newline at end of file diff --git a/01_pillow_basics/open_image_context.py b/01_pillow_basics/open_image_context.py index 88bfe4f..4b43637 100644 --- a/01_pillow_basics/open_image_context.py +++ b/01_pillow_basics/open_image_context.py @@ -3,4 +3,4 @@ from PIL import Image with Image.open("flowers.jpg") as image: - image.show("flowers") + image.show("flowers") \ No newline at end of file diff --git a/01_pillow_basics/open_image_from_memory.py b/01_pillow_basics/open_image_from_memory.py index 55dcca1..db728a7 100644 --- a/01_pillow_basics/open_image_from_memory.py +++ b/01_pillow_basics/open_image_from_memory.py @@ -1,4 +1,4 @@ -# open_image_from_memory_2.py +# open_image_from_memory.py import io import urllib.request diff --git a/01_pillow_basics/open_image_from_tar.py b/01_pillow_basics/open_image_from_tar.py index c30ff6e..ee1959f 100644 --- a/01_pillow_basics/open_image_from_tar.py +++ b/01_pillow_basics/open_image_from_tar.py @@ -4,4 +4,4 @@ fobj = TarIO.TarIO("flowers.tar", "flowers.jpg") image = Image.open(fobj) -image.show() +image.show() \ No newline at end of file diff --git a/01_pillow_basics/save_image.py b/01_pillow_basics/save_image.py index ec5f801..96b0d0b 100644 --- a/01_pillow_basics/save_image.py +++ b/01_pillow_basics/save_image.py @@ -10,8 +10,8 @@ def image_converter(input_file_path, output_file_path): image.save(output_file_path) original_suffix = pathlib.Path(input_file_path).suffix new_suffix = pathlib.Path(output_file_path).suffix - print(f"Converting {input_file_path} from {original_suffix} " f"to {new_suffix}") - + print(f"Converting {input_file_path} from {original_suffix} " + f"to {new_suffix}") if __name__ == "__main__": - image_converter("flowers.jpg", "flowers.png") + image_converter("flowers.jpg", "flowers.png") \ No newline at end of file diff --git a/01_pillow_basics/save_image_with_compression.py b/01_pillow_basics/save_image_with_compression.py index d8f5dce..f3ef3ea 100644 --- a/01_pillow_basics/save_image_with_compression.py +++ b/01_pillow_basics/save_image_with_compression.py @@ -12,4 +12,4 @@ def image_quality(input_file_path, output_file_path, quality): if __name__ == "__main__": image_quality("blue_flowers.jpg", "blue_flowers_compressed.jpg", - quality=95) + quality=95) \ No newline at end of file diff --git a/01_pillow_basics/save_image_with_new_dpi.py b/01_pillow_basics/save_image_with_new_dpi.py index 8236b6f..0ee52e4 100644 --- a/01_pillow_basics/save_image_with_new_dpi.py +++ b/01_pillow_basics/save_image_with_new_dpi.py @@ -11,4 +11,4 @@ def image_converter(input_file_path, output_file_path, dpi): if __name__ == "__main__": image_converter("blue_flowers.jpg", "blue_flowers_dpi.jpg", - dpi=(72, 72)) + dpi=(72, 72)) \ No newline at end of file diff --git a/01_pillow_basics/thumbnail_maker.py b/01_pillow_basics/thumbnail_maker.py index 03639b9..05f9abd 100644 --- a/01_pillow_basics/thumbnail_maker.py +++ b/01_pillow_basics/thumbnail_maker.py @@ -3,13 +3,11 @@ from PIL import Image -def create_thumbnail(input_file_path, thumbnail_path, - thumbnail_size): +def create_thumbnail(input_file_path, thumbnail_path, thumbnail_size): with Image.open(input_file_path) as image: image.thumbnail(thumbnail_size) - image.save(thumbnail_path, "JPEG") + image.save(thumbnail_path, format="JPEG") if __name__ == "__main__": - create_thumbnail("flowers.jpg", - "flowers.thumbnail", (128, 128)) + create_thumbnail("flowers.jpg", "flowers.thumbnail", (128, 128)) \ No newline at end of file diff --git a/02_colors/color_test.py b/02_colors/color_test.py index 7a381f2..55be1f9 100644 --- a/02_colors/color_test.py +++ b/02_colors/color_test.py @@ -9,4 +9,4 @@ def get_rgb_value(color_name): if __name__ == "__main__": for color in ImageColor.colormap: - print(f"{color} = {get_rgb_value(color)}") + print(f"{color} = {get_rgb_value(color)}") \ No newline at end of file diff --git a/02_colors/create_4_color.py b/02_colors/create_4_color.py index 13baa03..7bf5721 100644 --- a/02_colors/create_4_color.py +++ b/02_colors/create_4_color.py @@ -10,4 +10,4 @@ def four_color(input_image_path, output_image_path): if __name__ == "__main__": - four_color("monarch_caterpillar.jpg", "four_color_caterpillar.png") + four_color("monarch_caterpillar.jpg", "four_color_caterpillar.png") \ No newline at end of file diff --git a/02_colors/create_bw.py b/02_colors/create_bw.py index b56254f..bc02ebb 100644 --- a/02_colors/create_bw.py +++ b/02_colors/create_bw.py @@ -10,4 +10,4 @@ def black_and_white(input_image_path, output_image_path): if __name__ == "__main__": - black_and_white("monarch_caterpillar.jpg", "bw_caterpillar.jpg") + black_and_white("monarch_caterpillar.jpg", "bw_caterpillar.jpg") \ No newline at end of file diff --git a/02_colors/create_bw_dithering.py b/02_colors/create_bw_dithering.py index 6d15073..3d374bb 100644 --- a/02_colors/create_bw_dithering.py +++ b/02_colors/create_bw_dithering.py @@ -10,4 +10,4 @@ def black_and_white(input_image_path, output_image_path): if __name__ == "__main__": - black_and_white("monarch_caterpillar.jpg", "dither_caterpillar.jpg") + black_and_white("monarch_caterpillar.jpg", "dither_caterpillar.jpg") \ No newline at end of file diff --git a/02_colors/create_grayscale.py b/02_colors/create_grayscale.py index 107bc9a..9001788 100644 --- a/02_colors/create_grayscale.py +++ b/02_colors/create_grayscale.py @@ -10,4 +10,4 @@ def grayscale(input_image_path, output_image_path): if __name__ == "__main__": - grayscale("monarch_caterpillar.jpg", "gray_caterpillar.jpg") + grayscale("monarch_caterpillar.jpg", "gray_caterpillar.jpg") \ No newline at end of file diff --git a/02_colors/create_image.py b/02_colors/create_image.py index 6824ae5..b45ba3b 100644 --- a/02_colors/create_image.py +++ b/02_colors/create_image.py @@ -25,4 +25,4 @@ def create_image(path, size): if __name__ == "__main__": - create_image("lines.png", (150, 150)) + create_image("lines.png", (150, 150)) \ No newline at end of file diff --git a/02_colors/create_sepia.py b/02_colors/create_sepia.py index 7b39353..a4b5982 100644 --- a/02_colors/create_sepia.py +++ b/02_colors/create_sepia.py @@ -34,4 +34,4 @@ def create_sepia(input_image_path, output_image_path): if __name__ == "__main__": - create_sepia("monarch_caterpillar.jpg", "sepia_caterpillar.jpg") + create_sepia("monarch_caterpillar.jpg", "sepia_caterpillar.jpg") \ No newline at end of file diff --git a/02_colors/image_colors.py b/02_colors/image_colors.py index 5304ec2..da010f7 100644 --- a/02_colors/image_colors.py +++ b/02_colors/image_colors.py @@ -11,4 +11,4 @@ def get_image_colors(image_path): if __name__ == "__main__": - print(get_image_colors("cape_thick_knee.jpg")) + print(get_image_colors("cape_thick_knee.jpg")) \ No newline at end of file diff --git a/02_colors/image_converter.py b/02_colors/image_converter.py index 03d591e..10873be 100644 --- a/02_colors/image_converter.py +++ b/02_colors/image_converter.py @@ -20,13 +20,13 @@ "Black and White": black_and_white, "Grayscale": grayscale, "Sepia": sepia, -} + } def main(): effect_names = list(effects.keys()) layout = [ - [sg.Image(key="-IMAGE-", size=(400, 400))], + [sg.Image(key="-IMAGE-", size=(400,400))], [ sg.Text("Image File"), sg.Input(size=(25, 1), key="-FILENAME-"), @@ -38,7 +38,7 @@ def main(): sg.Combo( effect_names, default_value="Normal", key="-EFFECTS-", enable_events=True, readonly=True - ), + ), ], [sg.Button("Save")], ] @@ -52,13 +52,13 @@ def main(): if event in ["Load Image", "-EFFECTS-"]: selected_effect = values["-EFFECTS-"] image_file = values["-FILENAME-"] - if os.path.exists(image_file): + if image_file: effects[selected_effect](image_file, tmp_file) image = Image.open(tmp_file) image.thumbnail((400, 400)) bio = io.BytesIO() image.save(bio, format="PNG") - window["-IMAGE-"].update(data=bio.getvalue()) + window["-IMAGE-"].update(data=bio.getvalue(), size=(400,400)) if event == "Save" and values["-FILENAME-"]: save_filename = sg.popup_get_file( "File", file_types=file_types, save_as=True, no_window=True @@ -75,4 +75,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/03_metadata/exif_getter.py b/03_metadata/exif_getter.py index 2f6fa79..e6f6613 100644 --- a/03_metadata/exif_getter.py +++ b/03_metadata/exif_getter.py @@ -16,4 +16,4 @@ def get_exif(image_file_path): if __name__ == "__main__": exif = get_exif("bridge.JPG") - print(exif) + print(exif) \ No newline at end of file diff --git a/03_metadata/exif_viewer.py b/03_metadata/exif_viewer.py index 8cc00c8..38735e5 100644 --- a/03_metadata/exif_viewer.py +++ b/03_metadata/exif_viewer.py @@ -6,25 +6,27 @@ from PIL import Image from PIL.ExifTags import TAGS -file_types = [("(JPEG (*.jpg)", "*.jpg"), - ("All files (*.*)", "*.*")] +file_types = [ + ("(JPEG (*.jpg)", "*.jpg"), + ("All files (*.*)", "*.*"), + ] fields = { - "File name": "File name", - "File size": "File size", - "Model": "Camera Model", - "ExifImageWidth": "Width", - "ExifImageHeight": "Height", - "DateTime": "Creation Date", - "static_line": "*", - "MaxApertureValue": "Aperture", - "ExposureTime": "Exposure", - "FNumber": "F-Stop", - "Flash": "Flash", - "FocalLength": "Focal Length", - "ISOSpeedRatings": "ISO", - "ShutterSpeedValue": "Shutter Speed", -} + "File name": "File name", + "File size": "File size", + "Model": "Camera Model", + "ExifImageWidth": "Width", + "ExifImageHeight": "Height", + "DateTime": "Creation Date", + "static_line": "*", + "MaxApertureValue": "Aperture", + "ExposureTime": "Exposure", + "FNumber": "F-Stop", + "Flash": "Flash", + "FocalLength": "Focal Length", + "ISOSpeedRatings": "ISO", + "ShutterSpeedValue": "Shutter Speed", + } def get_exif_data(path): @@ -49,19 +51,17 @@ def get_exif_data(path): def main(): - layout = [ - [ + layout = [[ sg.FileBrowse( "Load image data", file_types=file_types, key="-LOAD-", - enable_events=True - ) - ] - ] + enable_events=True, + ) + ]] for field in fields: - layout += [ - [sg.Text(fields[field], size=(10, 1)), - sg.Text("", size=(25, 1), key=field)] - ] + layout += [[ + sg.Text(fields[field], size=(10, 1)), + sg.Text("", size=(25, 1), key=field), + ]] window = sg.Window("Image information", layout) while True: @@ -81,4 +81,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/03_metadata/gps_exif_getter.py b/03_metadata/gps_exif_getter.py index 3e039d0..26b1721 100644 --- a/03_metadata/gps_exif_getter.py +++ b/03_metadata/gps_exif_getter.py @@ -25,4 +25,4 @@ def get_exif(image_file_path): if __name__ == "__main__": exif = get_exif("jester.jpg") - print(exif) + print(exif) \ No newline at end of file diff --git a/03_metadata/tiff_metadata.py b/03_metadata/tiff_metadata.py index c61d831..3d7830e 100644 --- a/03_metadata/tiff_metadata.py +++ b/03_metadata/tiff_metadata.py @@ -14,4 +14,4 @@ def get_metadata(image_file_path): if __name__ == "__main__": metadata = get_metadata("reportlab_cover.tiff") - print(metadata) + print(metadata) \ No newline at end of file diff --git a/04_filters/blur_image.py b/04_filters/blur_image.py index 3b8ca52..8d23b8a 100644 --- a/04_filters/blur_image.py +++ b/04_filters/blur_image.py @@ -9,6 +9,5 @@ def blur(input_image, output_image): filtered_image = image.filter(ImageFilter.BLUR) filtered_image.save(output_image) - if __name__ == "__main__": - blur("butterfly.jpg", "butterfly_blurred.jpg") + blur("butterfly.jpg", "butterfly_blurred.jpg") \ No newline at end of file diff --git a/04_filters/boxblur_image.py b/04_filters/boxblur_image.py index 6823a66..3461e35 100644 --- a/04_filters/boxblur_image.py +++ b/04_filters/boxblur_image.py @@ -11,4 +11,4 @@ def boxblur(input_image, output_image): if __name__ == "__main__": - boxblur("trex.jpg", "trex_boxblur.jpg") + boxblur("trex.jpg", "trex_boxblur.jpg") \ No newline at end of file diff --git a/04_filters/color_lut_image.py b/04_filters/color_lut_image.py new file mode 100644 index 0000000..85e6d29 --- /dev/null +++ b/04_filters/color_lut_image.py @@ -0,0 +1,14 @@ +# color_lut_image.py + +from PIL import Image +from PIL import ImageFilter + + +def color_lut(input_image, output_image): + image = Image.open(input_image) + filtered_image = image.filter(ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)) + filtered_image.save(output_image) + + +if __name__ == "__main__": + color_lut("trex.jpg", "trex_lut.jpg") \ No newline at end of file diff --git a/04_filters/contour_image.py b/04_filters/contour_image.py index 0533063..568d0a7 100644 --- a/04_filters/contour_image.py +++ b/04_filters/contour_image.py @@ -9,6 +9,5 @@ def contour(input_image, output_image): filtered_image = image.filter(ImageFilter.CONTOUR) filtered_image.save(output_image) - if __name__ == "__main__": - contour("flowers_dallas.jpg", "flowers_contour.jpg") + contour("flowers_dallas.jpg", "flowers_contour.jpg") \ No newline at end of file diff --git a/04_filters/detail_image.py b/04_filters/detail_image.py index bd3956c..616f428 100644 --- a/04_filters/detail_image.py +++ b/04_filters/detail_image.py @@ -9,6 +9,5 @@ def detail(input_image, output_image): filtered_image = image.filter(ImageFilter.DETAIL) filtered_image.save(output_image) - if __name__ == "__main__": - detail("butterfly.jpg", "detailed_butterfly.jpg") + detail("butterfly.jpg", "detailed_butterfly.jpg") \ No newline at end of file diff --git a/04_filters/edge_enhance_image.py b/04_filters/edge_enhance_image.py index efb0f73..f3232c6 100644 --- a/04_filters/edge_enhance_image.py +++ b/04_filters/edge_enhance_image.py @@ -9,6 +9,5 @@ def edge_enhance(input_image, output_image): filtered_image = image.filter(ImageFilter.EDGE_ENHANCE) filtered_image.save(output_image) - if __name__ == "__main__": - edge_enhance("cactus.jpg", "cactus_edge.jpg") + edge_enhance("cactus.jpg", "cactus_edge.jpg") \ No newline at end of file diff --git a/04_filters/emboss_image.py b/04_filters/emboss_image.py index 84e37e0..eaa1dce 100644 --- a/04_filters/emboss_image.py +++ b/04_filters/emboss_image.py @@ -11,4 +11,4 @@ def emboss(input_image, output_image): if __name__ == "__main__": - emboss("hummingbird.jpg", "hummingbird_emboss.jpg") + emboss("hummingbird.jpg", "hummingbird_emboss.jpg") \ No newline at end of file diff --git a/04_filters/find_edges_image.py b/04_filters/find_edges_image.py index 7c01d0d..72528b5 100644 --- a/04_filters/find_edges_image.py +++ b/04_filters/find_edges_image.py @@ -11,4 +11,4 @@ def find_edges(input_image, output_image): if __name__ == "__main__": - find_edges("buffalo.jpg", "buffalo_edges.jpg") + find_edges("buffalo.jpg", "buffalo_edges.jpg") \ No newline at end of file diff --git a/04_filters/gaussian_blur_image.py b/04_filters/gaussian_blur_image.py index 89c19ed..25ffa22 100644 --- a/04_filters/gaussian_blur_image.py +++ b/04_filters/gaussian_blur_image.py @@ -11,4 +11,4 @@ def gaussian_blur(input_image, output_image): if __name__ == "__main__": - gaussian_blur("trex.jpg", "trex_gauss.jpg") + gaussian_blur("trex.jpg", "trex_gauss.jpg") \ No newline at end of file diff --git a/04_filters/image_filter_gui.py b/04_filters/image_filter_gui.py index 784a5cf..49db35f 100644 --- a/04_filters/image_filter_gui.py +++ b/04_filters/image_filter_gui.py @@ -12,80 +12,4 @@ from edge_enhance_image import edge_enhance from emboss_image import emboss from find_edges_image import find_edges -from PIL import Image - -file_types = [("JPEG (*.jpg)", "*.jpg"), ("All files (*.*)", "*.*")] - -tmp_file = tempfile.NamedTemporaryFile(suffix=".jpg").name - -effects = { - "Normal": shutil.copy, - "Blur": blur, - "Contour": contour, - "Detail": detail, - "Edge Enhance": edge_enhance, - "Emboss": emboss, - "Find Edges": find_edges, -} - -def apply_effect(values, window): - selected_effect = values["-EFFECTS-"] - image_file = values["-FILENAME-"] - if os.path.exists(image_file): - effects[selected_effect](image_file, tmp_file) - image = Image.open(tmp_file) - image.thumbnail((400, 400)) - bio = io.BytesIO() - image.save(bio, format="PNG") - window["-IMAGE-"].update(data=bio.getvalue()) - - -def save_image(values): - save_filename = sg.popup_get_file( - "File", file_types=file_types, save_as=True, no_window=True - ) - if save_filename == values["-FILENAME-"]: - sg.popup_error( - "You are not allowed to overwrite the original image!") - else: - if save_filename: - shutil.copy(tmp_file, save_filename) - sg.popup(f"Saved: {save_filename}") - - -def main(): - effect_names = list(effects.keys()) - layout = [ - [sg.Image(key="-IMAGE-", size=(400, 400))], - [ - sg.Text("Image File"), - sg.Input(size=(25, 1), key="-FILENAME-"), - sg.FileBrowse(file_types=file_types), - sg.Button("Load Image") - ], - [ - sg.Text("Effect"), - sg.Combo( - effect_names, default_value="Normal", key="-EFFECTS-", - enable_events=True, readonly=True - ), - ], - [sg.Button("Save")], - ] - - window = sg.Window("Image Filter App", layout, size=(450, 500)) - - while True: - event, values = window.read() - if event == "Exit" or event == sg.WIN_CLOSED: - break - if event in ["Load Image", "-EFFECTS-"]: - apply_effect(values, window) - if event == "Save" and values["-FILENAME-"]: - save_image(values) - - window.close() - - -if __name__ == "__main__": - main() +from PIL import Image \ No newline at end of file diff --git a/04_filters/minfilter_image.py b/04_filters/minfilter_image.py index ab759c9..b85934c 100644 --- a/04_filters/minfilter_image.py +++ b/04_filters/minfilter_image.py @@ -11,4 +11,4 @@ def minfilter(input_image, output_image): if __name__ == "__main__": - minfilter("giraffe.jpg", "giraffe_minfilter.jpg") + minfilter("giraffe.jpg", "giraffe_minfilter.jpg") \ No newline at end of file diff --git a/04_filters/sharpen_image.py b/04_filters/sharpen_image.py index 4f90069..aa0366e 100644 --- a/04_filters/sharpen_image.py +++ b/04_filters/sharpen_image.py @@ -9,6 +9,5 @@ def sharpen(input_image, output_image): filtered_image = image.filter(ImageFilter.SHARPEN) filtered_image.save(output_image) - if __name__ == "__main__": - sharpen("grasshopper.jpg", "grasshopper_sharpened.jpg") + sharpen("grasshopper.jpg", "grasshopper_sharpened.jpg") \ No newline at end of file diff --git a/04_filters/smooth_image.py b/04_filters/smooth_image.py index 10c60b5..f5584aa 100644 --- a/04_filters/smooth_image.py +++ b/04_filters/smooth_image.py @@ -11,4 +11,4 @@ def smooth(input_image, output_image): if __name__ == "__main__": - smooth("spider.jpg", "spider_smooth_more.jpg") + smooth("spider.jpg", "spider_smooth.jpg") \ No newline at end of file diff --git a/04_filters/unsharp_mask_image.py b/04_filters/unsharp_mask_image.py index 89858c8..84ad48c 100644 --- a/04_filters/unsharp_mask_image.py +++ b/04_filters/unsharp_mask_image.py @@ -11,4 +11,4 @@ def unsharp_mask(input_image, output_image): if __name__ == "__main__": - unsharp_mask("trex.jpg", "trex_unsharp.jpg") + unsharp_mask("trex.jpg", "trex_unsharp.jpg") \ No newline at end of file diff --git a/05_cropping_rotating_resizing/crop_image.py b/05_cropping_rotating_resizing/crop_image.py index a972953..e3e3f78 100644 --- a/05_cropping_rotating_resizing/crop_image.py +++ b/05_cropping_rotating_resizing/crop_image.py @@ -10,6 +10,6 @@ def crop_image(image_path, coords, output_image_path): if __name__ == "__main__": - crop_image("green_mantis.jpeg", - (302, 101, 910, 574), - "cropped_mantis.jpg") + crop_image("green_mantis.jpeg", + (302, 101, 910, 574), + "cropped_mantis.jpg") \ No newline at end of file diff --git a/05_cropping_rotating_resizing/image_rotator_gui.py b/05_cropping_rotating_resizing/image_rotator_gui.py index e382fc7..006a502 100644 --- a/05_cropping_rotating_resizing/image_rotator_gui.py +++ b/05_cropping_rotating_resizing/image_rotator_gui.py @@ -21,7 +21,7 @@ "Rotate 180": None, "Rotate 270": None, "Mirror": mirror, -} + } def apply_rotate(image_file, effect): if effect == "Rotate 90": @@ -44,16 +44,17 @@ def apply_effect(values, window): image.thumbnail((400, 400)) bio = io.BytesIO() image.save(bio, format="PNG") - window["-IMAGE-"].update(data=bio.getvalue()) + window["-IMAGE-"].update(data=bio.getvalue(), size=(400, 400)) def save_image(image_filename): save_filename = sg.popup_get_file( - "File", file_types=file_types, save_as=True, no_window=True - ) + "File", file_types=file_types, save_as=True, no_window=True, + ) if save_filename == image_filename: sg.popup_error( - "You are not allowed to overwrite the original image!") + "You are not allowed to overwrite the original image!", + ) else: if save_filename: shutil.copy(tmp_file, save_filename) @@ -63,7 +64,7 @@ def save_image(image_filename): def main(): effect_names = list(effects.keys()) layout = [ - [sg.Image(key="-IMAGE-", size=(400, 400))], + [sg.Image(key="-IMAGE-", size=(400,400))], [ sg.Text("Image File"), sg.Input(size=(25, 1), key="-FILENAME-"), @@ -74,8 +75,8 @@ def main(): sg.Text("Effect"), sg.Combo( effect_names, default_value="Normal", key="-EFFECTS-", - enable_events=True, readonly=True - ), + enable_events=True, readonly=True, + ), ], [sg.Button("Save")], ] @@ -84,11 +85,11 @@ def main(): while True: event, values = window.read() - image_filename = values["-FILENAME-"] if event == "Exit" or event == sg.WIN_CLOSED: break if event in ["Load Image", "-EFFECTS-"]: apply_effect(values, window) + image_filename = values["-FILENAME-"] if event == "Save" and image_filename: save_image(image_filename) diff --git a/05_cropping_rotating_resizing/mirror_image.py b/05_cropping_rotating_resizing/mirror_image.py index e8efd07..32aa5ee 100644 --- a/05_cropping_rotating_resizing/mirror_image.py +++ b/05_cropping_rotating_resizing/mirror_image.py @@ -5,10 +5,10 @@ def mirror(image_path, output_image_path): image = Image.open(image_path) - rotated_image = image.transpose(Image.FLIP_LEFT_RIGHT) - rotated_image.save(output_image_path) + mirror_image = image.transpose(Image.FLIP_LEFT_RIGHT) + mirror_image.save(output_image_path) if __name__ == "__main__": image = "mantis.jpg" - mirror(image, "mantis_mirrored.jpg") + mirror(image, "mantis_mirrored.jpg") \ No newline at end of file diff --git a/05_cropping_rotating_resizing/resize_image.py b/05_cropping_rotating_resizing/resize_image.py index 279fe3c..94926c1 100644 --- a/05_cropping_rotating_resizing/resize_image.py +++ b/05_cropping_rotating_resizing/resize_image.py @@ -19,4 +19,4 @@ def resize(input_image_path, output_image_path, size): input_image_path="pilot_knob.jpg", output_image_path="pilot_knob_small.jpg", size=(800, 400), - ) + ) \ No newline at end of file diff --git a/05_cropping_rotating_resizing/rotate_image.py b/05_cropping_rotating_resizing/rotate_image.py index fd021cc..cad46ec 100644 --- a/05_cropping_rotating_resizing/rotate_image.py +++ b/05_cropping_rotating_resizing/rotate_image.py @@ -11,4 +11,4 @@ def rotate(image_path, degrees_to_rotate, output_image_path): if __name__ == "__main__": image = "dragonfly.jpg" - rotate(image, 90, "dragonfly_rotated.jpg") + rotate(image, 90, "dragonfly_rotated.jpg") \ No newline at end of file diff --git a/05_cropping_rotating_resizing/scale_image.py b/05_cropping_rotating_resizing/scale_image.py index 3d4a1c6..511c338 100644 --- a/05_cropping_rotating_resizing/scale_image.py +++ b/05_cropping_rotating_resizing/scale_image.py @@ -34,5 +34,5 @@ def scale(input_image_path, scale( input_image_path="pilot_knob.jpg", output_image_path="pilot_knob_scaled.jpg", - width=800 - ) + width=800, + ) \ No newline at end of file diff --git a/06_enhancing/enhance_brightness.py b/06_enhancing/enhance_brightness.py index f8e1216..bcc1330 100644 --- a/06_enhancing/enhance_brightness.py +++ b/06_enhancing/enhance_brightness.py @@ -13,4 +13,4 @@ def enhance_brightness(image_path, enhance_factor, output_path): if __name__ == "__main__": enhance_brightness("silver_falls.jpg", 1.5, - "silver_falls_enhanced.jpg") + "silver_falls_enhanced.jpg") \ No newline at end of file diff --git a/06_enhancing/enhance_color.py b/06_enhancing/enhance_color.py index 11aadcb..efb7856 100644 --- a/06_enhancing/enhance_color.py +++ b/06_enhancing/enhance_color.py @@ -13,4 +13,4 @@ def enhance_color(image_path, enhance_factor, output_path): if __name__ == "__main__": enhance_color("goldenrod_soldier_beetle.jpg", 0.0, - "beetle_color_enhanced.jpg") + "beetle_color_enhanced.jpg") \ No newline at end of file diff --git a/06_enhancing/enhance_contrast.py b/06_enhancing/enhance_contrast.py index d8e17cc..921fa62 100644 --- a/06_enhancing/enhance_contrast.py +++ b/06_enhancing/enhance_contrast.py @@ -13,4 +13,4 @@ def enhance_contrast(image_path, enhance_factor, output_path): if __name__ == "__main__": enhance_contrast("madison_county_bridge.jpg", 2.5, - "madison_county_bridge_enhanced.jpg") + "madison_county_bridge_enhanced.jpg") \ No newline at end of file diff --git a/06_enhancing/enhance_sharpness.py b/06_enhancing/enhance_sharpness.py index 63bafda..f80e164 100644 --- a/06_enhancing/enhance_sharpness.py +++ b/06_enhancing/enhance_sharpness.py @@ -13,4 +13,4 @@ def enhance_sharpness(image_path, enhance_factor, output_path): if __name__ == "__main__": enhance_sharpness("hummingbird.jpg", 2.5, - "hummingbird_sharpened.jpg") + "hummingbird_sharpened.jpg") \ No newline at end of file diff --git a/06_enhancing/image_enhancer_gui.py b/06_enhancing/image_enhancer_gui.py new file mode 100644 index 0000000..e29d183 --- /dev/null +++ b/06_enhancing/image_enhancer_gui.py @@ -0,0 +1,13 @@ +# image_enhancer_gui.py + +import io +import os +import PySimpleGUI as sg +import shutil +import tempfile + +from enhance_brightness import enhance_brightness +from enhance_color import enhance_color +from enhance_contrast import enhance_contrast +from enhance_sharpness import enhance_sharpness +from PIL import Image diff --git a/07_combining/blend_images.py b/07_combining/blend_images.py index f8a09e6..1ebd378 100644 --- a/07_combining/blend_images.py +++ b/07_combining/blend_images.py @@ -13,4 +13,4 @@ def blend(input_image_path, input_image_path_2, output_path, alpha): blended_image.save(output_path) if __name__ == "__main__": - blend("skyline.png", "shell.png", "blended.png", alpha=0.4) + blend("skyline.png", "shell.png", "blended.png", alpha=0.2) \ No newline at end of file diff --git a/07_combining/composite_images.py b/07_combining/composite_images.py index 7b631b8..741eb56 100644 --- a/07_combining/composite_images.py +++ b/07_combining/composite_images.py @@ -3,8 +3,7 @@ from PIL import Image -def composite_image(input_image_path, input_image_path_2, - output_path): +def composite_image(input_image_path, input_image_path_2, output_path): image1 = Image.open(input_image_path) image2 = Image.open(input_image_path_2).resize(image1.size) mask = Image.new("L", image1.size, 120) @@ -13,5 +12,4 @@ def composite_image(input_image_path, input_image_path_2, if __name__ == "__main__": - composite_image("pilot_knob.jpg", "grasshopper.jpg", - "composited.jpg") \ No newline at end of file + composite_image("pilot_knob.jpg", "grasshopper.jpg", "composited.jpg") \ No newline at end of file diff --git a/07_combining/concatenating_images.py b/07_combining/concatenating_images.py index c0ed030..ef1eb03 100644 --- a/07_combining/concatenating_images.py +++ b/07_combining/concatenating_images.py @@ -3,8 +3,9 @@ from PIL import Image -def concatenate_vertically(first_image_path, second_image_path, - output_image_path): +def concatenate_vertically( + first_image_path, second_image_path, output_image_path, + ): image_one = Image.open(first_image_path) image_two = Image.open(second_image_path) height = image_one.height + image_two.height @@ -16,8 +17,9 @@ def concatenate_vertically(first_image_path, second_image_path, new_image.save(output_image_path) -def concatenate_horizontally(first_image_path, second_image_path, - output_image_path): +def concatenate_horizontally( + first_image_path, second_image_path, output_image_path, + ): image_one = Image.open(first_image_path) image_two = Image.open(second_image_path) width = image_one.width + image_two.width @@ -30,10 +32,6 @@ def concatenate_horizontally(first_image_path, second_image_path, if __name__ == "__main__": - coords = (125, 712, 642, 963) concatenate_horizontally("silver_falls.jpg", "silver_falls2.jpg", - "silver_h_combined.jpg") - concatenate_vertically("silver_falls.jpg", - "silver_falls2.jpg", - "silver_v_combined.jpg") + "silver_h_combined.jpg") \ No newline at end of file diff --git a/07_combining/concatenating_mismatched_images.py b/07_combining/concatenating_mismatched_images.py index f8f0a88..c45391a 100644 --- a/07_combining/concatenating_mismatched_images.py +++ b/07_combining/concatenating_mismatched_images.py @@ -3,8 +3,9 @@ from PIL import Image -def concatenate_vertically(first_image_path, second_image_path, - output_image_path): +def concatenate_vertically( + first_image_path, second_image_path, output_image_path, + ): image_one = Image.open(first_image_path) image_two = Image.open(second_image_path) height = image_one.height + image_two.height @@ -17,8 +18,9 @@ def concatenate_vertically(first_image_path, second_image_path, new_image.save(output_image_path) -def concatenate_horizontally(first_image_path, second_image_path, - output_image_path): +def concatenate_horizontally( + first_image_path, second_image_path, output_image_path, + ): image_one = Image.open(first_image_path) image_two = Image.open(second_image_path) width = image_one.width + image_two.width @@ -32,10 +34,9 @@ def concatenate_horizontally(first_image_path, second_image_path, if __name__ == "__main__": - coords = (125, 712, 642, 963) - concatenate_horizontally("hummingbird.jpg", - "silver_falls2.jpg", - "h_combined.jpg") - concatenate_vertically("hummingbird.jpg", - "silver_falls2.jpg", - "v_combined.jpg") + concatenate_horizontally( + "hummingbird.jpg", "silver_falls2.jpg", "h_combined.jpg", + ) + concatenate_vertically( + "hummingbird.jpg", "silver_falls2.jpg", "v_combined.jpg", + ) \ No newline at end of file diff --git a/07_combining/image_paste.py b/07_combining/image_paste.py index 09ab1a5..eb5fddb 100644 --- a/07_combining/image_paste.py +++ b/07_combining/image_paste.py @@ -12,4 +12,4 @@ def paste_demo(image_path, output_path, crop_coords): if __name__ == "__main__": coords = (125, 712, 642, 963) - paste_demo("hummingbird.jpg", "hummingbird_pasted.jpg", coords) + paste_demo("hummingbird.jpg", "hummingbird_pasted.jpg", coords) \ No newline at end of file diff --git a/07_combining/image_tiling.py b/07_combining/image_tiling.py index 30b0c1c..d8f2776 100644 --- a/07_combining/image_tiling.py +++ b/07_combining/image_tiling.py @@ -19,4 +19,4 @@ def image_tiling(image_path, output_path, crop_coords): if __name__ == "__main__": coords = (125, 712, 642, 963) - image_tiling("hummingbird.jpg", "hummingbird_tiled.jpg", coords) + image_tiling("hummingbird.jpg", "hummingbird_tiled.jpg", coords) \ No newline at end of file diff --git a/07_combining/watermark.py b/07_combining/watermark.py index 7e68229..f479c40 100644 --- a/07_combining/watermark.py +++ b/07_combining/watermark.py @@ -3,8 +3,10 @@ from PIL import Image -def watermark(input_image_path, output_image_path, - watermark_image_path, position): +def watermark( + input_image_path, output_image_path, + watermark_image_path, position, + ): base_image = Image.open(input_image_path) watermark_image = Image.open(watermark_image_path) # add watermark to your image @@ -14,4 +16,4 @@ def watermark(input_image_path, output_image_path, if __name__ == "__main__": watermark("hummingbird.jpg", "hummingbird_watermarked.jpg", - "logo.png", position=(0, 0)) + "logo.png", position=(0, 0)) \ No newline at end of file diff --git a/07_combining/watermark_gui.py b/07_combining/watermark_gui.py index bfb5de5..08b78d7 100644 --- a/07_combining/watermark_gui.py +++ b/07_combining/watermark_gui.py @@ -23,23 +23,24 @@ def convert_image(image_path): def create_row(label, key, file_types, save=False): if save: return [ - sg.Text(label), - sg.Input(size=(25, 1), key=key), - sg.FileSaveAs(file_types=file_types) - ] - return [ - sg.Text(label), - sg.Input(size=(25, 1), key=key), - sg.FileBrowse(file_types=file_types), - ] + sg.Text(label), + sg.Input(size=(25, 1), key=key), + sg.FileSaveAs(file_types=file_types), + ] + else: + return [ + sg.Text(label), + sg.Input(size=(25, 1), key=key), + sg.FileBrowse(file_types=file_types), + ] def apply_watermark(original_image, values, position, window): watermark_with_transparency( - original_image, tmp_file, values["-WATERMARK-"], position - ) + original_image, tmp_file, values["-WATERMARK-"], position, + ) photo_img = convert_image(tmp_file) - window["-IMAGE-"].update(data=photo_img) + window["-IMAGE-"].update(data=photo_img, size=(400,400)) def check_for_errors(values): @@ -57,11 +58,12 @@ def check_for_errors(values): def save_image(values): save_filename = sg.popup_get_file( - "File", file_types=file_types, save_as=True, no_window=True - ) + "File", file_types=file_types, save_as=True, no_window=True, + ) if save_filename == values["-FILENAME-"]: sg.popup_error( - "You are not allowed to overwrite the original image!") + "You are not allowed to overwrite the original image!", + ) else: if save_filename: shutil.copy(tmp_file, save_filename) @@ -71,7 +73,7 @@ def save_image(values): def main(): original_image = None layout = [ - [sg.Image(key="-IMAGE-", size=(400, 400))], + [sg.Image(key="-IMAGE-", size=(400,400))], create_row("Image File:", "-FILENAME-", file_types), create_row("Watermark File:", "-WATERMARK-", [("PNG (*.png)", "*.png")]), @@ -85,19 +87,20 @@ def main(): sg.Input("0", size=(5, 1), enable_events=True, key="-WATERMARK-Y-"), ], - [sg.Button("Apply Watermark", enable_events=True), - sg.Button("Save Image", enable_events=True), - ], + [ + sg.Button("Apply Watermark", enable_events=True), + sg.Button("Save Image", enable_events=True), + ], ] - window = sg.Window("Watermark GUI", layout, size=(450, 500)) + window = sg.Window("Watermark GUI", layout, size=(450, 600)) while True: event, values = window.read() if event == "Exit" or event == sg.WIN_CLOSED: break - + watermark_x = values["-WATERMARK-X-"] watermark_y = values["-WATERMARK-Y-"] @@ -105,7 +108,7 @@ def main(): filename = values["-FILENAME-"] if os.path.exists(filename): photo_img = convert_image(filename) - window["-IMAGE-"].update(data=photo_img) + window["-IMAGE-"].update(data=photo_img, size=(400,400)) original_image = filename shutil.copy(original_image, tmp_file) if event in ["-WATERMARK-X-", "-WATERMARK-Y-"]: @@ -118,8 +121,7 @@ def main(): if event == "Apply Watermark": if check_for_errors(values): continue - position = (int(watermark_x), - int(watermark_y)) + position = (int(watermark_x), int(watermark_y)) apply_watermark(original_image, values, position, window) if event == "Save Image" and values["-FILENAME-"]: save_image(values) @@ -128,4 +130,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/07_combining/watermark_transparent.py b/07_combining/watermark_transparent.py index 0bed20b..860d7bf 100644 --- a/07_combining/watermark_transparent.py +++ b/07_combining/watermark_transparent.py @@ -3,8 +3,10 @@ from PIL import Image -def watermark_with_transparency(input_image_path, output_image_path, - watermark_image_path, position): +def watermark_with_transparency( + input_image_path, output_image_path, + watermark_image_path, position, + ): base_image = Image.open(input_image_path) watermark = Image.open(watermark_image_path) width, height = base_image.size @@ -17,5 +19,5 @@ def watermark_with_transparency(input_image_path, output_image_path, if __name__ == "__main__": watermark_with_transparency( "hummingbird.jpg", "hummingbird_watermarked2.jpg", "logo.png", - position=(0, 0) - ) + position=(0, 0), + ) \ No newline at end of file diff --git a/08_drawing/draw_arc.py b/08_drawing/draw_arc.py index ae19410..5759419 100644 --- a/08_drawing/draw_arc.py +++ b/08_drawing/draw_arc.py @@ -8,7 +8,7 @@ def arc(output_path): draw = ImageDraw.Draw(image) draw.arc((25, 50, 175, 200), start=30, end=250, fill="green") - draw.arc((100, 150, 275, 300), start=20, end=100, width=5, + draw.arc((100, 150, 275, 300), start=20, end=100, width=5, fill="yellow") image.save(output_path) diff --git a/08_drawing/draw_chord.py b/08_drawing/draw_chord.py index 5b165ed..1a2c890 100644 --- a/08_drawing/draw_chord.py +++ b/08_drawing/draw_chord.py @@ -1,4 +1,4 @@ -# draw_chard.py +# draw_chord.py from PIL import Image, ImageDraw @@ -8,8 +8,8 @@ def chord(output_path): draw = ImageDraw.Draw(image) draw.chord((25, 50, 175, 200), start=30, end=250, fill="red") - draw.chord((100, 150, 275, 300), start=20, end=100, width=5, fill="yellow", - outline="blue") + draw.chord((100, 150, 275, 300), start=20, end=100, width=5, + fill="yellow", outline="blue") image.save(output_path) diff --git a/08_drawing/draw_line.py b/08_drawing/draw_line.py index 509bfd2..bb0111b 100644 --- a/08_drawing/draw_line.py +++ b/08_drawing/draw_line.py @@ -7,11 +7,11 @@ def line(image_path, output_path): image = Image.open(image_path) draw = ImageDraw.Draw(image) - colors = ["red", "green", "blue", "yellow", - "purple", "orange"] + colors = ["red", "green", "blue", "yellow", "purple", "orange"] for i in range(0, 100, 20): - draw.line((i, 0) + image.size, width=5, fill=random.choice(colors)) + draw.line((i, 0) + image.size, width=5, + fill=random.choice(colors)) image.save(output_path) diff --git a/08_drawing/drawing_gui.py b/08_drawing/drawing_gui.py index d676c07..50b02b1 100644 --- a/08_drawing/drawing_gui.py +++ b/08_drawing/drawing_gui.py @@ -41,19 +41,19 @@ def apply_drawing(values, window): fill=fill_color, width=width, outline=outline_color, - ) + ) elif shape == "Rectangle": draw.rectangle( (begin_x, begin_y, end_x, end_y), fill=fill_color, width=width, outline=outline_color, - ) + ) image.save(tmp_file) bio = io.BytesIO() image.save(bio, format="PNG") - window["-IMAGE-"].update(data=bio.getvalue()) + window["-IMAGE-"].update(data=bio.getvalue(), size=(400,400)) def create_coords_elements(label, begin_x, begin_y, key1, key2): @@ -61,16 +61,17 @@ def create_coords_elements(label, begin_x, begin_y, key1, key2): sg.Text(label), sg.Input(begin_x, size=(5, 1), key=key1, enable_events=True), sg.Input(begin_y, size=(5, 1), key=key2, enable_events=True), - ] + ] def save_image(values): save_filename = sg.popup_get_file( - "File", file_types=file_types, save_as=True, no_window=True - ) + "File", file_types=file_types, save_as=True, no_window=True, + ) if save_filename == values["-FILENAME-"]: sg.popup_error( - "You are not allowed to overwrite the original image!") + "You are not allowed to overwrite the original image!", + ) else: if save_filename: shutil.copy(tmp_file, save_filename) @@ -80,12 +81,12 @@ def save_image(values): def main(): colors = list(ImageColor.colormap.keys()) layout = [ - [sg.Image(key="-IMAGE-", size=(400, 400))], + [sg.Image(key="-IMAGE-", size=(400,400))], [ sg.Text("Image File"), sg.Input( - size=(25, 1), key="-FILENAME-" - ), + size=(25, 1), key="-FILENAME-", + ), sg.FileBrowse(file_types=file_types), sg.Button("Load Image"), ], @@ -97,15 +98,15 @@ def main(): key="-SHAPES-", enable_events=True, readonly=True, - ), + ), ], [ *create_coords_elements( - "Begin Coords", "10", "10", "-BEGIN_X-", "-BEGIN_Y-" - ), + "Begin Coords", "10", "10", "-BEGIN_X-", "-BEGIN_Y-", + ), *create_coords_elements( - "End Coords", "100", "100", "-END_X-", "-END_Y-" - ), + "End Coords", "100", "100", "-END_X-", "-END_Y-", + ), ], [ sg.Text("Fill"), @@ -114,23 +115,23 @@ def main(): default_value=colors[0], key="-FILL_COLOR-", enable_events=True, - readonly=True - ), + readonly=True, + ), sg.Text("Outline"), sg.Combo( colors, default_value=colors[0], key="-OUTLINE_COLOR-", enable_events=True, - readonly=True - ), + readonly=True, + ), sg.Text("Width"), sg.Input("3", size=(5, 1), key="-WIDTH-", enable_events=True), ], [sg.Button("Save")], ] - window = sg.Window("Drawing GUI", layout, size=(450, 500)) + window = sg.Window("Drawing GUI", layout, size=(450, 575)) events = [ "Load Image", diff --git a/09_drawing_text/center_text.py b/09_drawing_text/center_text.py index 90ef6a1..3bb9a38 100644 --- a/09_drawing_text/center_text.py +++ b/09_drawing_text/center_text.py @@ -13,7 +13,7 @@ def center(output_path): new_width = (width - font_width) / 2 new_height = (height - font_height) / 2 - draw.text((new_width, new_height), text, fill="black") + draw.text((new_width, new_height), text, font=font, fill="black") image.save(output_path) if __name__ == "__main__": diff --git a/09_drawing_text/centered_text.jpg b/09_drawing_text/centered_text.jpg new file mode 100644 index 0000000..244b26b Binary files /dev/null and b/09_drawing_text/centered_text.jpg differ diff --git a/09_drawing_text/create_anchor.py b/09_drawing_text/create_anchor.py index 67b98f4..bf77c53 100644 --- a/09_drawing_text/create_anchor.py +++ b/09_drawing_text/create_anchor.py @@ -4,13 +4,12 @@ def anchor(xy=(100, 100), anchor="la"): - font = ImageFont.truetype("Gidole-Regular.ttf", 22) + font = ImageFont.truetype("Gidole-Regular.ttf", 32) image = Image.new("RGB", (200, 200), "white") draw = ImageDraw.Draw(image) draw.line(((0, 100), (200, 100)), "gray") draw.line(((100, 0), (100, 200)), "gray") - draw.text((100, 100), f"{anchor} Example", fill="black", - anchor=anchor, font=font) + draw.text((100, 100), "Python", fill="black", anchor=anchor, font=font) image.save(f"anchor_{anchor}.jpg") if __name__ == "__main__": diff --git a/09_drawing_text/draw_text.py b/09_drawing_text/draw_text.py index 508d409..6fd44dc 100644 --- a/09_drawing_text/draw_text.py +++ b/09_drawing_text/draw_text.py @@ -7,7 +7,7 @@ def text(output_path): image = Image.new("RGB", (200, 200), "green") draw = ImageDraw.Draw(image) draw.text((10, 10), "Hello from") - draw.text((10, 25), "Pillow") + draw.text((10, 25), "Pillow",) image.save(output_path) if __name__ == "__main__": diff --git a/09_drawing_text/draw_truetype.py b/09_drawing_text/draw_truetype.py index 116c4af..2315321 100644 --- a/09_drawing_text/draw_truetype.py +++ b/09_drawing_text/draw_truetype.py @@ -9,7 +9,7 @@ def text(input_image_path, output_path): y = 10 for font_size in range(12, 75, 10): font = ImageFont.truetype("Gidole-Regular.ttf", size=font_size) - draw.text((10, y), f"Chihuly Exhibit ({font_size=})", font=font) + draw.text((10, y), f"Chihuly Exhibit ({font_size=}", font=font) y += 35 image.save(output_path) diff --git a/09_drawing_text/text_alignment.py b/09_drawing_text/text_alignment.py index cc1066f..3bb43b5 100644 --- a/09_drawing_text/text_alignment.py +++ b/09_drawing_text/text_alignment.py @@ -10,7 +10,7 @@ def alignment(output_path): y = 10 font = ImageFont.truetype("Gidole-Regular.ttf", size=12) for alignment in alignments: - draw.text((10, y), f"Hello from\n Pillow", font=font, + draw.text((10, y), f"Hello from\nPillow", font=font, align=alignment, fill="black") y += 35 image.save(output_path) diff --git a/09_drawing_text/text_gui.py b/09_drawing_text/text_gui.py index bea3af2..b1f0e9a 100644 --- a/09_drawing_text/text_gui.py +++ b/09_drawing_text/text_gui.py @@ -24,6 +24,7 @@ def get_value(key, values): def apply_text(values, window): + global ttf_files image_file = values["-FILENAME-"] font_name = values["-TTF-"] font_size = get_value("-FONT_SIZE-", values) @@ -41,13 +42,14 @@ def apply_text(values, window): if font_name == "Default Font": font = None else: - font = ImageFont.truetype(font_name, size=font_size) + font = ImageFont.truetype( + ttf_files[font_name], size=font_size) draw.text((x, y), text=text, font=font, fill=color) image.save(tmp_file) bio = io.BytesIO() image.save(bio, format="PNG") - window["-IMAGE-"].update(data=bio.getvalue()) + window["-IMAGE-"].update(data=bio.getvalue(), size=(400,400)) def create_row(label, key, file_types): @@ -55,7 +57,7 @@ def create_row(label, key, file_types): sg.Text(label), sg.Input(size=(25, 1), key=key), sg.FileBrowse(file_types=file_types), - ] + ] def get_ttf_files(directory=None): @@ -73,11 +75,12 @@ def get_ttf_files(directory=None): def save_image(values): save_filename = sg.popup_get_file( - "File", file_types=file_types, save_as=True, no_window=True - ) + "File", file_types=file_types, save_as=True, no_window=True, + ) if save_filename == values["-FILENAME-"]: sg.popup_error( - "You are not allowed to overwrite the original image!") + "You are not allowed to overwrite the original image!", + ) else: if save_filename: shutil.copy(tmp_file, save_filename) @@ -85,12 +88,12 @@ def save_image(values): def update_ttf_values(window): + global ttf_files directory = sg.popup_get_folder("Get TTF Directory") if directory is not None: ttf_files = get_ttf_files(directory) new_values = list(ttf_files.keys()) - window["-TTF-"].update(values=new_values, - value=new_values[0]) + window["-TTF-"].update(values=new_values, value=new_values[0]) def main(): @@ -102,7 +105,7 @@ def main(): elements = [ [sg.Menu(menu_items)], - [sg.Image(key="-IMAGE-", size=(400, 400))], + [sg.Image(key="-IMAGE-", size=(400,400))], create_row("Image File:", "-FILENAME-", file_types), [sg.Button("Load Image")], [sg.Text("Text:"), sg.Input(key="-TEXT-", enable_events=True)], @@ -127,7 +130,7 @@ def main(): [sg.Button("Save Image")], ] - window = sg.Window("Draw Text GUI", elements, size=(500, 500)) + window = sg.Window("Draw Text GUI", elements, size=(500, 600)) while True: event, values = window.read() @@ -143,6 +146,5 @@ def main(): window.close() - if __name__ == "__main__": main() \ No newline at end of file diff --git a/09_drawing_text/truetype_fonts.jpg b/09_drawing_text/truetype_fonts.jpg new file mode 100644 index 0000000..419573e Binary files /dev/null and b/09_drawing_text/truetype_fonts.jpg differ diff --git a/10_imagechops/add_images.py b/10_imagechops/add_images.py index c166103..2831556 100644 --- a/10_imagechops/add_images.py +++ b/10_imagechops/add_images.py @@ -6,10 +6,11 @@ def add_images(image_path_one, image_path_two, output_path, scale=1.0, offset=0): image_one = Image.open(image_path_one) image_two = Image.open(image_path_two) - image_three = ImageChops.add(image_one, image_two, scale=scale, offset=offset) + image_three = ImageChops.add( + image_one, image_two, scale=scale, offset=offset, + ) image_three.save(output_path) if __name__ == "__main__": - add_images("shell.png", "skyline.png", - "added_images.jpg") \ No newline at end of file + add_images("shell.png", "skyline.png", "added_images.jpg") \ No newline at end of file diff --git a/10_imagechops/apply_hard_light.py b/10_imagechops/apply_hard_light.py index 9d686b0..e7056d7 100644 --- a/10_imagechops/apply_hard_light.py +++ b/10_imagechops/apply_hard_light.py @@ -10,5 +10,4 @@ def hard_light(image_path_one, image_path_two, output_path): if __name__ == "__main__": - hard_light("shell.png", "skyline.png", - "hard_light.jpg") \ No newline at end of file + hard_light("shell.png", "skyline.png", "hard_light.jpg") \ No newline at end of file diff --git a/10_imagechops/apply_overlay.py b/10_imagechops/apply_overlay.py index 312a892..ac6b23e 100644 --- a/10_imagechops/apply_overlay.py +++ b/10_imagechops/apply_overlay.py @@ -10,5 +10,4 @@ def overlay(image_path_one, image_path_two, output_path): if __name__ == "__main__": - overlay("shell.png", "skyline.png", - "overlay.jpg") \ No newline at end of file + overlay("shell.png", "skyline.png", "overlay.jpg") \ No newline at end of file diff --git a/10_imagechops/apply_soft_light.py b/10_imagechops/apply_soft_light.py index 7bad856..e00ac2b 100644 --- a/10_imagechops/apply_soft_light.py +++ b/10_imagechops/apply_soft_light.py @@ -10,5 +10,4 @@ def soft_light(image_path_one, image_path_two, output_path): if __name__ == "__main__": - soft_light("shell.png", "skyline.png", - "soft_light.jpg") \ No newline at end of file + soft_light("shell.png", "skyline.png", "soft_light.jpg") \ No newline at end of file diff --git a/10_imagechops/darken_image.py b/10_imagechops/darken_image.py index 60ef51b..4804e1b 100644 --- a/10_imagechops/darken_image.py +++ b/10_imagechops/darken_image.py @@ -10,5 +10,4 @@ def darken(image_path_one, image_path_two, output_path): if __name__ == "__main__": - darken("shell.png", "skyline.png", - "darker_images.jpg") \ No newline at end of file + darken("shell.png", "skyline.png", "darker_image.jpg") \ No newline at end of file diff --git a/10_imagechops/imagechops_gui.py b/10_imagechops/imagechops_gui.py index e360b79..0142410 100644 --- a/10_imagechops/imagechops_gui.py +++ b/10_imagechops/imagechops_gui.py @@ -30,7 +30,7 @@ "Hard Light": hard_light, "Soft Light": soft_light, "Overlay": overlay, -} + } def apply_effect(values, window): @@ -39,23 +39,23 @@ def apply_effect(values, window): image_file_two = values["-FILENAME_TWO-"] if os.path.exists(image_file_one): shutil.copy(image_file_one, tmp_file) - if selected_effect == "Normal": - effects[selected_effect](image_file_one, tmp_file) - elif selected_effect == "Negative": + if selected_effect in ["Normal", "Negative"]: effects[selected_effect](image_file_one, tmp_file) elif os.path.exists(image_file_two): - effects[selected_effect](image_file_one, image_file_two, - tmp_file) + effects[selected_effect]( + image_file_one, image_file_two, tmp_file, + ) elif selected_effect not in ["Normal", "Negative"]: - sg.popup("You need both images selected to apply " - "this effect!") + sg.popup( + "You need both images selected to apply this effect!", + ) return image = Image.open(tmp_file) image.thumbnail((400, 400)) bio = io.BytesIO() image.save(bio, format="PNG") - window["-IMAGE-"].update(data=bio.getvalue()) + window["-IMAGE-"].update(data=bio.getvalue(), size=(400,400)) def create_row(label, key, file_types): @@ -63,17 +63,17 @@ def create_row(label, key, file_types): sg.Text(label), sg.Input(size=(25, 1), key=key), sg.FileBrowse(file_types=file_types), - ] + ] -def save_image(filename_one, filename_two): +def save_image(*filenames): save_filename = sg.popup_get_file( - "File", file_types=file_types, save_as=True, no_window=True - ) - filenames = [filename_one, filename_two] + "File", file_types=file_types, save_as=True, no_window=True, + ) if save_filename in filenames: sg.popup_error( - "You are not allowed to overwrite the original image!") + "You are not allowed to overwrite the original images!", + ) else: if save_filename: shutil.copy(tmp_file, save_filename) @@ -83,7 +83,7 @@ def save_image(filename_one, filename_two): def main(): effect_names = list(effects.keys()) layout = [ - [sg.Image(key="-IMAGE-", size=(400, 400))], + [sg.Image(key="-IMAGE-", size=(400,400))], create_row("Image File 1:", "-FILENAME_ONE-", file_types), [sg.Button("Load Image")], create_row("Image File 2:", "-FILENAME_TWO-", file_types), @@ -91,11 +91,11 @@ def main(): sg.Text("Effect"), sg.Combo( effect_names, default_value="Normal", key="-EFFECTS-", - enable_events=True, readonly=True - ), + enable_events=True, readonly=True, + ), ], [sg.Button("Save")], - ] + ] window = sg.Window("ImageChops GUI", layout, size=(450, 600)) @@ -115,4 +115,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/10_imagechops/lighten_image.py b/10_imagechops/lighten_image.py index 5d6200f..825a700 100644 --- a/10_imagechops/lighten_image.py +++ b/10_imagechops/lighten_image.py @@ -10,5 +10,4 @@ def lighten(image_path_one, image_path_two, output_path): if __name__ == "__main__": - lighten("shell.png", "skyline.png", - "lighter_image.jpg") \ No newline at end of file + lighten("shell.png", "skyline.png", "lighter_image.jpg") \ No newline at end of file diff --git a/11_imageops/apply_autocontrast.py b/11_imageops/apply_autocontrast.py index 62685ce..89ebe90 100644 --- a/11_imageops/apply_autocontrast.py +++ b/11_imageops/apply_autocontrast.py @@ -3,13 +3,11 @@ from PIL import Image, ImageOps -def autocontrast(image_path, output_path, cutoff=0, - ignore=None): +def autocontrast(image_path, output_path, cutoff=0, ignore=None): image = Image.open(image_path) converted_image = ImageOps.autocontrast(image, cutoff, ignore) converted_image.save(output_path) if __name__ == "__main__": - autocontrast("ducklings.jpg", "autocontrast.jpg", - cutoff=0.5) \ No newline at end of file + autocontrast("ducklings.jpg", "autocontrast.jpg", cutoff=0.5) \ No newline at end of file diff --git a/11_imageops/apply_crop.py b/11_imageops/apply_crop.py index a278c35..bed91c6 100644 --- a/11_imageops/apply_crop.py +++ b/11_imageops/apply_crop.py @@ -2,7 +2,6 @@ from PIL import Image, ImageOps - def crop(image_path, output_path, border): image = Image.open(image_path) converted_image = ImageOps.crop(image, border) @@ -10,5 +9,4 @@ def crop(image_path, output_path, border): if __name__ == "__main__": - crop("flower_border.jpg", "flower_no_border.jpg", - border=100) \ No newline at end of file + crop("flower_border.jpg", "flower_no_border.jpg", border=100) \ No newline at end of file diff --git a/11_imageops/apply_expand.py b/11_imageops/apply_expand.py index a4806e2..d3c10e5 100644 --- a/11_imageops/apply_expand.py +++ b/11_imageops/apply_expand.py @@ -10,5 +10,4 @@ def expand(image_path, output_path, border, fill): if __name__ == "__main__": - expand("flowers.jpg", "flower_border.jpg", - border=100, fill="yellow") \ No newline at end of file + expand("flowers.jpg", "flower_border.jpg", border=100, fill="yellow") \ No newline at end of file diff --git a/11_imageops/apply_scale.py b/11_imageops/apply_scale.py index 4f27ce2..054a799 100644 --- a/11_imageops/apply_scale.py +++ b/11_imageops/apply_scale.py @@ -10,5 +10,4 @@ def scale(image_path, output_path, factor): if __name__ == "__main__": - scale("flowers.jpg", "flower_scaled.jpg", - factor=0.5) \ No newline at end of file + scale("flowers.jpg", "flower_scaled.jpg", factor=0.6) \ No newline at end of file diff --git a/11_imageops/imageops_gui.py b/11_imageops/imageops_gui.py index 495c7c7..b782a58 100644 --- a/11_imageops/imageops_gui.py +++ b/11_imageops/imageops_gui.py @@ -8,11 +8,9 @@ from apply_autocontrast import autocontrast from apply_equalize import equalize -from apply_fit import fit from apply_flip import flip from apply_invert import invert from apply_mirror import mirror -from apply_posterize import posterize from apply_solarize import solarize from PIL import Image @@ -23,24 +21,21 @@ "Normal": shutil.copy, "Autocontrast": autocontrast, "Equalize": equalize, - "Fit": fit, "Flip": flip, "Mirror": mirror, "Negative": invert, - "Posterize": posterize, "Solarize": solarize, -} + } def apply_effect(image_file_one, effect, image_obj): if os.path.exists(image_file_one): effects[effect](image_file_one, tmp_file) - image = Image.open(tmp_file) image.thumbnail((400, 400)) bio = io.BytesIO() image.save(bio, format="PNG") - image_obj.update(data=bio.getvalue()) + image_obj.update(data=bio.getvalue(), size=(400,400)) def create_row(label, key, file_types): @@ -48,13 +43,13 @@ def create_row(label, key, file_types): sg.Text(label), sg.Input(size=(25, 1), key=key), sg.FileBrowse(file_types=file_types), - ] + ] def save_image(filename_one): save_filename = sg.popup_get_file( - "File", file_types=file_types, save_as=True, no_window=True - ) + "File", file_types=file_types, save_as=True, no_window=True, + ) if save_filename == filename_one: sg.popup_error("You are not allowed to overwrite the original image!") else: @@ -66,7 +61,7 @@ def save_image(filename_one): def main(): effect_names = list(effects.keys()) layout = [ - [sg.Image(key="-IMAGE-", size=(400, 400))], + [sg.Image(key="-IMAGE-", size=(400,400))], create_row("Image File 1:", "-FILENAME_ONE-", file_types), [sg.Button("Load Image")], [ @@ -77,7 +72,7 @@ def main(): key="-EFFECTS-", enable_events=True, readonly=True, - ), + ), ], [sg.Button("Save")], ] @@ -103,4 +98,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/12_integration/image_viewer.py b/12_integration/image_viewer.py new file mode 100644 index 0000000..677ff53 --- /dev/null +++ b/12_integration/image_viewer.py @@ -0,0 +1,42 @@ +# image_viewer.py + +import io +import os +import PySimpleGUI as sg +from PIL import Image + + +file_types = [("JPEG (*.jpg)", "*.jpg"), + ("All files (*.*)", "*.*")] + +def main(): + layout = [ + [sg.Image(key="-IMAGE-")], + [ + sg.Text("Image File"), + sg.Input(size=(25, 1), key="-FILE-"), + sg.FileBrowse(file_types=file_types), + sg.Button("Load Image"), + ], + ] + + window = sg.Window("Image Viewer", layout) + + while True: + event, values = window.read() + if event == "Exit" or event == sg.WIN_CLOSED: + break + if event == "Load Image": + filename = values["-FILE-"] + if os.path.exists(filename): + image = Image.open(values["-FILE-"]) + image.thumbnail((400, 400)) + bio = io.BytesIO() + image.save(bio, format="PNG") + window["-IMAGE-"].update(data=bio.getvalue()) + + window.close() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/12_integration/kivy_image.py b/12_integration/kivy_image.py index 1298fb7..f49400c 100644 --- a/12_integration/kivy_image.py +++ b/12_integration/kivy_image.py @@ -9,4 +9,4 @@ def build(self): return Image(source="pink_flower.jpg") # run the App -ImageViewer().run() +ImageViewer().run() \ No newline at end of file diff --git a/12_integration/kivy_pillow_demo.py b/12_integration/kivy_pillow_demo.py index 1f00a95..98682db 100644 --- a/12_integration/kivy_pillow_demo.py +++ b/12_integration/kivy_pillow_demo.py @@ -11,9 +11,9 @@ class MyApp(App): def build(self): image = Image(source="") pil_image = PilImage.open("pink_flower.jpg") - img_data = BytesIO() # Save PIL image to memory + img_data = BytesIO() pil_image.save(img_data, format='png') img_data.seek(0) @@ -24,4 +24,4 @@ def build(self): return image # run the App -MyApp().run() +MyApp().run() \ No newline at end of file diff --git a/12_integration/psg_pillow_demo.py b/12_integration/psg_pillow_demo.py index 22f3004..0b9e07d 100644 --- a/12_integration/psg_pillow_demo.py +++ b/12_integration/psg_pillow_demo.py @@ -1,4 +1,4 @@ -# psg_pilloww_demo.py +# psg_pillow_demo.py import PySimpleGUI as sg from PIL import Image, ImageTk diff --git a/12_integration/tk_pillow_demo.py b/12_integration/tk_pillow_demo.py index f098f6a..add8ed3 100644 --- a/12_integration/tk_pillow_demo.py +++ b/12_integration/tk_pillow_demo.py @@ -3,6 +3,7 @@ from tkinter import Tk, Canvas, NW from PIL import Image, ImageTk + def main(): root = Tk() root.title("Tkinter Image Viewer") diff --git a/12_integration/wx_pillow_demo.py b/12_integration/wx_pillow_demo.py index 3fd72ad..5b414ad 100644 --- a/12_integration/wx_pillow_demo.py +++ b/12_integration/wx_pillow_demo.py @@ -12,8 +12,7 @@ def __init__(self, parent, image_size): width, height = pil_img.size bitmap = wx.BitmapFromBuffer(width, height, pil_img.tobytes()) - self.image_ctrl = wx.StaticBitmap( - self, bitmap=wx.Bitmap(bitmap)) + self.image_ctrl = wx.StaticBitmap(self, bitmap=wx.Bitmap(bitmap)) main_sizer = wx.BoxSizer(wx.VERTICAL) main_sizer.Add(self.image_ctrl, 0, wx.ALL, 5) @@ -22,6 +21,7 @@ def __init__(self, parent, image_size): main_sizer.Fit(parent) self.Layout() + class MainFrame(wx.Frame): def __init__(self): super().__init__(None, title="wxPython Image Viewer") diff --git a/13_alternatives/concatenating.py b/13_alternatives/concatenating.py index 8dc17be..348104d 100644 --- a/13_alternatives/concatenating.py +++ b/13_alternatives/concatenating.py @@ -21,4 +21,4 @@ def concatenate(input_image_path, output_path): output.save(output_path) if __name__ == "__main__": - concatenate("author.jpg", "concatenated.jpg") \ No newline at end of file + concatenate("author.jpg", "stacked.jpg") \ No newline at end of file diff --git a/13_alternatives/eye_finder.py b/13_alternatives/eye_finder.py index 9ecd1f8..47c09f5 100644 --- a/13_alternatives/eye_finder.py +++ b/13_alternatives/eye_finder.py @@ -19,16 +19,26 @@ def find_eyes(image_path): haar_classifier = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml") - faces = haar_classifier.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=5) + faces = haar_classifier.detectMultiScale( + gray_img, scaleFactor=1.1, minNeighbors=5, + ) print("Number of faces found: {faces}".format(faces=len(faces))) for (x, y, width, height) in faces: - cv2.rectangle(color_img, (x, y), (x + width, y + height), (0, 255, 0), 2) + cv2.rectangle( + color_img, + (x, y), (x + width, y + height), + (0, 255, 0), 2, + ) roi_gray = gray_img[y : y + height, x : x + width] roi_color = color_img[y : y + height, x : x + width] eyes = eye_cascade.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: - cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) + cv2.rectangle( + roi_color, + (ex, ey), (ex + ew, ey + eh), + (0, 255, 0), 2, + ) # Show the faces / eyes found cv2.imshow(filename, color_img) @@ -37,4 +47,4 @@ def find_eyes(image_path): if __name__ == "__main__": - find_eyes("author.jpg") + find_eyes("author.jpg") \ No newline at end of file diff --git a/13_alternatives/face_finder.py b/13_alternatives/face_finder.py index 0ad9e5d..3af27f0 100644 --- a/13_alternatives/face_finder.py +++ b/13_alternatives/face_finder.py @@ -18,11 +18,17 @@ def find_faces(image_path): # Use OpenCV's built-in Haar classifier haar_classifier = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") - faces = haar_classifier.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=5) + faces = haar_classifier.detectMultiScale( + gray_img, scaleFactor=1.1, minNeighbors=5, + ) print("Number of faces found: {faces}".format(faces=len(faces))) for (x, y, width, height) in faces: - cv2.rectangle(color_img, (x, y), (x + width, y + height), (0, 255, 0), 2) + cv2.rectangle( + color_img, + (x, y), (x + width, y + height), + (0, 255, 0), 2, + ) # Show the faces found cv2.imshow(filename, color_img) @@ -31,4 +37,4 @@ def find_faces(image_path): if __name__ == "__main__": - find_faces("author.jpg") + find_faces("author.jpg") \ No newline at end of file diff --git a/13_alternatives/wand_edger.py b/13_alternatives/wand_edger.py index 10db1ab..62c4e18 100644 --- a/13_alternatives/wand_edger.py +++ b/13_alternatives/wand_edger.py @@ -9,6 +9,5 @@ def edge(input_image_path, output_path): img.edge(radius=3) img.save(filename=output_path) - if __name__ == "__main__": edge("ducklings.jpg", "edged.jpg") \ No newline at end of file diff --git a/14_batch_processing/controller.py b/14_batch_processing/controller.py index c8c6db0..760caa9 100644 --- a/14_batch_processing/controller.py +++ b/14_batch_processing/controller.py @@ -9,12 +9,12 @@ from PIL import Image -def get_image_paths(path, recursive, file_format="png"): - if "/" == path[-1]: - search_path = path + f"**/*.{file_format}" - else: - search_path = path + f"/**/*.{file_format}" - +def get_image_paths(search_path, recursive, file_format="png"): + if "/" != search_path[-1]: + search_path += "/" + if recursive: + search_path += "**/" + search_path += f"*.{file_format}" image_paths = glob.glob(search_path, recursive=recursive) return image_paths @@ -29,7 +29,8 @@ def resize_image(image_path, width, height, output_dir): pil_image.save(output) return f"{image_path} converted to {output}" else: - return f"{image_path} size is smaller than new size. Skipping file." + pil_image.save(output) + return f"{image_path} copied to {output}." def resize_images(image_paths, width, height, output_dir): @@ -49,15 +50,16 @@ def resize_images(image_paths, width, height, output_dir): images_converted = 0 with ThreadPoolExecutor(max_workers=5) as executor: futures = [ - executor.submit(resize_image, image_path, width, height, - output_dir) for - image_path in image_paths - ] + executor.submit( + resize_image, image_path, width, height, output_dir, + ) + for image_path in image_paths + ] for future in as_completed(futures): result = future.result() - if "Skipping" not in result: + if "converted" in result: images_converted += 1 - print(result) + print(future.result()) end = time.time() print(f"Converted {images_converted} image(s) in {end-start} seconds.") diff --git a/14_batch_processing/image_resizer_cli.py b/14_batch_processing/image_resizer_cli.py index e30e943..a00fe20 100644 --- a/14_batch_processing/image_resizer_cli.py +++ b/14_batch_processing/image_resizer_cli.py @@ -8,12 +8,12 @@ from PIL import Image -def get_image_paths(path, recursive): - if "/" == path[-1]: - search_path = path + "**/*.png" - else: - search_path = path + "/**/*.png" - +def get_image_paths(search_path, recursive): + if "/" != search_path[-1]: + search_path += "/" + if recursive: + search_path += "**/" + search_path += "*.png" image_paths = glob.glob(search_path, recursive=recursive) return image_paths @@ -39,12 +39,15 @@ def resize_images(image_paths, width, height, output_dir): image_name = os.path.basename(image_path) output = os.path.join(output_dir, image_name) if height < im_h or width < im_w: + # convert image and inform user pil_image.thumbnail((width, height), Image.ANTIALIAS) - pil_image.save(output) print(f"{image_path} converted to {output}") images_converted += 1 else: + # do not convert image, and inform user print(f"{image_path} size is smaller than new size. Skipping file.") + # save (converted) image to destination directory + pil_image.save(output) end = time.time() print(f"Converted {images_converted} image(s) in {end-start} seconds.") print(f"Output folder is: {output_dir}") @@ -93,4 +96,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/14_batch_processing/image_resizer_cli_modular.py b/14_batch_processing/image_resizer_cli_modular.py index 2d881b0..1b233f4 100644 --- a/14_batch_processing/image_resizer_cli_modular.py +++ b/14_batch_processing/image_resizer_cli_modular.py @@ -34,11 +34,11 @@ def main(): return else: if controller.validate_directory(input_dir): - image_paths = controller.get_image_paths( - input_dir, args.recursive) + image_paths = controller.get_image_paths(input_dir, args.recursive) controller.resize_images( - image_paths, args.width, args.height, output_dir) + image_paths, args.width, args.height, output_dir, + ) if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/14_batch_processing/image_resizer_cli_threads.py b/14_batch_processing/image_resizer_cli_threads.py index 8e581ef..e4c852e 100644 --- a/14_batch_processing/image_resizer_cli_threads.py +++ b/14_batch_processing/image_resizer_cli_threads.py @@ -10,12 +10,12 @@ from PIL import Image -def get_image_paths(path, recursive): - if "/" == path[-1]: - search_path = path + "**/*.png" - else: - search_path = path + "/**/*.png" - +def get_image_paths(search_path, recursive): + if "/" != search_path[-1]: + search_path += "/" + if recursive: + search_path += "**/" + search_path += "*.png" image_paths = glob.glob(search_path, recursive=recursive) return image_paths @@ -30,7 +30,8 @@ def resize_image(image_path, width, height, output_dir): pil_image.save(output) return f"{image_path} converted to {output}" else: - return f"{image_path} size is smaller than new size. Skipping file." + pil_image.save(output) + return f"{image_path} copied to {output}." def resize_images(image_paths, width, height, output_dir): @@ -50,15 +51,16 @@ def resize_images(image_paths, width, height, output_dir): images_converted = 0 with ThreadPoolExecutor(max_workers=5) as executor: futures = [ - executor.submit(resize_image, image_path, width, height, - output_dir) for - image_path in image_paths - ] + executor.submit( + resize_image, image_path, width, height, output_dir, + ) + for image_path in image_paths + ] for future in as_completed(futures): result = future.result() - if "Skipping" not in result: + if "converted" in result: images_converted += 1 - print(result) + print(future.result()) end = time.time() print(f"Converted {images_converted} image(s) in {end-start} seconds.") @@ -108,4 +110,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/14_batch_processing/image_resizer_gui.py b/14_batch_processing/image_resizer_gui.py index 33092d2..ac60464 100644 --- a/14_batch_processing/image_resizer_gui.py +++ b/14_batch_processing/image_resizer_gui.py @@ -33,8 +33,7 @@ def resize(values): if len(image_paths) < 1: sg.popup("No images found") return - controller.resize_images(image_paths, width, height, - output_folder) + controller.resize_images(image_paths, width, height, output_folder) def verify(input_folder, output_folder, width, height): @@ -59,7 +58,7 @@ def main(): [sg.Checkbox("Recursive Search", key="-RECURSIVE-", enable_events=True), sg.Text("Format:"), - sg.Combo(values=["jpg, png"], default_value="png", + sg.Combo(values=["jpg","png"], default_value="png", readonly=True, key="-FORMAT-", enable_events=True)], create_row("Output Image Folder", "-OUTPUT_FOLDER-"), [ @@ -68,7 +67,7 @@ def main(): sg.Text("Height"), sg.Input(key="-HEIGHT-", enable_events=True, size=(10, 5)) ], - [sg.Output()], + [sg.Output(size=(80, 3))], [sg.Button("Resize")], ]