# MaskMaker create_image_with_grid Part of the [[distillery_maskmaker]] ```python @classmethod async def create_image_with_grid(cls, image_BytesIO_input, grid_size): # Create an image with a grid overlay of size grid_size x grid_size, and mark each cell with coordinates. try: image_object = Image.open(image_BytesIO_input) draw = ImageDraw.Draw(image_object) width, height = image_object.size num_rows, num_cols = grid_size, grid_size # Calculate the size of each cell to dynamically fit the text in it cell_width = width / num_cols cell_height = height / num_rows # Draw the grid for i in range(1, num_rows): y = (height / num_rows) * i draw.line([(0, y), (width, y)], fill="purple", width=1) for i in range(1, num_cols): x = (width / num_cols) * i draw.line([(x, 0), (x, height)], fill="purple", width=1) # Load our font scaling_factor = 0.35 font_size = int(cell_height * scaling_factor) font = ImageFont.truetype("assets/SpaceMono-Bold.ttf", font_size) # Add coordinates to the center of each cell for row_idx in range(num_rows): for col_idx in range(num_cols): # Calculate the center of the cell x_center = (width / num_cols) * (col_idx + 0.5) y_center = (height / num_rows) * (row_idx + 0.5) # Create the coordinate label label = f'{chr(97 + col_idx)}{row_idx + 1}' # Get text bounding box bbox = draw.textbbox((0, 0), label, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1] # Draw the text draw.text((x_center - text_width / 2, y_center - text_height / 2), label, font=font, fill="purple") image_BytesIO_output = io.BytesIO() image_object.save(image_BytesIO_output, format='PNG') image_BytesIO_output.seek(0) return image_BytesIO_output except Exception as e: raise ``` ## Overview The `create_image_with_grid` method is a class method in the [[distillery_maskmaker]]. It's responsible for creating a new image with a grid overlay and labeled coordinates. ## Functionality The method performs the following tasks: - Opens the input image - Draws a grid on the image based on the specified grid size - Calculates appropriate font size for cell labels - Adds coordinate labels to the center of each grid cell - Returns the resulting image as a BytesIO object ## Key Implementation Points 1. It uses [[PIL]] (Python Imaging Library) for image drawing and manipulation. 2. It calculates cell dimensions based on the image size and grid size. 3. It draws purple lines to create the grid. 4. It uses a custom font (`SpaceMono-Bold.ttf`) for the coordinate labels. 5. It calculates the center of each cell to position the labels. 6. It uses alphabetic characters for columns and numbers for rows in the labels. ## Explanation for Intermediate Users This method is like creating a customized graph paper overlay for an image: 1. It starts with your original image as the background. 2. It then draws a grid over it, like lines on graph paper. 3. For each 'square' in this grid, it adds a label (like 'A1', 'B2', etc.) in the center. 4. The labels are sized to fit nicely within each cell. 5. The result is your original image with a clear, labeled grid overlay. This grid overlay is useful for users to easily reference specific areas of an image using coordinates. It's particularly helpful when used in conjunction with the [[MaskMaker render_mask_on_image]] method, as users can specify areas to mask using these grid coordinates.