|
8 | 8 | import PIL.Image
|
9 | 9 |
|
10 | 10 |
|
11 |
| -def is_image(filename): |
12 |
| - """ |
13 |
| - Checks if a filename is an image (png, jpg or jpeg, case insensitive) |
14 |
| -
|
15 |
| - :param filename: The filename (as a string) |
16 |
| - :return: `True` if the given filename is an image, or `False` if it's not |
17 |
| - """ |
18 |
| - |
19 |
| - file = filename.lower() |
20 |
| - return file.endswith(".png") or file.endswith(".jpg") or file.endswith(".jpeg") |
21 |
| - |
22 |
| - |
23 |
| -def get_filename_with_type(filename, file_type, suffix=""): |
24 |
| - """ |
25 |
| - Get a filename and return it with the given suffix and the correct file extension for the given type |
26 |
| -
|
27 |
| - :param filename: The filename (e.g. "image_file.jpg") |
28 |
| - :param file_type: The file type (e.g. "PNG") |
29 |
| - :param suffix: An optional string to place between the name and the extension of the file (default is "") |
30 |
| - :return: the filename with the correct extension for the given type (e.g. "image_file.png") |
31 |
| - """ |
32 |
| - |
33 |
| - extension = filename.split(".")[-1] |
34 |
| - return filename[:-(len(extension) + 1)] + suffix + "." + file_type.lower() |
35 |
| - |
36 |
| - |
37 |
| -def export_file(path, name, width, height, export_type, overwrite): |
38 |
| - """ |
39 |
| - Open, resize and save an image with the given properties |
40 |
| -
|
41 |
| - :param path: The path to the directory where the image is located (without the image filename) |
42 |
| - :param name: The filename of the image we want to export |
43 |
| - :param width: The new width we want to resize to |
44 |
| - :param height: The new height we want to resize to |
45 |
| - :param export_type: The file type we want to save to (we ignore it if `overwrite` is `True`) |
46 |
| - :param overwrite: Whether we want to overwrite the original files or not |
47 |
| - """ |
48 |
| - |
49 |
| - img_path = os.path.join(path, name) |
50 |
| - |
51 |
| - # set the destination image file we want to save |
52 |
| - dest_img_name = get_filename_with_type(name, export_type, "_resize") |
53 |
| - if overwrite: |
54 |
| - dest_img_name = name |
55 |
| - |
56 |
| - try: |
57 |
| - # open the given image, resize and save it |
58 |
| - img = PIL.Image.open(img_path) |
59 |
| - img = img.resize((width, height), PIL.Image.ANTIALIAS) |
60 |
| - img.save(os.path.join(path, dest_img_name)) |
61 |
| - except IOError: |
62 |
| - return False |
63 |
| - |
64 |
| - return True |
65 |
| - |
66 |
| - |
67 |
| -def image_files_in_dir(selected_dir): |
68 |
| - """ |
69 |
| - Returns the number of image files in the given directory |
70 |
| -
|
71 |
| - :param selected_dir: The directory containing the images |
72 |
| - :return: the number of image files the directory contains |
73 |
| - """ |
74 |
| - images = 0 |
75 |
| - |
76 |
| - for path, subdirs, files in os.walk(selected_dir): |
77 |
| - for name in files: |
78 |
| - if is_image(name): |
79 |
| - images += 1 |
80 |
| - |
81 |
| - return images |
82 |
| - |
83 |
| - |
84 |
| -def export_all_in_dir(selected_dir, width, height, export_type, overwrite, progress_bar_callback=None): |
85 |
| - """ |
86 |
| - Export all the images in the selected directory |
87 |
| -
|
88 |
| - When `self.init_export()` is called, everything is ready to resize and export images |
89 |
| - This loops through all the files in the given directory and calls `self.export_file()` |
90 |
| - for the actual opening, resizing and saving of the files |
91 |
| -
|
92 |
| - :param selected_dir: The path to the directory containing all the images to resize |
93 |
| - :param width: The new width we want to resize to |
94 |
| - :param height: The new height we want to resize to |
95 |
| - :param export_type: The file type we want to save to (we ignore it if `overwrite` is `True`) |
96 |
| - :param overwrite: Whether we want to overwrite the original files or not |
97 |
| - :return: `True` if all images were exported successfully or `False` if there was an error |
98 |
| - """ |
99 |
| - |
100 |
| - all_exported_successfully = True |
101 |
| - |
102 |
| - for path, subdirs, files in os.walk(selected_dir): |
103 |
| - for name in files: |
104 |
| - if is_image(name): |
105 |
| - # export and check if everything went okay |
106 |
| - exported_successfully = export_file(path, name, width, height, export_type, overwrite) |
107 |
| - if not exported_successfully: |
108 |
| - all_exported_successfully = False |
109 |
| - |
110 |
| - # update the progress bar |
111 |
| - if progress_bar_callback is not None: |
112 |
| - progress_bar_callback() |
113 |
| - |
114 |
| - return all_exported_successfully |
| 11 | +class HelpingMethods(object): |
| 12 | + @staticmethod |
| 13 | + def is_image(filename): |
| 14 | + """ |
| 15 | + Checks if a filename is an image (png, jpg or jpeg, case insensitive) |
| 16 | +
|
| 17 | + :param filename: The filename (as a string) |
| 18 | + :return: `True` if the given filename is an image, or `False` if it's not |
| 19 | + """ |
| 20 | + |
| 21 | + file = filename.lower() |
| 22 | + return file.endswith(".png") or file.endswith(".jpg") or file.endswith(".jpeg") |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def get_filename_with_type(filename, file_type, suffix=""): |
| 26 | + """ |
| 27 | + Get a filename and return it with the given suffix and the correct file extension for the given type |
| 28 | +
|
| 29 | + :param filename: The filename (e.g. "image_file.jpg") |
| 30 | + :param file_type: The file type (e.g. "PNG") |
| 31 | + :param suffix: An optional string to place between the name and the extension of the file (default is "") |
| 32 | + :return: the filename with the correct extension for the given type (e.g. "image_file.png") |
| 33 | + """ |
| 34 | + |
| 35 | + extension = filename.split(".")[-1] |
| 36 | + return filename[:-(len(extension) + 1)] + suffix + "." + file_type.lower() |
| 37 | + |
| 38 | + |
| 39 | +class ImgEdit(object): |
| 40 | + def export_file(self, path, name, width, height, export_type, overwrite): |
| 41 | + """ |
| 42 | + Open, resize and save an image with the given properties |
| 43 | +
|
| 44 | + :param path: The path to the directory where the image is located (without the image filename) |
| 45 | + :param name: The filename of the image we want to export |
| 46 | + :param width: The new width we want to resize to |
| 47 | + :param height: The new height we want to resize to |
| 48 | + :param export_type: The file type we want to save to (we ignore it if `overwrite` is `True`) |
| 49 | + :param overwrite: Whether we want to overwrite the original files or not |
| 50 | + """ |
| 51 | + |
| 52 | + img_path = os.path.join(path, name) |
| 53 | + |
| 54 | + # set the destination image file we want to save |
| 55 | + dest_img_name = HelpingMethods.get_filename_with_type(name, export_type, "_resize") |
| 56 | + if overwrite: |
| 57 | + dest_img_name = name |
| 58 | + |
| 59 | + try: |
| 60 | + # open the given image, resize and save it |
| 61 | + img = PIL.Image.open(img_path) |
| 62 | + img = img.resize((width, height), PIL.Image.ANTIALIAS) |
| 63 | + img.save(os.path.join(path, dest_img_name)) |
| 64 | + except IOError: |
| 65 | + return False |
| 66 | + |
| 67 | + return True |
| 68 | + |
| 69 | + def export_all_in_dir(self, selected_dir, width, height, export_type, overwrite, q): |
| 70 | + """ |
| 71 | + Export all the images in the selected directory |
| 72 | +
|
| 73 | + When `self.init_export()` is called, everything is ready to resize and export images |
| 74 | + This loops through all the files in the given directory and calls `self.export_file()` |
| 75 | + for the actual opening, resizing and saving of the files |
| 76 | +
|
| 77 | + :param selected_dir: The path to the directory containing all the images to resize |
| 78 | + :param width: The new width we want to resize to |
| 79 | + :param height: The new height we want to resize to |
| 80 | + :param export_type: The file type we want to save to (we ignore it if `overwrite` is `True`) |
| 81 | + :param overwrite: Whether we want to overwrite the original files or not |
| 82 | + :return: `True` if all images were exported successfully or `False` if there was an error |
| 83 | + """ |
| 84 | + |
| 85 | + # reset default values |
| 86 | + self.num_of_exported_images = 0 |
| 87 | + self.num_of_images_to_export = None |
| 88 | + |
| 89 | + # loop through all the files in the given directory and store the |
| 90 | + # path and the filename of image files in a list |
| 91 | + images = [] |
| 92 | + for path, subdirs, files in os.walk(selected_dir): |
| 93 | + for name in files: |
| 94 | + if HelpingMethods.is_image(name): |
| 95 | + images.append({ |
| 96 | + "path": path, |
| 97 | + "name": name |
| 98 | + }) |
| 99 | + self.num_of_images_to_export = len(images) |
| 100 | + |
| 101 | + # loop through the images list to open, resize and save them |
| 102 | + all_exported_successfully = True |
| 103 | + for img in images: |
| 104 | + # export and check if everything went okay |
| 105 | + exported_successfully = self.export_file(img["path"], img["name"], width, height, export_type, overwrite) |
| 106 | + if not exported_successfully: |
| 107 | + all_exported_successfully = False |
| 108 | + |
| 109 | + self.num_of_exported_images += 1 |
| 110 | + |
| 111 | + q.put(all_exported_successfully) |
| 112 | + |
| 113 | + def __init__(self): |
| 114 | + self.num_of_exported_images = 0 |
| 115 | + self.num_of_images_to_export = None |
0 commit comments