|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import hashlib |
| 5 | +from PIL import Image, ImageDraw |
| 6 | + |
| 7 | +__author__ = "Lorena Mesa" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +class Identicon: |
| 12 | + |
| 13 | + def __init__(self, input_str: str) -> None: |
| 14 | + self.md5hash_str: str = self._convert_string_to_sha_hash(input_str) |
| 15 | + |
| 16 | + def _convert_string_to_sha_hash(self, input_str: str) -> str: |
| 17 | + """ |
| 18 | + Function that takes an input string and returns a md5 hexdigest string. |
| 19 | +
|
| 20 | + :return: md5 hexdigest of an input string |
| 21 | + """ |
| 22 | + if len(input_str) < 1: |
| 23 | + raise ValueError("Input string cannot be empty.") |
| 24 | + |
| 25 | + return hashlib.md5(input_str.encode('utf-8')).hexdigest() |
| 26 | + |
| 27 | + def _build_grid(self) -> list[list]: |
| 28 | + """ |
| 29 | + Function that takes an input md5 hexdigest string and builds |
| 30 | + a list of lists using grid size to determine the size of the |
| 31 | + grid. Each value within the list of lists contains a row of booleans |
| 32 | + that indicates if that given element will be filled with a color. |
| 33 | +
|
| 34 | + :return: a list of lists representing a grid of the pixels to be drawn in a PIL Image |
| 35 | + """ |
| 36 | + grid_size: int = 5 |
| 37 | + grid: list = [] |
| 38 | + for row_number in range(grid_size): |
| 39 | + row: list = list() |
| 40 | + for element_number in range(grid_size): |
| 41 | + element: int = row_number * grid_size + element_number + 6 |
| 42 | + fill_element: bool = int(self.md5hash_str[element], base=16) % 2 == 0 |
| 43 | + row.append(fill_element) |
| 44 | + grid.append(row) |
| 45 | + return grid |
| 46 | + |
| 47 | + def _generate_image_fill_color(self, md5hash_str: str) -> tuple: |
| 48 | + """ |
| 49 | + Function that generates a R,G,B value to use to fill the PIL Image. |
| 50 | +
|
| 51 | + :param md5hash_str: md5 hexdigest of an input string |
| 52 | + :return: a tuple of numbers representing the R,G.B value to fill the PIL Image |
| 53 | + """ |
| 54 | + return tuple(int(md5hash_str[i:i+2], base=16) for i in range(0, 2*3, 2)) |
| 55 | + |
| 56 | + def draw_image(self, filename: str=None) -> Image: |
| 57 | + """ |
| 58 | + Function that generates a grid - a list of lists - indicating which pixels are to be filled |
| 59 | + and uses the md5hash_str to generate an image fill color. Function creates a PIL Image, drawing it, |
| 60 | + and saving it. |
| 61 | +
|
| 62 | + :param filename: filename of PIL png image generated |
| 63 | + :return: None |
| 64 | + """ |
| 65 | + |
| 66 | + fill_color: tuple = self._generate_image_fill_color(self.md5hash_str) |
| 67 | + grid: list[list] = self._build_grid() |
| 68 | + |
| 69 | + SQUARE: int = 50 |
| 70 | + size: tuple = (5 * 50, 5 * 50) |
| 71 | + bg_color: tuple = (214,214,214) |
| 72 | + |
| 73 | + image: Image = Image.new("RGB", size, bg_color) |
| 74 | + draw: ImageDraw = ImageDraw.Draw(image) |
| 75 | + |
| 76 | + # Makes the identicon symmetrical |
| 77 | + for i in range(5): |
| 78 | + grid[i][4] = grid[i][0] |
| 79 | + grid[i][3] = grid[i][1] |
| 80 | + |
| 81 | + for row in range(5): |
| 82 | + for element in range(5): |
| 83 | + # Boolean check to confirm 'True' to draw and fill the pixel in the iamge |
| 84 | + if grid[row][element]: |
| 85 | + bounding_box: list[int] = [element * SQUARE, row * SQUARE, element * SQUARE + SQUARE, row * SQUARE + SQUARE] |
| 86 | + # TODO: Should we use multiple fill colors? May need to draw multiple rectangles to obtain this |
| 87 | + draw.rectangle(bounding_box, fill=fill_color) |
| 88 | + |
| 89 | + if not filename: |
| 90 | + filename: str = 'example' |
| 91 | + |
| 92 | + # TODO: Confirm overwrite file is one of same name exists |
| 93 | + image.save(f'{filename}.png') |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + parser = argparse.ArgumentParser( |
| 97 | + description="Generate an identicon with Python 3.", |
| 98 | + usage="""Example: python main.py -s='931D387731bBbC988B31220' or add the optional -o flag to specify name of identicon |
| 99 | + image generated such as python main.py -s='931D387731bBbC988B31220' -o='my_identicon.jpg'.""" |
| 100 | + ) |
| 101 | + |
| 102 | + def len_gt_zero(input_str: str): |
| 103 | + if len(input_str) > 0: |
| 104 | + return input_str |
| 105 | + raise argparse.ArgumentTypeError("Input string must have length greater than 0 in order to generate an identicon.") |
| 106 | + |
| 107 | + parser.add_argument( |
| 108 | + "-s", |
| 109 | + "--string", |
| 110 | + default="", |
| 111 | + type=str, |
| 112 | + required=True, |
| 113 | + help="An input string used to generate an identicon.", |
| 114 | + ) |
| 115 | + parser.add_argument( |
| 116 | + "-o", |
| 117 | + "--output", |
| 118 | + default="", |
| 119 | + type=str, |
| 120 | + required=False, |
| 121 | + help="Name for output identicon image generated.", |
| 122 | + ) |
| 123 | + |
| 124 | + args = parser.parse_args() |
| 125 | + |
| 126 | + identicon = Identicon(input_str=args.string) |
| 127 | + identicon.draw_image(filename=args.output) |
0 commit comments