Aug-29-2017, 08:40 AM
(Aug-28-2017, 09:58 PM)nilamo Wrote: Are you skipping black or something? That looks like it should be the #1 color, instead of the cloud/off-white.My image hosting convert transparent pixels to black and since alpha_pixels = False, it's skipping them.
(Aug-28-2017, 09:58 PM)nilamo Wrote: Unless I'm reading it wrong, color is already a tuple of int, so converting it to a string and calling eval() on it will result with... exactly the same thing you started with.You right thanks it was an old test.
Also do you know which is the better ? Use an hash map to store the color (key) and it's image number...
for rgba_pixel in PIL_file.getdata(): if rgba_pixel[3] == 0: if alpha_pixels == True: rgba_pixel = (0, 0, 0, 0) else: all_pixels -= 1 continue try: nb = colors_hm[rgba_pixel]['nb'] colors_hm[rgba_pixel] = {'nb':nb + 1} except: colors_hm[rgba_pixel] = {'nb':1}and then add only colors under the min_percentage into a list and sort it.
Or use only a list, makes severals test to update theses values like the hash map, delete colors below min_percentage and just sort it after ? Is it worth it ?
Current new code:
#!/usr/bin/env python #-*- coding: utf-8 -*- import os import json from PIL import Image from PIL import ImageDraw # Init file = '/path/to/your/file.png' results_dir = '/path/to/results/dir' min_percentage = 0.5 # min percentage of color in the file to be added alpha_pixels = True # use transparent pixels circle_size = 500 # circle file size (pixels) color_files = True # get results with image files results_file = True # get json results results_color_format = 'hex' # hex / rgb / rgba results_ext = '.json' PIL_file = Image.open(file) all_pixels = PIL_file.size[0] * PIL_file.size[1] colors_hm = {} sorted_colors = [] if not os.path.isdir(results_dir): os.makedirs(results_dir) for rgba_pixel in PIL_file.getdata(): if rgba_pixel[3] == 0: if alpha_pixels == True: rgba_pixel = (0, 0, 0, 0) else: all_pixels -= 1 continue try: nb = colors_hm[rgba_pixel]['nb'] colors_hm[rgba_pixel] = {'nb':nb + 1} except: colors_hm[rgba_pixel] = {'nb':1} for color in colors_hm: color_percentage = colors_hm[color]['nb'] * 100 / float(all_pixels) if color_percentage >= min_percentage: if color_files == True: img = Image.new('RGBA', (100, 100), (color[0],color[1],color[2],color[3])) file_name = '%03.4f %%.png' % color_percentage img.save(os.path.join(results_dir,file_name),format="png") sorted_colors.append({'color':color,'num':color_percentage}) sorted_colors.sort(key=lambda k: k['num'],reverse=True) circle = Image.new('RGBA', (circle_size,circle_size), (0,0,0,0)) current_deg = 0 for x in sorted_colors: pieslice_deg = x['num'] * 3.6 ImageDraw.Draw(circle).pieslice([10, 10, circle_size-10, circle_size-10], current_deg, current_deg + pieslice_deg, fill=(x['color'][0],x['color'][1],x['color'][2],x['color'][3])) current_deg += pieslice_deg if results_file == True: if results_color_format == 'hex': x['color'] = '#%02x%02x%02x' % (x['color'][0],x['color'][1],x['color'][2]) elif results_color_format == 'rgb': x['color'] = (x['color'][0],x['color'][1],x['color'][2]) circle.save(os.path.join(results_dir,"circle.png"),format="png") if results_file == True: with open(os.path.join(results_dir,"results"+results_ext), 'w') as outfile: json.dump(sorted_colors, outfile) print("Done.")