$ python3 generate_grid.py --help
usage: generate_grid.py [-h] --rows ROWS --cols COLS --cell-size CELL_SIZE [--grid-color GRID_COLOR] [--h-padding H_PADDING] [--v-padding V_PADDING] --output OUTPUT
Generate a grid image
options:
-h, --help show this help message and exit
--rows ROWS Number of rows in the grid
--cols COLS Number of columns in the grid
--cell-size CELL_SIZE
Size of each cell in pixels
--grid-color GRID_COLOR
Color of the grid lines
--h-padding H_PADDING
Horizontal padding in pixels
--v-padding V_PADDING
Vertical padding in pixels
--output OUTPUT Output filename for the image
Last active
August 11, 2023 09:25
-
-
Save andriihomiak/46305ff505f3072f70851a63bcb342d8 to your computer and use it in GitHub Desktop.
Grid image generator (customizable color, cell size, page margins, grid size)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from PIL import Image, ImageDraw | |
def create_custom_grid(rows, cols, cell_size, grid_color, h_padding, v_padding, output_filename): | |
# Calculate the width and height of the image | |
width = cols * cell_size + 2 * h_padding | |
height = rows * cell_size + 2 * v_padding | |
# Create a blank white image | |
img = Image.new('RGB', (width, height), 'white') | |
draw = ImageDraw.Draw(img) | |
# Draw the vertical lines | |
for x in range(cols + 1): | |
draw.line([x * cell_size + h_padding, v_padding, x * cell_size + h_padding, height - v_padding], fill=grid_color) | |
# Draw the horizontal lines | |
for y in range(rows + 1): | |
draw.line([h_padding, y * cell_size + v_padding, width - h_padding, y * cell_size + v_padding], fill=grid_color) | |
# Save the image | |
img.save(output_filename) | |
print(f"Image saved as {output_filename}") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Generate a grid image") | |
parser.add_argument("--rows", type=int, required=True, help="Number of rows in the grid") | |
parser.add_argument("--cols", type=int, required=True, help="Number of columns in the grid") | |
parser.add_argument("--cell-size", type=int, required=True, help="Size of each cell in pixels") | |
parser.add_argument("--grid-color", type=str, default="lightgray", help="Color of the grid lines") | |
parser.add_argument("--h-padding", type=int, default=0, help="Horizontal padding in pixels") | |
parser.add_argument("--v-padding", type=int, default=0, help="Vertical padding in pixels") | |
parser.add_argument("--output", type=str, required=True, help="Output filename for the image") | |
args = parser.parse_args() | |
create_custom_grid(args.rows, args.cols, args.cell_size, args.grid_color, args.h_padding, args.v_padding, args.output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pillow==10.0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Image was generated with the following command: