Skip to content

Commit 1b95e84

Browse files
bbelderbospybites
authored andcommitted
Pcc31 (pybites#94)
* 29 readme * init commit * working app * updates readme
1 parent b5d01b6 commit 1b95e84

23 files changed

+353
-0
lines changed

31/bbelderbos/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*swp
2+
*pyc
3+
__pycache__
4+
images
5+
out.png

31/bbelderbos/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## PyBites Code Challenge 31 - Image Manipulation With Pillow
2+
3+
### Submission: PyBites Banner Generator
4+
5+
I made this utility to create quick but nice banners for PyBites Articles / Challenges / News / Special. However I think the code can easily be extended or modified to use it for your own needs.
6+
7+
It takes an existing PyBites logo as the first image, and downloads (caches) the second image.
8+
9+
*Text for Banner* is for the text to be put on the banner. `SourceSansPro-Regular.otf` gave me a weird char for newline (`\n`) so I switched to `Ubuntu-R.ttf`. Of course you could build this out to also let the user choose the font type and more things. The underlying `banner.py` script should make this easy: all font settings are collected into a namedtuple before passing it to the image creating class.
10+
11+
The last option of the form controls the location of the second image titled *Use Second Image as Background?*. By default this is turned on and the second image serves as a background image (example 2). If you disable it, the image will be resized to thumbnail and aligned to the right (example 1 / as in [the article](https://pybit.es/pillow-banner-image.html)).
12+
13+
#### Example 1. - make a PyBites Code Challenge banner
14+
15+
1. Choose *challenge* as first image, provide URL to the second image, add banner text, disable *Use Second Image as Background?*:
16+
17+
![example1a.png](assets/readme/example1a.png)
18+
19+
2. Click *Generate banner*:
20+
21+
![example1b.png](assets/readme/example1b.png)
22+
23+
3. Right-click and save the image.
24+
25+
#### Example 2. - make a PyBites News banner
26+
27+
1. Choose *news* as first image, provide URL to the second image, add banner text, leave *Use Second Image as Background?* enabled:
28+
29+
![example2a.png](assets/readme/example2a.png)
30+
31+
2. Click *Generate banner*:
32+
33+
![example2b.png](assets/readme/example2b.png)
34+
35+
3. Right-click and save the image.
36+
37+
### PyBites articles:
38+
39+
* [Using Pillow to Create Nice Banners For Your Site](https://pybit.es/pillow-banner-image.html)
40+
41+
* This week I will write a part 2 how I wrapped the original `banner.py` (command line) script into this Flask app. If I don't get to update this readme, the previous article will link to it ...

31/bbelderbos/app.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
import requests
3+
4+
from flask import Flask, abort, render_template, request, send_file
5+
6+
from forms import ImageForm
7+
from banner.banner import generate_banner
8+
from banner.banner import DEFAULT_OUTPUT_FILE as outfile
9+
10+
IMAGES = 'images'
11+
12+
app = Flask(__name__)
13+
14+
15+
def _download_image(from_url, to_file, chunk_size=2000):
16+
r = requests.get(from_url, stream=True)
17+
18+
with open(to_file, 'wb') as fd:
19+
for chunk in r.iter_content(chunk_size):
20+
fd.write(chunk)
21+
22+
23+
def get_image(image_url):
24+
basename = os.path.basename(image_url)
25+
local_image = os.path.join(IMAGES, basename)
26+
27+
if not os.path.isfile(local_image):
28+
_download_image(image_url, local_image)
29+
30+
return local_image
31+
32+
33+
@app.route('/', methods=['GET', 'POST'])
34+
def image_inputs():
35+
form = ImageForm(request.form)
36+
37+
if request.method == 'POST' and form.validate():
38+
image1 = form.image_url1.data
39+
image2 = get_image(form.image_url2.data)
40+
text = form.text.data
41+
print(text)
42+
background = form.background.data
43+
44+
args = [image1, image2, text, background]
45+
46+
generate_banner(args)
47+
48+
if os.path.isfile(outfile):
49+
return send_file(outfile, mimetype='image/png')
50+
else:
51+
abort(400)
52+
53+
return render_template('imageform.html', form=form)
54+
55+
56+
if __name__ == "__main__":
57+
app.run(debug=True)
224 KB
Binary file not shown.

31/bbelderbos/assets/Ubuntu-R.ttf

346 KB
Binary file not shown.
203 KB
Loading
383 KB
Loading
370 KB
Loading
386 KB
Loading
386 KB
Loading

0 commit comments

Comments
 (0)