generated from Eddie/Backend-Boilerplate
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.5 KiB
69 lines
2.5 KiB
import os, random |
|
from PIL import Image |
|
|
|
|
|
light_directory = os.fsencode(str(os.getcwd() + "/planets/light")) |
|
noise_directory = os.fsencode(str(os.getcwd() + "/planets/noise")) |
|
sphere_directory = os.fsencode(str(os.getcwd() + "/planets/sphere")) |
|
|
|
amount_lights = len(os.listdir(light_directory)) |
|
amount_noise = len(os.listdir(noise_directory)) |
|
amount_sphere = len(os.listdir(sphere_directory)) |
|
|
|
def changeImageColor(im, red, green, blue): |
|
# Split into 3 channels |
|
r, g, b, a = im.split() |
|
# Change channel by factor |
|
r = r.point(lambda i: i * red) |
|
g = g.point(lambda i: i * green) |
|
b = b.point(lambda i: i * blue) |
|
# Recombine back to RGB image |
|
result = Image.merge('RGBA', (r, g, b, a)) |
|
return result |
|
|
|
def changePlanetLayerColor(planetLayer): |
|
red = random.uniform(-0.5,1.5) |
|
green = random.uniform(-0.5,1.5) |
|
blue = random.uniform(-0.5,1.5) |
|
#print(red,green,blue) |
|
changedColor = changeImageColor(planetLayer, red, green, blue) |
|
return changedColor |
|
|
|
def blendPlanetParts(light, noise1, noise2, noise3, sphere, alpha): |
|
blended = Image.blend(sphere, light, alpha) |
|
blended = Image.blend(blended, noise1, alpha) |
|
blended = Image.blend(blended, noise2, alpha) |
|
blended = Image.blend(blended, noise3, alpha) |
|
return blended |
|
|
|
def mergePlanetParts(light, noise1, noise2, noise3, sphere): |
|
planet = sphere |
|
planet.paste(light, (0, 0), light) |
|
planet.paste(noise1, (0, 0), noise1) |
|
planet = Image.blend(planet, noise2, 0.1) |
|
planet = Image.blend(planet, noise3, 0.1) |
|
return planet |
|
|
|
|
|
def createRandomPlanet(seed): |
|
|
|
random.seed(seed) |
|
light = Image.open("planets/light/light" + str(random.randrange(0,amount_lights)) + ".png").convert('RGBA') |
|
noise1 = Image.open("planets/noise/noise" + str(random.randrange(0,amount_noise)) + ".png").convert('RGBA') |
|
noise2 = Image.open("planets/noise/noise" + str(random.randrange(0,amount_noise)) + ".png").convert('RGBA') |
|
noise3 = Image.open("planets/noise/noise" + str(random.randrange(0,amount_noise)) + ".png").convert('RGBA') |
|
sphere = Image.open("planets/sphere/sphere" + str(random.randrange(0,amount_sphere)) + ".png").convert('RGBA') |
|
|
|
light = changePlanetLayerColor(light) |
|
noise1 = changePlanetLayerColor(noise1) |
|
noise2 = changePlanetLayerColor(noise2) |
|
noise3 = changePlanetLayerColor(noise3) |
|
sphere = changePlanetLayerColor(sphere) |
|
|
|
planet = mergePlanetParts(light, noise1, noise2, noise3, sphere) |
|
planet.save("static/IMG/planet.png") |
|
return |
|
|
|
|
|
#planet = createRandomPlanet('Elijah') |
|
#planet.save("planet2.png") |