Browse Source

Image Handling gefixed

Und kleiner Cleanup
master
Eddie 2 years ago
parent
commit
cd267700af
  1. 14
      app.py
  2. BIN
      planet2.png
  3. 11
      planets.py
  4. BIN
      static/IMG/planet.png
  5. 3
      templates/index.html

14
app.py

@ -1,20 +1,22 @@
import os import os
from flask import Flask, render_template from flask import Flask, render_template, send_file
from planets import createRandomPlanet from planets import createRandomPlanet
app = Flask(__name__) app = Flask(__name__)
IMG_FOLDER = os.path.join('static', 'IMG') IMG_FOLDER = os.path.join('static', 'IMG')
app.config['UPLOAD_FOLDER'] = IMG_FOLDER app.config['UPLOAD_FOLDER'] = IMG_FOLDER
@app.route('/')
def index():
planet = os.path.join(app.config['UPLOAD_FOLDER'], 'planet.png')
return render_template('index.html', planet=planet)
@app.route('/<seed>') @app.route('/<seed>')
def hello(seed): def hello(seed):
print(seed)
createRandomPlanet(seed) createRandomPlanet(seed)
planet = os.path.join(app.config['UPLOAD_FOLDER'], 'planet.png') planet = os.path.join(app.config['UPLOAD_FOLDER'], 'planet.png')
return render_template('hello.html', planet=planet) return send_file(planet)
if __name__=='__main__': if __name__=='__main__':
app.run(debug=True, host='0.0.0.0') app.run(debug=True, host='0.0.0.0')

BIN
planet2.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 434 KiB

11
planets.py

@ -1,7 +1,6 @@
import os, random import os, random
from PIL import Image from PIL import Image
light_directory = os.fsencode(str(os.getcwd() + "/planets/light")) light_directory = os.fsencode(str(os.getcwd() + "/planets/light"))
noise_directory = os.fsencode(str(os.getcwd() + "/planets/noise")) noise_directory = os.fsencode(str(os.getcwd() + "/planets/noise"))
sphere_directory = os.fsencode(str(os.getcwd() + "/planets/sphere")) sphere_directory = os.fsencode(str(os.getcwd() + "/planets/sphere"))
@ -11,13 +10,12 @@ amount_noise = len(os.listdir(noise_directory))
amount_sphere = len(os.listdir(sphere_directory)) amount_sphere = len(os.listdir(sphere_directory))
def changeImageColor(im, red, green, blue): def changeImageColor(im, red, green, blue):
# Split into 3 channels
r, g, b, a = im.split() r, g, b, a = im.split()
# Change channel by factor
r = r.point(lambda i: i * red) r = r.point(lambda i: i * red)
g = g.point(lambda i: i * green) g = g.point(lambda i: i * green)
b = b.point(lambda i: i * blue) b = b.point(lambda i: i * blue)
# Recombine back to RGB image
result = Image.merge('RGBA', (r, g, b, a)) result = Image.merge('RGBA', (r, g, b, a))
return result return result
@ -25,7 +23,6 @@ def changePlanetLayerColor(planetLayer):
red = random.uniform(-0.5,1.5) red = random.uniform(-0.5,1.5)
green = random.uniform(-0.5,1.5) green = random.uniform(-0.5,1.5)
blue = random.uniform(-0.5,1.5) blue = random.uniform(-0.5,1.5)
#print(red,green,blue)
changedColor = changeImageColor(planetLayer, red, green, blue) changedColor = changeImageColor(planetLayer, red, green, blue)
return changedColor return changedColor
@ -63,7 +60,3 @@ def createRandomPlanet(seed):
planet = mergePlanetParts(light, noise1, noise2, noise3, sphere) planet = mergePlanetParts(light, noise1, noise2, noise3, sphere)
planet.save("static/IMG/planet.png") planet.save("static/IMG/planet.png")
return return
#planet = createRandomPlanet('Elijah')
#planet.save("planet2.png")

BIN
static/IMG/planet.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 378 KiB

After

Width:  |  Height:  |  Size: 443 KiB

3
templates/hello.html → templates/index.html

@ -2,9 +2,10 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Hello Worl!</title> <title>Planets</title>
</head> </head>
<body> <body>
<h1>Welcome to Planets</h1>
<img src="{{ planet }}" alt="Planet"> <img src="{{ planet }}" alt="Planet">
</body> </body>
</html> </html>
Loading…
Cancel
Save