def pretreate_image(im, image_name): """对图像进行预处理 :param im: 需要预处理的图像 :return: 无 """ threshold = 180 image_binary = image_binaryzation(im, threshold) image_binary_pixels = list(image_binary.getdata()) im_rgb = im.convert('RGB') list_rgb_pixels = list(im_rgb.getdata()) image_rgb_pixels = list_rgb_pixels.copy() width, height = im.size remove_noise(image_rgb_pixels, image_binary_pixels, width, height) im_rgb.putdata(image_rgb_pixels) im_rgb.save(image_name + "_without_noise.jpg") list_pixels_for_statistic = image_rgb_pixels.copy() # 用于统计的像素list,接下来要将背景色像数去掉 remove_background_pixels(list_pixels_for_statistic) # pixels_statistic = {} pixels_statistic_largest_4 = {} # 图中最多的的四种颜色 / The 4 colors with the largest area least_color_value = 0 # 四种颜色中,最小那种颜色的面积 / The area of the color with the minimal area for color in list_pixels_for_statistic: # if color not in pixels_statistic: area = list_pixels_for_statistic.count(color) # pixels_statistic.setdefault(color, area) if len(pixels_statistic_largest_4) < 4: pixels_statistic_largest_4.setdefault(color, area) least_color_key, least_color_value = CustomDataStructureUtil.get_min(pixels_statistic_largest_4) else: if color not in pixels_statistic_largest_4: if area > least_color_value: pixels_statistic_largest_4.pop(least_color_key) pixels_statistic_largest_4.setdefault(color, area) least_color_key, least_color_value = CustomDataStructureUtil.get_min(pixels_statistic_largest_4) print(CustomDataStructureUtil.get_min(pixels_statistic_largest_4)[1]) print(pixels_statistic_largest_4) largest_colors = list(pixels_statistic_largest_4.keys()) image_pixels = image_rgb_pixels.copy() for large_color in largest_colors: get_single_color_image(im_rgb.copy(), image_pixels.copy(), large_color, width, height)