Skip to content

Commit

Permalink
074
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Jun 10, 2017
1 parent db9838a commit c87ef8b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 074/.gitignore
@@ -0,0 +1,2 @@
*jpg
*png
Binary file added 074/opensans.ttf
Binary file not shown.
7 changes: 7 additions & 0 deletions 074/requirements.txt
@@ -0,0 +1,7 @@
certifi==2017.4.17
chardet==3.0.4
idna==2.5
olefile==0.44
Pillow==4.1.1
requests==2.17.3
urllib3==1.21.1
82 changes: 82 additions & 0 deletions 074/text_on_image.py
@@ -0,0 +1,82 @@
import os
import textwrap

from PIL import Image, ImageDraw, ImageFont
import requests

FONT_SIZE = 30
FONT = ImageFont.truetype('opensans.ttf', FONT_SIZE)
TEXT_COLOR = (0, 0, 0, 255) # black, 100% opacity
OVERLAY_COLOR = (255, 255, 255, 178) # white, ~70% opacity
TEXT_OFFSET = (20, 20)
TEXT_CHAR_LIMIT = 250
RESIZE_WIDTH = 500
TEXT_WIDTH = 30 # experimental
IN_FILE = 'input.png'
OUT_FILE = 'output.png'


def download_url(url, in_file=IN_FILE, chunk_size=2000):
print('Downloading {}'.format(url))
r = requests.get(url, stream=True)
print('Saving as {}'.format(in_file))

with open(in_file, 'wb') as fd:
for chunk in r.iter_content(chunk_size):
fd.write(chunk)

return in_file


def get_input(prompt):
# https://stackoverflow.com/questions/30239092/how-to-get-multiline-input-from-user
print(prompt)
lines = []
while True:
line = input()
if line:
line = textwrap.fill(line, width=TEXT_WIDTH)
lines.append(line)
else:
break
return '\n\n'.join(lines)


def _resize(img):
basewidth = RESIZE_WIDTH
wpercent = basewidth / float(img.size[0])
hsize = int(float(img.size[1]) * float(wpercent))
return img.resize((basewidth, hsize), Image.ANTIALIAS)


def create_img_with_text(base_img, input_text, out_file=OUT_FILE):
base = Image.open(base_img).convert('RGBA')
base = _resize(base)

txt = Image.new('RGBA', base.size, OVERLAY_COLOR)

draw_context = ImageDraw.Draw(txt)
draw_context.text(TEXT_OFFSET, input_text, font=FONT, fill=TEXT_COLOR)

out = Image.alpha_composite(base, txt)
out.save(out_file)

return out_file


if __name__ == '__main__':
resource = input('Image file (local or url to download): ')

if os.path.isfile(resource):
base_img = resource
else:
base_img = download_url(resource)

prompt = 'Text to put on image '
prompt += '(max {} chars, double enter = end): '.format(TEXT_CHAR_LIMIT)
input_text = get_input(prompt)
input_text = input_text[:TEXT_CHAR_LIMIT]

out_file = create_img_with_text(base_img, input_text)

print('{} created'.format(out_file))
2 changes: 1 addition & 1 deletion LOG.md
Expand Up @@ -75,7 +75,7 @@
| 071 | Jun 08, 2017 | [How to calculate the # of days between two dates #Python #datetime](071) | Simple CLI script to calculate the number of days between two dates. It uses datetime and dateutils to do this. Still need to strip out the timestamp in the final output though. Maybe a regex? Needs investigation. |
| 072 | Jun 09, 2017 | [Packt ebook download manager](072) | Using requests to login to Packt, BeautifulSoup to parse the account/ebooks page for links, letting user download his/her books via interactive script |
| 073 | Jun 10, 2017 | [#Python script to download a file using #FTP](073) | A simple and quick script to download a file from an FTP server using ftplib. Would ideally be matched with a cron job. |
| 074 | Jun 11, 2017 | [TITLE](074) | LEARNING |
| 074 | Jun 11, 2017 | [Using Pillow to add text and opacity to an image = your own cards](074) | Played with the Pillow module. Script to let user enter an image path (or url) and text to put on the image. Pillow does the rest. Could be a useful recipe to make your own Birthday cards :) |
| 075 | Jun 12, 2017 | [TITLE](075) | LEARNING |
| 076 | Jun 13, 2017 | [TITLE](076) | LEARNING |
| 077 | Jun 14, 2017 | [TITLE](077) | LEARNING |
Expand Down

0 comments on commit c87ef8b

Please sign in to comment.