Skip to content

Instantly share code, notes, and snippets.

@Ethosa
Created August 12, 2021 06:12
Show Gist options
  • Save Ethosa/12aac2c32ed79430b9c204125d63b7b0 to your computer and use it in GitHub Desktop.
Save Ethosa/12aac2c32ed79430b9c204125d63b7b0 to your computer and use it in GitHub Desktop.
triangulize
# author Ethosa
# add image named "1.jpg" to folder and run it!
from PIL import Image
from random import randint
GRID_KOEF = 65
def triangulize_block(img):
w, h = img.size
i = 0
clr1 = img.getpixel((randint(0, w-1), randint(0, h-1)))
clr2 = img.getpixel((randint(0, w-1), randint(0, h-1)))
for y in range(img.size[1]):
for x in range(img.size[0]):
clr = clr1 if x > i else clr2
img.putpixel((x, y), clr)
i += 1
return img
def triangulize_image(img):
width, height = img.size
grid = width // GRID_KOEF if width > height else height // GRID_KOEF
for x in range(0, width, grid):
for y in range(0, height, grid):
img1 = img.crop((x, y, x+grid, y+grid))
cropped = triangulize_block(img1)
img.paste(cropped, (x, y))
return img
img = Image.open("1.jpg")
img.show()
img = triangulize_image(img)
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment