def getAdjacent(self, provinceID): """ Returns a list of adjacent provinceIDs. """ # get province bounding box provinceColorImage = ImageChops.constant(self.provinceImage, self.provinceColorFromID[provinceID]) mask = ImageChops.invert(neImage(self.provinceImage, provinceColorImage)) xMin, yMin, xMax, yMax = Image.getbbox(mask) # grow box #TODO: wraparound xMin = max(0, xMin - 1) yMin = max(0, yMin - 1) xMax = min(self.provinceImage.size[0]-1, xMax + 1) yMax = min(self.provinceImage.size[1]-1, yMax + 1) box = (xMin, yMin, xMax, yMax) # crop to area mask = mask.crop(box) growFilter = ImageFilter.Kernel((3, 3), (0, 1, 0, 1, 0, 1, 0, 1, 0)) mask = mask.filter(growFilter) provinceColorImage = provinceColorImage.crop(box) blackImage = ImageChops.constant(provinceColorImage, (0, 0, 0)) provinceColorImage = Image.composite(blackImage, provinceColorImage, mask) borderColors = provinceColorImage.getcolors() result = [color for (count, color) in borderColors]
def get_adjacent(self, province_id): """ Returns a list of adjacent province_ids. """ # get province bounding box province_color_image = ImageChops.constant( self.province_image, self.province_color_by_id[province_id]) mask = ImageChops.invert( ne_image(self.province_image, province_color_image)) x_min, y_min, x_max, y_max = Image.getbbox(mask) # grow box #TODO: wraparound x_min = max(0, x_min - 1) y_min = max(0, y_min - 1) x_max = min(self.province_image.size[0] - 1, x_max + 1) y_max = min(self.province_image.size[1] - 1, y_max + 1) box = (x_min, y_min, x_max, y_max) # crop to area mask = mask.crop(box) grow_filter = ImageFilter.Kernel((3, 3), (0, 1, 0, 1, 0, 1, 0, 1, 0)) mask = mask.filter(grow_filter) province_color_image = province_color_image.crop(box) black_image = ImageChops.constant(province_color_image, (0, 0, 0)) province_color_image = Image.composite(black_image, province_color_image, mask) border_colors = province_color_image.getcolors() result = [color for (count, color) in border_colors]
def get_adjacent(self, province_id): """ Returns a list of adjacent province_ids. """ # get province bounding box province_color_image = ImageChops.constant(self.province_image, self.province_color_by_id[province_id]) mask = ImageChops.invert(ne_image(self.province_image, province_color_image)) x_min, y_min, x_max, y_max = Image.getbbox(mask) # grow box #TODO: wraparound x_min = max(0, x_min - 1) y_min = max(0, y_min - 1) x_max = min(self.province_image.size[0]-1, x_max + 1) y_max = min(self.province_image.size[1]-1, y_max + 1) box = (x_min, y_min, x_max, y_max) # crop to area mask = mask.crop(box) grow_filter = ImageFilter.Kernel((3, 3), (0, 1, 0, 1, 0, 1, 0, 1, 0)) mask = mask.filter(grow_filter) province_color_image = province_color_image.crop(box) black_image = ImageChops.constant(province_color_image, (0, 0, 0)) province_color_image = Image.composite(black_image, province_color_image, mask) border_colors = province_color_image.getcolors() result = [color for (count, color) in border_colors]
def autocrop_image(image: Image) -> Image: """ This function is adapted from https://gist.github.com/odyniec/3470977 :param image: input png image to crop to content :return: cropped Image object in jpg format """ # Get the bounding box bbox = image.getbbox() # Crop the image to the contents of the bounding box image = image.crop(bbox) # Determine the width and height of the cropped image (width, height) = image.size # Create a new image object for the output image cropped_image = Image.new("RGBA", (width, height), (0, 0, 0, 0)) # Paste the cropped image onto the new image cropped_image.paste(image) # Done! return cropped_image.convert("RGB")
def getAdjacent(self, provinceID): """ Returns a list of adjacent provinceIDs. """ # get province bounding box provinceColorImage = ImageChops.constant( self.provinceImage, self.provinceColorFromID[provinceID]) mask = ImageChops.invert( neImage(self.provinceImage, provinceColorImage)) xMin, yMin, xMax, yMax = Image.getbbox(mask) # grow box #TODO: wraparound xMin = max(0, xMin - 1) yMin = max(0, yMin - 1) xMax = min(self.provinceImage.size[0] - 1, xMax + 1) yMax = min(self.provinceImage.size[1] - 1, yMax + 1) box = (xMin, yMin, xMax, yMax) # crop to area mask = mask.crop(box) growFilter = ImageFilter.Kernel((3, 3), (0, 1, 0, 1, 0, 1, 0, 1, 0)) mask = mask.filter(growFilter) provinceColorImage = provinceColorImage.crop(box) blackImage = ImageChops.constant(provinceColorImage, (0, 0, 0)) provinceColorImage = Image.composite(blackImage, provinceColorImage, mask) borderColors = provinceColorImage.getcolors() result = [color for (count, color) in borderColors]
def ocr( x_start: int, y_start: int, x_end: int, y_end: int, debug: bool = False, bmp: Image = None, cropb: bool = False, ocr_filter: bool = True, binf: int = 0, sliced: bool = False ) -> str: """Perform an OCR of the supplied area, returns a string of the result. Keyword arguments debug -- Saves an image of what is sent to the OCR (default False) bmp -- A bitmap from the get_bitmap() function, use this if you're performing multiple different OCR-readings in succession from the same page. This is to avoid to needlessly get the same bitmap multiple times. If a bitmap is not passed, the function will get the bitmap itself. (default None) cropb -- Whether the bmp provided should be cropped. filter -- Whether to filter the image for better OCR. binf -- Threshold value for binarizing filter. Zero means no filtering. sliced -- Whether the image has ben sliced so there's very little blank space. Gets better readings from small values for some reason. """ x_start += Window.x x_end += Window.x y_start += Window.y y_end += Window.y if bmp is None: bmp = Inputs.get_cropped_bitmap(x_start, y_start, x_end, y_end) elif cropb: # Bitmaps are created with a 8px border bmp = bmp.crop((x_start + 8, y_start + 8, x_end + 8, y_end + 8)) if binf > 0: # Binarizing Filter fn = lambda x: 255 if x > binf else 0 bmp = bmp.convert('L') # To Monochrome bmp = bmp.point(fn, mode='1') if debug: bmp.save("debug_ocr_whiten.png") if ocr_filter: # Resizing and sharpening *_, right, lower = bmp.getbbox() bmp = bmp.resize((right * 4, lower * 4), Image.BICUBIC) # Resize image bmp = bmp.filter(ImageFilter.SHARPEN) if debug: bmp.save("debug_ocr_filter.png") if sliced: s = pytesseract.image_to_string(bmp, config='--psm 6') else: s = pytesseract.image_to_string(bmp, config='--psm 4') return s
def adjust_image(self, img: Image) -> Image: # arr = ImageFilter().filter(img) # img = Image.fromarray(arr) width, height = img.size if height > self.HEIGHT: width, height = int(width * self.HEIGHT / height), self.HEIGHT img = img.resize((width, height)) elif height < self.HEIGHT: canvas = Image.new('L', (width, self.HEIGHT), ImageFilter.BG) canvas.paste(img, img.getbbox()) img = canvas return img
def rectangle_highlighted(image: Image, coordinates: tuple) -> bool: """If the bounding box of the image overlaps with the button coordinates, it's likely to be a button 3d effect. """ image_coordinates = image.getbbox() if image_coordinates is not None: image_coordinates = absolute_coordinates(image_coordinates, coordinates) return similar_rectangles(image_coordinates, coordinates) return False
def adjust_image(self, img: Image) -> Image: orig_img = img arr = self.image_filter.filter(img) img = Image.fromarray(arr) # arr = ImageFilter().filter(img) # img = Image.fromarray(arr) width, height = img.size if height > self.in_height: width, height = int(width * self.in_height / height), self.in_height img = img.resize((width, height)) elif height < self.in_height: canvas = Image.new('L', (width, self.in_height), ImageFilter.BG) try: canvas.paste(img, img.getbbox()) except: raise img = canvas return img
def autocrop_image(image: Image, border=0) -> Image: """Crop empty space from PIL image Parameters ---------- image : Image PIL image to crop border : int, optional scale border outwards, by default 0 Returns ------- Image Cropped image """ bbox = image.getbbox() image = image.crop(bbox) (width, height) = image.size width += border * 2 height += border * 2 cropped_image = Image.new("RGBA", (width, height), (0, 0, 0, 0)) cropped_image.paste(image, (border, border)) return cropped_image
def cut_image(img: Image): left, upper, right, lower = img.getbbox() return img.crop((0, upper, img.width, lower))
def trim_transparency(img: Image) -> Image: img = img.convert("RGBA") return img.crop(img.getbbox())
def unpad_bbox(img: PIL.Image): img = img.convert('RGB') return img.getbbox()