Skip to content

Commit

Permalink
lint: black
Browse files Browse the repository at this point in the history
  • Loading branch information
SubhrajitPrusty committed Oct 28, 2023
1 parent 323ddb6 commit c1237ce
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 162 deletions.
121 changes: 64 additions & 57 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@
genTriangle,
nGradient,
random_gradient,
swirl_image)
swirl_image,
)

UPLOAD_FOLDER = os.path.join("static", "upload")
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
ALLOWED_EXTENSIONS = set(["png", "jpg", "jpeg"])

app = Flask(__name__, static_url_path="/static")
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["MAX_CONTENT_LENGTH"] = 5 * 1024 * 1024


def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route("/", methods=['GET'])
@app.route("/", methods=["GET"])
def index():
return render_template("home.html")


@app.route("/poly", methods=['GET', 'POST'])
@app.route("/poly", methods=["GET", "POST"])
def poly():
if request.method == 'POST':
if request.method == "POST":
# get data
side = int(request.form.get('side'))
np = int(request.form.get('np'))
outline = request.form.get('outline')
bgtype = request.form.get('bgtype')
swirl = request.form.get('swirl')
side = int(request.form.get("side"))
np = int(request.form.get("np"))
outline = request.form.get("outline")
bgtype = request.form.get("bgtype")
swirl = request.form.get("swirl")

error = None

Expand All @@ -55,7 +55,7 @@ def poly():
error = "WARNING: Too less points OR too many points"

fname = "wall-{}.png".format(int(time.time()))
fpath = 'static/images/' + fname
fpath = "static/images/" + fname

shift = side // 10
nside = side + shift * 2 # increase size to prevent underflow
Expand All @@ -65,11 +65,11 @@ def poly():
if bgtype == "nbyn":
img = NbyNGradient(nside)
elif bgtype == "customColors":
nColors = request.form.get('nColors')
nColors = request.form.get("nColors")
colors = []

for i in range(int(nColors)):
colors.append(request.form.get('rgb' + str(i + 1)))
colors.append(request.form.get("rgb" + str(i + 1)))

try:
colors = [tuple(bytes.fromhex(x[1:])) for x in colors]
Expand All @@ -81,7 +81,7 @@ def poly():

if error is not None:
print(error)
return render_template('error.html', context=error)
return render_template("error.html", context=error)

if outline:
outline = tuple(bytes.fromhex("#2c2c2c"[1:]))
Expand All @@ -97,39 +97,39 @@ def poly():
# print(fpath)
img.save(fpath)

imgurl = url_for('static', filename='images/' + fname)
imgurl = url_for("static", filename="images/" + fname)
return render_template("download.html", context=imgurl, home="poly")
else:
return render_template('poly.html')
return render_template("poly.html")


@app.route("/shape", methods=['GET', 'POST'])
@app.route("/shape", methods=["GET", "POST"])
def shape():
if request.method == 'POST':
side = int(request.form.get('side'))
outline = request.form.get('outline')
bgtype = request.form.get('bgtype')
swirl = request.form.get('swirl')
shape = request.form.get('shape')
if request.method == "POST":
side = int(request.form.get("side"))
outline = request.form.get("outline")
bgtype = request.form.get("bgtype")
swirl = request.form.get("swirl")
shape = request.form.get("shape")

error = None

if side > 5000 or side < 100:
error = "WARNING: Image too large OR Image too small"

fname = "wall-{}.png".format(int(time.time()))
fpath = 'static/images/' + fname
fpath = "static/images/" + fname

img = random_gradient(side)

if bgtype == "nbyn":
img = NbyNGradient(side)
elif bgtype == "customColors":
nColors = request.form.get('nColors')
nColors = request.form.get("nColors")
colors = []

for i in range(int(nColors)):
colors.append(request.form.get('rgb' + str(i + 1)))
colors.append(request.form.get("rgb" + str(i + 1)))

try:
colors = [tuple(bytes.fromhex(x[1:])) for x in colors]
Expand All @@ -141,7 +141,7 @@ def shape():

if error is not None:
print(error)
return render_template('error.html', context=error)
return render_template("error.html", context=error)

if outline:
outline = tuple(bytes.fromhex("#2c2c2c"[1:]))
Expand All @@ -151,34 +151,34 @@ def shape():
if swirl:
img = swirl_image(img)

if shape == 'hexagon':
if shape == "hexagon":
img = genHexagon(side, side, img, outline, per=5)
elif shape == 'squares':
elif shape == "squares":
img = genSquares(side, side, img, outline, per=5)
elif shape == 'diamond':
elif shape == "diamond":
img = genDiamond(side, side, img, outline, per=5)
elif shape == 'triangle':
elif shape == "triangle":
img = genTriangle(side, side, img, outline, per=5)
elif shape == 'isometric':
elif shape == "isometric":
img = genIsometric(side, side, img, outline, per=5)
# print(fpath)
img.save(fpath)
imgurl = url_for('static', filename='images/' + fname)
imgurl = url_for("static", filename="images/" + fname)
return render_template("download.html", context=imgurl, home="shape")
else:
return render_template('shape.html')
return render_template("shape.html")


@app.route("/pic", methods=['GET', 'POST'])
@app.route("/pic", methods=["GET", "POST"])
def pic():
if request.method == 'POST':
if request.method == "POST":
# print(request.files)
# print(request.form)
if 'image' not in request.files:
if "image" not in request.files:
error = "No file part"
return render_template("error.html", context=error)
else:
file = request.files['image']
file = request.files["image"]
# print(file.filename)
# print(len(file.filename))
if len(file.filename) < 1:
Expand All @@ -187,11 +187,11 @@ def pic():

