def clustering(size, target, rep0, rep1, rep2, struct, sent=False): width, height = size struct_size = (width - SPACING*2, BUFFER*2) struct_img = slocbox(struct_size, struct) struct_loc = (SPACING, SPACING) # FIXME: Is there a better way to more evenly space these? cluster_size = (int(width // 3.03) - SPACING, height - (struct_loc[1] + struct_size[1] + BUFFER)) cluster_0_loc = (SPACING, height - (cluster_size[1] + BUFFER//2)) cluster_1_loc = (SPACING + cluster_size[0] + cluster_0_loc[0], cluster_0_loc[1]) cluster_2_loc = (SPACING + cluster_size[0] + cluster_1_loc[0], cluster_0_loc[1]) if sent: cluster_0 = sent_box(rep0, cluster_size, target) cluster_1 = sent_box(rep1, cluster_size, target) cluster_2 = sent_box(rep2, cluster_size, target) else: cluster_0 = cluster_box(rep0, cluster_size, target) cluster_1 = cluster_box(rep1, cluster_size, target) cluster_2 = cluster_box(rep2, cluster_size, target) img = Image.new('RGBA', size=size, color=BLANK) draw = ImageDraw.Draw(img) rectround.rectangle(draw, size, fill=WHITE) img.paste(struct_img, struct_loc, struct_img) img.paste(cluster_0, cluster_0_loc, cluster_0) img.paste(cluster_1, cluster_1_loc, cluster_1) img.paste(cluster_2, cluster_2_loc, cluster_2) return img
def cluster_box(rep, size, target, conf_color=None): width, height = size cardinality = rep[0] confidence = rep[1] tweet = rep[2] if not conf_color: conf_color = cluster_color(float(confidence)) base_size = (width, height - BUFFER*2) base = Image.new('RGBA', base_size, color=BLANK) base_draw = ImageDraw.Draw(base) rectround.rectangle(base_draw, base_size, border=BLACK) base_loc = (0, BUFFER*2) tweet_size = (base_size[0] - BUFFER, base_size[1] - BUFFER) tweet_img = render_tweet(tweet, tweet_size) tweet_loc = (BUFFER // 2, BUFFER // 2) base.paste(tweet_img, tweet_loc) proportion = cardinality / len(sample(target)) color = Color(rgb=(conf_color[0] / 255, conf_color[1] / 255, conf_color[2] / 255)) color.luminance *= 0.66 color.saturation *= 0.9 card_loc = ((BUFFER*2, BUFFER // 2), (BUFFER*2 + (proportion * (width - BUFFER*4)), BUFFER*3//2)) rect_color = (int(color.red * 255), int(color.green * 255), int(color.blue * 255)) img = Image.new('RGBA', size=size, color=BLANK) draw = ImageDraw.Draw(img) rectround.rectangle(draw, size, fill=conf_color) draw.rectangle(card_loc, fill=rect_color) img.paste(base, base_loc, base) return img
def summary_box(summary, size): width, height = size base_size = (width, height - BUFFER*3) base = Image.new('RGBA', base_size, color=BLANK) base_draw = ImageDraw.Draw(base) rectround.rectangle(base_draw, base_size, border=BLACK) base_loc = (0, BUFFER*3) summary_size = (base_size[0] - BUFFER*4, base_size[1] - BUFFER*4) summary_img = box(summary, summary_size) summary_loc = (BUFFER*2, BUFFER*2) base.paste(summary_img, summary_loc) notice_size = (width - BUFFER, BUFFER*2) notice_img = box('Here\'s a summary of what people are saying.', notice_size) notice_loc = (BUFFER, BUFFER // 2) img = Image.new('RGBA', size, color=BLANK) draw = ImageDraw.Draw(img) rectround.rectangle(draw, size) img.paste(notice_img, notice_loc) img.paste(base, base_loc, base) return img
def slocbox(size, text): width, height = size text_size = (width - BUFFER*2, height - SPACING) text_img = box(text, text_size) text_loc = (BUFFER, SPACING//2) base = Image.new('RGBA', size, color=BLANK) draw = ImageDraw.Draw(base) rectround.rectangle(draw, size) base.paste(text_img, text_loc, text_img) return base
def graph_box(size): width, height = size sloc_size = (width - BUFFER*2, BUFFER*2) slocbox_0 = slocbox(sloc_size, 'Below the summary are tweets representing different groups. The bar above each tweet reflects the size of the group it represents.') sloc_0 = (BUFFER, 0) cluster_size = (width // 2 - BUFFER, height//2) cluster_bar = colorbar(cluster_color, cluster_size, 'Low representation', 'High representation', 0, 1) cluster_loc = (BUFFER, SPACING*5) sent_size = (width // 2 - BUFFER, height//2) sent_bar = colorbar(sent_color, sent_size, 'Negative sentiment', 'Positive sentiment', -1, 1) sent_loc = (width - (cluster_size[0] + BUFFER), SPACING*5) base = Image.new('RGBA', size, color=BLANK) base_draw = ImageDraw.Draw(base) rectround.rectangle(base_draw, size) base.paste(cluster_bar, cluster_loc, cluster_bar) base.paste(sent_bar, sent_loc, sent_bar) base.paste(slocbox_0, sloc_0, slocbox_0) return base