(Aug-28-2017, 04:47 PM)nilamo Wrote: Do you have a sample image, and what the output would be (for those of us that want to see what's up without needing to run it)?
Good idea
INPUT
Input image
Init values
# Init file = '/Users/mathieu/Desktop/Fond Salle/fond salle 3.png' results_dir = '/Users/mathieu/Desktop/Fond Salle/final' min_percentage = 0.5 # min percentage of color in the file to be added alpha_pixels = False # I don't want to process the transparent background circle_size = 200 # circle file size (pixels) color_files = True # results with image files results_file = True # json results results_color_format = 'hex' # hex color format results_ext = '.json'
OUTPUT
Results preview
Pie chart
Json file
[{"color": "#eff8ff", "num": 27.855486948809506}, {"color": "#ffb009", "num": 17.40053298739901}, {"color": "#3b2802", "num": 13.558536465017378}, {"color": "#0affe8", "num": 9.815141346250764}, {"color": "#efcc8f", "num": 4.208820734803976}, {"color": "#f74a00", "num": 3.6577831216158483}, {"color": "#000000", "num": 2.9170917393700897}, {"color": "#ffdda1", "num": 2.2327861897305246}, {"color": "#f7e0ba", "num": 2.0723164118198536}, {"color": "#ef4c20", "num": 1.5319486682273635}, {"color": "#668eaf", "num": 1.2689553607238304}, {"color": "#08ccba", "num": 1.2210042076398797}, {"color": "#b0d4ff", "num": 1.1766799667504932}, {"color": "#f9f212", "num": 0.9418584888512515}, {"color": "#87aacc", "num": 0.8144526546677727}, {"color": "#c49257", "num": 0.6270762490811471}, {"color": "#058074", "num": 0.5094968165410814}]
(Aug-28-2017, 04:47 PM)nilamo Wrote: Line 81:print "Done."
That would throw an error in python3.
Thanks! 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: rgba_pixel = eval(str(color)) if color_files == True: img = Image.new('RGBA', (100, 100), (rgba_pixel[0],rgba_pixel[1],rgba_pixel[2],rgba_pixel[3])) file_name = '%03.4f %%.png' % color_percentage img.save(os.path.join(results_dir,file_name),format="png") sorted_colors.append({'color':rgba_pixel,'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: rgba_pixel = eval(str(x['color'])) 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=(rgba_pixel[0],rgba_pixel[1],rgba_pixel[2],rgba_pixel[3])) current_deg += pieslice_deg if results_file == True: if results_color_format == 'hex': x['color'] = '#%02x%02x%02x' % (rgba_pixel[0],rgba_pixel[1],rgba_pixel[2]) elif results_color_format == 'rgb': x['color'] = (rgba_pixel[0],rgba_pixel[1],rgba_pixel[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.")