Last active
May 3, 2024 11:44
-
-
Save ZeroCool5254/c74afd146f73fd1d611eeabdd4d3a7f3 to your computer and use it in GitHub Desktop.
This script will generate an image based on the prompts given. Can either generate an image based on the amount of grids in the x and y axis, or provide a resolution and it will attempt to generate a set amount of grids that fits in the area.
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
from PIL import Image, ImageDraw, ImageFont | |
def generate_grid_image(num_grids_x, num_grids_y, grid_size): | |
image_size = num_grids_x * grid_size, num_grids_y * grid_size | |
image = Image.new('RGB', image_size, color='white') | |
draw = ImageDraw.Draw(image) | |
font_size = 24 | |
font = ImageFont.truetype('arial.ttf', font_size) #replace arial.ttf with any font. The font needs to be in the same folder as this script | |
for i in range(num_grids_x): | |
for j in range(num_grids_y): | |
x0, y0 = i * grid_size, j * grid_size | |
x1, y1 = x0 + grid_size, y0 + grid_size | |
draw.rectangle([x0, y0, x1, y1], outline='black') | |
text = str(j + 1) + chr(65 + i) | |
if i >= 26: | |
text = chr(65 + i // 26 - 1) + chr(65 + i % 26) | |
bbox = draw.textbbox((x0, y0), text, font=font) | |
text_width = bbox[2] - bbox[0] | |
text_height = bbox[3] - bbox[1] | |
draw.text((x0 + (grid_size - text_width) // 2, y0 + (grid_size - text_height) // 2), | |
text, font=font, fill='black') | |
image.save('grid_image.png') | |
def get_user_input(): | |
while True: | |
print("Select an option:") | |
print("A: Generate a grid based on the resolution provided") | |
print("B: Generate a grid based on the amount of grids on the width and height") | |
option = input("Enter your choice (A/B): ").lower() | |
if option == 'a': | |
resolution_width = int(input("Enter the resolution width: ")) | |
resolution_height = int(input("Enter the resolution height: ")) | |
grid_size = int(input("Enter the grid size in pixels: ")) | |
num_grids_x = resolution_width // grid_size | |
num_grids_y = resolution_height // grid_size | |
elif option == 'b': | |
num_grids_x = int(input("Enter the number of grids on the x axis: ")) | |
num_grids_y = int(input("Enter the number of grids on the y axis: ")) | |
grid_size = int(input("Enter the grid size in pixels: ")) | |
image_size = num_grids_x * grid_size, num_grids_y * grid_size | |
print(f"The image size will be: {image_size[0]}x{image_size[1]} pixels.") | |
else: | |
print("Invalid input. Please enter 'A' or 'B'.") | |
continue | |
total_grids = num_grids_x * num_grids_y | |
if option == 'a': | |
print(f"Your resolution ({resolution_width}x{resolution_height}) will generate a total of {total_grids} grids.") | |
print(f"Number of grids on the x axis: {num_grids_x}") | |
print(f"Number of grids on the y axis: {num_grids_y}") | |
elif option == 'b': | |
print(f"A grid size of {grid_size} pixels on {num_grids_x}x{num_grids_y} grid will generate a total of {total_grids} grids.") | |
choice = input("Is this information acceptable? (y/n): ").lower() | |
if choice == 'y': | |
return num_grids_x, num_grids_y, grid_size | |
elif choice == 'n': | |
continue | |
else: | |
print("Invalid choice. Please enter 'y' or 'n'.") | |
def main(): | |
num_grids_x, num_grids_y, grid_size = get_user_input() | |
generate_grid_image(num_grids_x, num_grids_y, grid_size) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment