Skip to content

Instantly share code, notes, and snippets.

@ChrisHinde
Last active August 21, 2023 14:43
Show Gist options
  • Save ChrisHinde/b56d6ccbc13f4f67ab27758e53a5394d to your computer and use it in GitHub Desktop.
Save ChrisHinde/b56d6ccbc13f4f67ab27758e53a5394d to your computer and use it in GitHub Desktop.
Image texture generators for Array Modifier "instance ID"
# imggen_randompatches.py
#
# Python script to generate an image with greyscale "patches"
# from black (0.0 / 0) to white (1.0 / 255) in random order
#
# CC-BY Chris Hindefjord - chris.hindefjord.se - 2023-08
#
# Tested with Python 3.9.2
# NB: Requires the PIL/Pillow library!
#
# Usage:
# Change the settings below to match the grid that you want.
# Two things to note:
# * The image doesn't need to be high resolution,
# but I probably wouldn't go bellow 500px (or 30px per patch)
# * If you change the number of columns (& rows) so the total number of patches
# will be larger than 256 you WILL get duplicate values.
# (NB: Since each patch is individually randomized, there is no guarantee
# that you will get an image where each possible value [0-255] is
# represented, even if you have 256 patches)
#
# Run:
# python3 imggen_randompatches.py
#
width, height = 1024, 1024
columns = 32
rows = columns # It is recommended to have the same amount of rows as columns,
# but you can change that if you want.
file_prefix = 'img_randompatches_'
#################################################
# You are advised not to edit anything bellow. #
# If you do, you risk breaking the script! #
#################################################
from PIL import Image, ImageDraw
import random
maxcolor = 255
maxsquare = columns * rows
square_side_x = width/columns
square_side_y = height/rows
filename = file_prefix + str(columns) + 'x' + str(rows) + '.png'
img = Image.new("RGB", (width, height))
img1 = ImageDraw.Draw(img)
for y in range(0, columns):
for x in range(0, columns):
c = random.randint(0, 255)
color = (c, c, c)
shape = [(x*square_side_x, y*square_side_y), ((x+1)*square_side_x, (y+1)*square_side_y)]
img1.rectangle(shape, fill = color)
img.save(filename)
print("Generated image saved to " + filename)
# imggen_sequence.py
#
# Python script to generate an image with greyscale "patches"
# from black (0.0 / 0) to white (1.0 / 255)
#
# CC-BY Chris Hindefjord - chris.hindefjord.se - 2023-08
#
# Tested with Python 3.9.2
# NB: Requires the PIL/Pillow library!
#
# Usage:
# Change the settings below to match the grid that you want.
# Two things to note:
# * The image doesn't need to be high resolution,
# but I probably wouldn't go bellow 500px (or 30px per patch)
# * If you change the number of columns (& rows) so the total patches
# will be larger than 256 it will result in an image with repeated
# value (being 1.0 or 'white')
#
# Run:
# python3 imggen_sequence.py
#
width, height = 1024, 1024
columns = 16
rows = columns # It is recommended to have the same amount of rows as columns,
# but you can change that if you want.
file_prefix = 'img_sequencegrid_'
#################################################
# You are advised not to edit anything bellow. #
# If you do, you risk breaking the script! #
#################################################
from PIL import Image, ImageDraw
maxcolor = 256
maxsquare = rows * columns
square_side_x = width/columns
square_side_y = height/rows
filename = file_prefix + str(columns) + 'x' + str(rows) + '.png'
if maxsquare > maxcolor:
print("WARNING: The number of patches (%d) is larger than the maximal color value (%d)!" % (maxsquare, maxcolor))
print(" The resulting image might not be what you expected (containing a big 'white' section)\n")
img = Image.new("RGB", (width, height))
img1 = ImageDraw.Draw(img)
square = 0
for y in range(0, columns):
for x in range(0, rows):
c = square
color = (c, c, c)
shape = [(x*square_side_x, y*square_side_y), ((x+1)*square_side_x, (y+1)*square_side_y)]
img1.rectangle(shape, fill = color)
square += 1
img.save(filename)
print("Generated image saved to " + filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment