|
| 1 | +from PIL import Image |
| 2 | +import os |
| 3 | + |
| 4 | +class Brander(): |
| 5 | + def __init__(self): |
| 6 | + # source directory |
| 7 | + self.from_dir = '' |
| 8 | + self.logo_path = '' |
| 9 | + self.logo_initial_obj = None |
| 10 | + self.scale = '' |
| 11 | + self.corner = '' |
| 12 | + self.to_dir_path = '' |
| 13 | + self.extensions = [] |
| 14 | + self.x = None |
| 15 | + self.y = None |
| 16 | + |
| 17 | + def setSrcDir(self, srcDir): |
| 18 | + """ |
| 19 | + save in a variable the path to the directory |
| 20 | + where we want to take the images from |
| 21 | + """ |
| 22 | + self.from_dir = srcDir |
| 23 | + |
| 24 | + def setLogoPath(self, logoPath): |
| 25 | + self.logo_path = logoPath |
| 26 | + |
| 27 | + # Associate an object that can be used in our program, |
| 28 | + # with the image at this path |
| 29 | + self.logo_initial_obj = Image.open(self.logo_path) |
| 30 | + |
| 31 | + def setScale(self, scale): |
| 32 | + self.scale = scale |
| 33 | + |
| 34 | + def setCorner(self, corner): |
| 35 | + # In which corner of the image will the logo be put? |
| 36 | + self.corner = corner |
| 37 | + |
| 38 | + def setDestPath(self, destPath): |
| 39 | + # Where do I save the new images? |
| 40 | + # set the path to the directory where to copy the modified images |
| 41 | + self.to_dir_path = destPath |
| 42 | + |
| 43 | + def addExtension(self, ext): |
| 44 | + """ |
| 45 | + Called when the user checks an extension box in the GUI. |
| 46 | + :param ext: |
| 47 | + :return: |
| 48 | + """ |
| 49 | + if ext.lower() not in self.extensions: |
| 50 | + self.extensions.append(ext.lower()) |
| 51 | + |
| 52 | + def removeExtension(self, ext): |
| 53 | + """ |
| 54 | + Called when box is unchecked. |
| 55 | + :param ext: |
| 56 | + :return: |
| 57 | + """ |
| 58 | + if ext.lower() in self.extensions: |
| 59 | + self.extensions.remove(ext.lower()) |
| 60 | + |
| 61 | + def srcImgsToObjects(self): |
| 62 | + # Keep a list of image objects all from this dir |
| 63 | + # having one of the the specified extensions |
| 64 | + os.chdir(self.from_dir) |
| 65 | + |
| 66 | + print("from dir = ", self.from_dir) |
| 67 | + |
| 68 | + to_be_proc_list = [] |
| 69 | + im_names = [] |
| 70 | + print ("extensions = ", self.extensions) |
| 71 | + for filename in os.listdir(self.from_dir): |
| 72 | + print("filename = ", filename) |
| 73 | + for ext in self.extensions: |
| 74 | + if filename.endswith(ext.lower()) \ |
| 75 | + or filename.endswith(ext.upper()): |
| 76 | + to_be_proc_list.append(Image.open(filename)) |
| 77 | + im_names.append(filename) |
| 78 | + continue |
| 79 | + |
| 80 | + return im_names, to_be_proc_list |
| 81 | + |
| 82 | + def resizeLogo(self): |
| 83 | + # Ask the user if it is upscale or down scale and by how much (width, height) |
| 84 | + w, h = self.logo_initial_obj.size |
| 85 | + scale_factor_w = 2; scale_factor_h = 2 |
| 86 | + if self.scale == "smaller": |
| 87 | + w, h = w / scale_factor_w, h / scale_factor_h |
| 88 | + elif self.scale == "bigger": |
| 89 | + w, h = w * scale_factor_w, h * scale_factor_h |
| 90 | + |
| 91 | + # Build another object with the original logo resized to what's better for us |
| 92 | + logo_obj = self.logo_initial_obj.resize((int(w), int(h))) |
| 93 | + |
| 94 | + return logo_obj, w, h |
| 95 | + |
| 96 | + def get_corner_pos(self, w_logo, h_logo, w_img, h_img, corner): |
| 97 | + """ |
| 98 | + Determine a proper position of the left corner fo the logo, |
| 99 | + relative to the image it will be put on. |
| 100 | + :param h_logo: |
| 101 | + :param w_img: |
| 102 | + :param h_img: |
| 103 | + :param corner: |
| 104 | + :return: |
| 105 | + """ |
| 106 | + until_margin = int(min(w_logo, h_logo) / 4) |
| 107 | + pos_coords = {"UP LEFT": (until_margin, until_margin), \ |
| 108 | + "UP RIGHT": (w_img - w_logo - until_margin, until_margin), \ |
| 109 | + "DOWN LEFT": (until_margin, h_img - h_logo - until_margin), \ |
| 110 | + "DOWN RIGHT": (w_img - w_logo - until_margin, h_img - h_logo - until_margin)} |
| 111 | + return pos_coords[corner] |
| 112 | + |
| 113 | + def canBrand(self): |
| 114 | + if self.from_dir == '' or self.logo_path == '' \ |
| 115 | + or self.scale == '' or self.corner == '' \ |
| 116 | + or self.to_dir_path == '' or len(self.extensions) <= 0: |
| 117 | + return False |
| 118 | + return True |
| 119 | + |
| 120 | + def brandAndSave(self): |
| 121 | + logo_obj, w_logo, h_logo = self.resizeLogo() |
| 122 | + im_names, to_be_proc_list = self.srcImgsToObjects() |
| 123 | + |
| 124 | + # Change current directory to the value of this path |
| 125 | + os.chdir(self.to_dir_path) |
| 126 | + |
| 127 | + # Introduce directory name at this path. |
| 128 | + # If the directory with the name introduced does not exist, create it. |
| 129 | + to_dir_name = "pycodette_loggoed" |
| 130 | + if not os.path.exists(to_dir_name): |
| 131 | + os.makedirs(to_dir_name) |
| 132 | + # Change directory to the created dir |
| 133 | + os.chdir(to_dir_name) |
| 134 | + |
| 135 | + print(to_dir_name) |
| 136 | + print("to be proc list = ", to_be_proc_list) |
| 137 | + |
| 138 | + # Create a new list of image objects |
| 139 | + #img_processed_list = [] |
| 140 | + import copy |
| 141 | + |
| 142 | + # Iterate through the list of images to be processed |
| 143 | + # Hint: use for |
| 144 | + for i in range(len(to_be_proc_list)): |
| 145 | + print("img", i) |
| 146 | + img = to_be_proc_list[i] |
| 147 | + img_new = copy.deepcopy(img) |
| 148 | + w_logo, h_logo = logo_obj.size |
| 149 | + w_img, h_img = img.size |
| 150 | + if self.x != None and self.y != None: |
| 151 | + pos_new = (self.x, self.y) |
| 152 | + else: |
| 153 | + pos_new = \ |
| 154 | + self.get_corner_pos(w_logo, h_logo, w_img, h_img, self.corner) |
| 155 | + img_new.paste(logo_obj, pos_new, logo_obj) |
| 156 | + |
| 157 | + # Save the new image into the current working directory |
| 158 | + img_new.save(im_names[i]) |
0 commit comments