if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
ufpath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
ufpath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
file.save(ufpath)
np = request.form.get('np')
outline = request.form.get('outline')
smart = request.form.get('smart')
np = request.form.get("np")
outline = request.form.get("outline")
smart = request.form.get("smart")

if np or smart:
og_img = Image.open(ufpath)
Expand All @@ -203,8 +203,8 @@ def pic():
else:
scale = 1
img = og_img.resize(
(width // scale, height // scale),
resample=Image.BICUBIC)
(width // scale, height // scale), resample=Image.BICUBIC
)
width = img.width
height = img.height
wshift = width // 100
Expand All @@ -225,17 +225,24 @@ def pic():
else:
pts = genPoints(int(np), n_width, n_height)

img = genPoly(img.width, img.height, img, pts,
wshift, hshift, outline, pic=True)
img = genPoly(
img.width,
img.height,
img,
pts,
wshift,
hshift,
outline,
pic=True,
)

fname = "wall-{}.png".format(int(time.time()))
fpath = 'static/images/' + fname
fpath = "static/images/" + fname

# print(fpath)
img.save(fpath)
imgurl = url_for('static', filename='images/' + fname)
return render_template(
"download.html", context=imgurl, home="pic")
imgurl = url_for("static", filename="images/" + fname)
return render_template("download.html", context=imgurl, home="pic")
else:
error = "Invalid input, try again"
return render_template("error.html", context=error)
Expand All @@ -246,8 +253,8 @@ def pic():
return render_template("pic.html")


if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
http_server = WSGIServer(('', port), app)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
http_server = WSGIServer(("", port), app)
print("Starting server:")
http_server.serve_forever()
70 changes: 35 additions & 35 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,46 @@

def readme():
try:
with open('README.md') as f:
with open("README.md") as f:
return f.read()
except BaseException:
pass


setup(name='wallgen',
version='1.0',
description='Generate low poly wallpapers',
long_description=readme(),
long_description_content_type='text/markdown',

author='Subhrajit Prusty',
author_email='[email protected]',
url='http://github.com/SubhrajitPrusty/wallgen',

setup_requires=['setuptools>=40.0.0'],

classifiers=[
'Development Status :: 4 - Beta',

'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',

'License :: OSI Approved :: MIT License',

'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],

keywords='image PIL wallpaper theme',
license='MIT',
packages=find_packages(),
install_requires=['pillow', 'click', 'scipy',
'numpy', 'Cython', 'scikit-image',
'loguru'],
entry_points="""
setup(
name="wallgen",
version="1.0",
description="Generate low poly wallpapers",
long_description=readme(),
long_description_content_type="text/markdown",
author="Subhrajit Prusty",
author_email="[email protected]",
url="http://github.com/SubhrajitPrusty/wallgen",
setup_requires=["setuptools>=40.0.0"],
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Software Development :: Build Tools",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
],
keywords="image PIL wallpaper theme",
license="MIT",
packages=find_packages(),
install_requires=[
"pillow",
"click",
"scipy",
"numpy",
"Cython",
"scikit-image",
"loguru",
],
entry_points="""
[console_scripts]
wallgen=wallgen:cli
""",
)
)
2 changes: 1 addition & 1 deletion tools/gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def swirl_image(image, strength=10):
w, h = image.shape[:2]
sw = swirl(image, rotation=0, strength=strength, radius=max(w, h))
with warnings.catch_warnings():
warnings.simplefilter('ignore')
warnings.simplefilter("ignore")
sw = img_as_ubyte(sw)

pil_img = Image.fromarray(sw)
Expand Down
11 changes: 6 additions & 5 deletions tools/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def distance(p1, p2):
(x1, y1) = p1
(x2, y2) = p2

d = int((y2 - y1)**2 + (x2 - x1)**2)**0.5
d = int((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5
return d


Expand Down Expand Up @@ -46,7 +46,7 @@ def genPoints(qty, width, height):


def calcCenter(ps):
""" calculate incenter of a triangle given all vertices"""
"""calculate incenter of a triangle given all vertices"""
mid1 = ((ps[0][0] + ps[1][0]) / 2, (ps[0][1] + ps[1][1]) / 2)
mid = ((mid1[0] + ps[2][0]) / 2, (mid1[1] + ps[2][1]) / 2)
return mid
Expand All @@ -60,7 +60,7 @@ def genSmartPoints(image):

# convert to RGB compatible image
with warnings.catch_warnings():
warnings.simplefilter('ignore')
warnings.simplefilter("ignore")
rgb_img = img_as_ubyte(color.gray2rgb(edges))

# convert to PIL image
Expand All @@ -84,8 +84,9 @@ def genSmartPoints(image):
raise Exception("EdgeDetectionError")

# get a n/5 number of points rather than all of the points
sample = np.random.choice(len(edges_data), len(
edges_data) // 5 if len(edges_data) / 5 < 50000 else 50000)
sample = np.random.choice(
len(edges_data), len(edges_data) // 5 if len(edges_data) / 5 < 50000 else 50000
)
edges_data = [edges_data[x] for x in sample]

# print(len(edges_data))
Expand Down
Loading

0 comments on commit c1237ce

Please sign in to comment.