def get_card(self): card = super().get_card() draw = ImageDraw.Draw(card) font = get_font(20) size = draw.textsize(self.title, font) draw.text(((card.size[0] - size[0]) / 2, (card.size[1] / 2 - size[1])), self.title, fill=getrgb("white"), font=font, align="center") font = get_font(15) size = draw.textsize(self.subtitle, font) draw.text( ((card.size[0] - size[0]) / 2, card.size[1] / 2 + size[1] * 0.5), self.subtitle, fill=getrgb("white"), font=font, align="center") if self.is_upside_down: card = card.rotate(180) return card
def blendified_color(self, div=1, sub=0): color = list(getrgb(self.color)) for i in range(len(color)): color[i] /= div for i in range(len(color)): color[i] = max(color[i] - sub, 0) for i in range(len(color)): color[i] = int(color[i]) return tuple(color)
def write_logo(self, card): draw = ImageDraw.Draw(card) text = "Stack Overflow" W, H = mm_to_px(self.size()) font = get_font(20) w, h = draw.textsize(text, font) draw.text(((W - w) // 2, (H - h) // 2), text, font=font, fill=getrgb(self.color))
def get_card(self): card = super().get_card() draw = ImageDraw.Draw(card) if self.has_title(): font = get_font(self.title_size) colors = get_source_code_coloring(self.title) for color in colors: draw.text((mm_to_px(10), self.top), colors[color], font=font, fill=getrgb(color)) height = draw.textsize("A", font)[1] self.top += height * 2.05 if self.has_title() or not self.should_resize: font = get_font(self.text_size) colors = get_source_code_coloring(self.text) for color in colors: draw.text((mm_to_px(10), self.top), colors[color], font=font, fill=getrgb(color), spacing=mm_to_px(.8)) else: sc_size = get_source_code_position_n_size(card, self.text, draw) font = get_font(sc_size) w, h = draw.textsize(self.text, font, spacing=mm_to_px(0.6)) colors = get_source_code_coloring(self.text) for color in colors: draw.text( (mm_to_px(10), (card.size[1] - h) // 2 - mm_to_px(2)), colors[color], font=font, fill=getrgb(color), spacing=mm_to_px(0.8)) if self.is_upside_down: card = card.rotate(180) return card
def box_draw(self, card, move, picture=None, color=None): assert picture is not None or color is not None box_size = mm_to_px((self.width - move, self.width - move)) if picture is None: picture = Image.new('RGB', box_size, getrgb(color)) picture = picture.crop(( 0, 0, ) + box_size) card.paste(picture, mm_to_px(move / 2, move / 2))
def get_card(self): source_code = get_source_code(self.rule) colors = get_source_code_coloring(source_code) card = super().get_card() draw = ImageDraw.Draw(card) sc_size = get_source_code_position_n_size(card, source_code, draw) font = get_font(sc_size) W, H = mm_to_px(self.size()) w, h = draw.textsize(source_code, font) for color in colors: draw.text((mm_to_px(10), (H - h) // 2 - mm_to_px(1)), colors[color], font=font, fill=getrgb(color), spacing=mm_to_px(0.8)) return card
def get_card(self): value = self.value[1] card = super().get_card() draw = ImageDraw.Draw(card) font = get_font(50) W, H = mm_to_px(self.size()) w, h = draw.textsize(value, font) if value[0] in '-': w += draw.textsize('-', font)[0] colors = get_source_code_coloring(value) for color in colors: draw.text(((W - w) // 2, (H - h) // 2 - mm_to_px(1)), colors[color], font=font, fill=getrgb(color), spacing=mm_to_px(0.8)) return card
def get_round_rectangle(size, color, radius): if isinstance(color, str): color = getrgb(color) if not isinstance(color, tuple): color = tuple(color) rectangle = Image.new('RGBA', mm_to_px(size), (*color, 0)) draw = ImageDraw.Draw(rectangle) draw.rectangle(mm_to_px(radius / 2, 0, size[0] - (radius / 2), size[1]), fill=color) draw.rectangle(mm_to_px(0, radius / 2, size[0], size[1] - (radius / 2)), fill=color) draw.ellipse(mm_to_px(0, 0, radius, radius), fill=color) draw.ellipse(mm_to_px(size[0] - radius, 0, size[0], radius), fill=color) draw.ellipse(mm_to_px(0, size[1] - radius, radius, size[1]), fill=color) draw.ellipse(mm_to_px(size[0] - radius, size[1] - radius, size[0], size[1]), fill=color) return rectangle
def generate_box(name): with PdfWriter(name) as f: extra_space_around_cards = 1 extra_space_because_of_how_box_is_folded = 2 for diff in [0, 1]: w = mm_to_px(Card.base_width + diff + extra_space_around_cards) l = mm_to_px((Card.base_height + diff) * 2 + extra_space_around_cards + extra_space_because_of_how_box_is_folded) h = mm_to_px(22) mx = max(w // 2, h * 2) mn = min(w // 2, h * 2) margin = mm_to_px(30) for i in range(2): cropping = mm_to_px(2) if i == 0 else 0 page = Image.new("RGB", mm_to_px(210, 297), (255, 255, 255)) draw = ImageDraw.Draw(page) color = getrgb("black") if diff == 0 else getrgb("true_black") draw.rectangle( ((margin - cropping, margin + mx - w // 2 - cropping), (margin + h + w + h + cropping, margin + mx + l + w // 2 + cropping)), color) draw.rectangle( ((margin + h - cropping, margin + mx - h * 2 - cropping), (margin + h + w + cropping, margin + mx + l + h * 2 + cropping)), color) if i == 0 and diff == 1: card = BackCard(px_to_mm(w), px_to_mm(l), True, "orange").get_card() page.paste(card, (margin + h, margin + mx), mask=card) if i == 1: line_color = getrgb("white") line_width = mm_to_px(.05) draw.line(( (margin + h, margin + mx + l + mn), (margin + h, margin + mx + l), ), line_color, line_width) draw.line(( (margin + h + w, margin + mx + l), (margin + h + w, margin + mx + l + mn), ), line_color, line_width) draw.line(( (margin + h + w, margin + mx - mn), (margin + h + w, margin + mx), ), line_color, line_width) draw.line(( (margin + h, margin + mx - mn), (margin + h, margin + mx), ), line_color, line_width) dashed_line(draw, ((margin + h, margin + mx - h), (margin + h + w, margin + mx - h)), line_color, line_width) dashed_line(draw, ((margin, margin + mx), (margin + h, margin + mx)), line_color, line_width) dashed_line(draw, ((margin + h, margin + mx), (margin + h + w, margin + mx)), line_color, line_width) dashed_line(draw, ((margin + h + w, margin + mx), (margin + h + w + h, margin + mx)), line_color, line_width) dashed_line(draw, ((margin, margin + mx + l), (margin + h, margin + mx + l)), line_color, line_width) dashed_line(draw, ((margin + h, margin + mx + l), (margin + h + w, margin + mx + l)), line_color, line_width) dashed_line(draw, ((margin + h + w, margin + mx + l), (margin + h + w + h, margin + mx + l)), line_color, line_width) dashed_line(draw, ((margin + h, margin + mx + l + h), (margin + h + w, margin + mx + l + h)), line_color, line_width) dashed_line(draw, ((margin + h, margin + mx), (margin + h, margin + mx + l)), line_color, line_width) dashed_line(draw, ((margin + h + w, margin + mx), (margin + h + w, margin + mx + l)), line_color, line_width) if i == 1: page = page.transpose(FLIP_LEFT_RIGHT) f.write(page)
def get_play( fns_count, values_count, input_value_index, output_value_index, used_fn_indexes, front_hand_count, right_hand_count, front_played_count=0, right_played_count=0, add_thumb=False ): background = ImageColor.getrgb(color_codes["lighter_black"]) base = Image.new("RGB", mm_to_px(87 - PictureHelpCard.DX, 87 - PictureHelpCard.DX), (*background,)) x1 = 30 x2 = 230 y1 = 30 y2 = 100 table = get_play_board() table.putalpha(100) table = transform_play_board(table, 0) base.paste(table, mask=table) blank = get_play_board() fn = get_play_board() fn_card = PlayingCardBack("yellow").get_card() fn.paste(fn_card, mm_to_px(x2, y1), fn_card) for i in range(fns_count): transformed = transform_play_board(fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(fn, i) base.paste(transformed, mask=transformed) value = get_play_board() value_card = PlayingCardBack("blue").get_card() value.paste(value_card, mm_to_px(x1, y1), value_card) for i in range(values_count): transformed = transform_play_board(value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(value, i) base.paste(transformed, mask=transformed) used_value = get_play_board() used_value_card = ValueCard("green", values["green"][0]).get_card() used_value.paste(used_value_card, mm_to_px(x1, y2), used_value_card) for i in range(14 - values_count - 1): transformed = transform_play_board(used_value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(used_value, i) base.paste(transformed, mask=transformed) if input_value_index is not None: input_value = get_play_board() input_value_card = ValueCard("green", values["green"][input_value_index]).get_card() input_value.paste(input_value_card, mm_to_px(x1, y2), input_value_card) i = 14 - values_count - 1 transformed = transform_play_board(input_value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(input_value, i) base.paste(transformed, mask=transformed) if output_value_index is not None: output_value = get_play_board() output_value_card = ValueCard("green", values["green"][output_value_index]).get_card() output_value.paste(output_value_card, mm_to_px(x2, y2), output_value_card) i = 0 transformed = transform_play_board(output_value, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(output_value, i) base.paste(transformed, mask=transformed) for i in range(right_hand_count): right_hand = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) right_hand_tmp = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) right_hand_card = PlayingCardBack("yellow").get_card() right_hand_tmp.paste(right_hand_card, mm_to_px(50, 50), right_hand_card) # right_hand_tmp = resize(right_hand_tmp, (2 * right_hand_tmp.size[0], 2 * right_hand_tmp.size[1])) right_hand_tmp = rotate(right_hand_tmp, 13 + (-i - 1) * 8, expand=False, center=mm_to_px((Card.base_width + 50, 50))) right_hand.paste(right_hand_tmp, mm_to_px(0, 80), right_hand_tmp) right_hand = rotate(right_hand, 180) coeffs = find_coefficients( [ (mm_to_px(140), mm_to_px(20)), (right_hand.size[0] - mm_to_px(20), mm_to_px(40)), (right_hand.size[0] - mm_to_px(20), right_hand.size[1] - mm_to_px(130)), (mm_to_px(160), right_hand.size[1] - mm_to_px(140)) ], [(0, 0), (right_hand.size[0], 0), (right_hand.size[0], right_hand.size[1]), (0, right_hand.size[1])] ) right_hand = transform(right_hand, right_hand.size, Image.PERSPECTIVE, coeffs) scale = 1.5 right_hand = resize(right_hand, (int(right_hand.width / scale), int(right_hand.height / scale))) base.paste(right_hand, mm_to_px(-110 / scale + 23, -30 / scale + 2), mask=right_hand) if add_thumb: thumb = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) thumb_tmp = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) thumb_card = Image.open("help/arrows/thumb.png").convert("RGBA") scale = 3. * 80 / ANTIALIASING / RESOLUTION_DPI thumb_card = resize(thumb_card, (int(thumb_card.width / scale), int(thumb_card.height / scale))) thumb_card = rotate(thumb_card, 180) color = getrgb("grey") for x in range(thumb_card.width): for y in range(thumb_card.height): thumb_card.putpixel((x, y), (*color, thumb_card.getpixel((x, y))[3])) thumb_tmp.paste(thumb_card, mm_to_px(70, 50), thumb_card) thumb_tmp = rotate(thumb_tmp, 13, expand=False, center=mm_to_px((Card.base_width + 50, 50))) thumb.paste(thumb_tmp, mm_to_px(0, 80), thumb_tmp) thumb = rotate(thumb, 180) coeffs = find_coefficients( [ (mm_to_px(140), mm_to_px(20)), (thumb.size[0] - mm_to_px(20), mm_to_px(40)), (thumb.size[0] - mm_to_px(20), thumb.size[1] - mm_to_px(130)), (mm_to_px(160), thumb.size[1] - mm_to_px(140)) ], [(0, 0), (thumb.size[0], 0), (thumb.size[0], thumb.size[1]), (0, thumb.size[1])] ) thumb = transform(thumb, thumb.size, Image.PERSPECTIVE, coeffs) scale = 1.5 thumb = resize(thumb, (int(thumb.width / scale), int(thumb.height / scale))) base.paste(thumb, mm_to_px(-110 / scale + 23, -30 / scale + 2), mask=thumb) for i in range(len(used_fn_indexes)): used_fn = get_play_board() used_fn_card = FunctionCard("green", get_rules()["green"][used_fn_indexes[i]]).get_card() used_fn_card = rotate(used_fn_card, randint(0, 360)) used_fn.paste(used_fn_card, mm_to_px(x1 / 2 + x2 / 2 - 5, y2 + 30), used_fn_card) transformed = transform_play_board(used_fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(used_fn, i) base.paste(transformed, mask=transformed) for i in range(front_played_count): i += 1 front_player_fn = get_play_board() front_player_fn_card = PlayingCardBack("yellow").get_card() front_player_fn_card = rotate(front_player_fn_card, 97 + 10 * -i) front_player_fn.paste(front_player_fn_card, mm_to_px(x1 - 10 + 2 * i + randint(-3, 3), 200 + 20 * i + randint(-3, 3)), front_player_fn_card) transformed = transform_play_board(front_player_fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(front_player_fn, i) base.paste(transformed, mask=transformed) for i in range(right_played_count): i += 1 right_player_fn = get_play_board() right_player_fn_card = PlayingCardBack("yellow").get_card() right_player_fn_card = rotate(right_player_fn_card, 51 + 8 * -i) right_player_fn.paste(right_player_fn_card, mm_to_px(x2 + 20 + 8 * i + randint(-3, 3), 300 - 20 * i + randint(-3, 3)), right_player_fn_card) transformed = transform_play_board(right_player_fn, i - 0.5) base.paste(blank, mask=transformed) transformed = transform_play_board(right_player_fn, i) base.paste(transformed, mask=transformed) for i in range(front_hand_count): front_hand = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) front_hand_tmp = Image.new("RGBA", mm_to_px(200, 200), (0, 0, 0, 0)) front_hand_card = ValueCard("green", values["green"][0]).get_card() front_hand_tmp.paste(front_hand_card, mm_to_px(50, 50), front_hand_card) # front_hand_tmp = resize(front_hand_tmp, (2 * front_hand_tmp.size[0], 2 * front_hand_tmp.size[1])) front_hand_tmp = rotate(front_hand_tmp, -2 + (-i - 1) * 5, expand=False, center=mm_to_px((Card.base_width + 50, Card.base_height + 50))) front_hand.paste(front_hand_tmp, mm_to_px(0, 0), front_hand_tmp) coeffs = get_main_plane_coeffs(front_hand.size, .9, i) front_hand = transform(front_hand, front_hand.size, Image.PERSPECTIVE, coeffs) scale = 2 front_hand = resize(front_hand, (int(front_hand.width / scale), int(front_hand.height / scale))) base.paste(front_hand, mm_to_px(-12, 46), mask=front_hand) return base