Skip to content

Commit

Permalink
custom cropped images
Browse files Browse the repository at this point in the history
  • Loading branch information
impshum committed Dec 20, 2017
1 parent 09f8978 commit 5818155
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
Binary file added data/cropped.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/overlay.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 35 additions & 13 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
import json
import random
import os
from config import *


def generate():
dir = os.path.dirname(__file__)
mg = MaterialGenerator()
qg = QuoteGenerator()
quote = qg.get_quote()
image = mg.draw_image(quote['quoteText'], quote[
'quoteAuthor'], (4000, 4000))
image.save(os.path.join(dir, 'data/image.jpg'), 'JPEG')
if gen:
mg = MaterialGenerator()
qg = QuoteGenerator()
quote = qg.get_quote()
image = mg.draw_image(quote['quoteText'], (4000, 4000))
image.save(os.path.join(dir, 'data/image.jpg'), 'JPEG')
else:
mg = BackgroundGenerator()
qg = QuoteGenerator()
quote = qg.get_quote()
image = mg.draw_image(quote['quoteText'], (4000, 4000))
image.save(os.path.join(dir, 'data/image.jpg'), 'JPEG')


def get_random_color():
Expand All @@ -24,18 +31,34 @@ def get_random_color():

class MaterialGenerator:

def draw_image(self, text, author, size):
COPYRIGHT_TEXT = "@PUTlN2"
def draw_image(self, text, size):
dir = os.path.dirname(__file__)
im = Image.new('RGB', size)
draw = ImageDraw.Draw(im)
draw.rectangle(((0, 0), size), fill=get_random_color())
font = ImageFont.truetype(os.path.join(
dir, 'fonts/DeFonarts-Bold.otf'), 280)
lines = textwrap.wrap(text, 20)
#if author != "":
# lines.append(' ')
# lines.append('- ' + author)
line_dimensions = [draw.textsize(line, font=font) for line in lines]
offset = (size[1] - sum(h for w, h in line_dimensions)) // 2
for line in lines:
w, h = draw.textsize(line, font=font)
pos = ((size[0] - w) / 2, offset)
draw.text(pos, line, font=font, fill=(255, 255, 255))
offset += font.getsize(line)[1]
del draw
return im

class BackgroundGenerator:

def draw_image(self, text, size):
dir = os.path.dirname(__file__)
im = Image.open('data/result.jpg')
draw = ImageDraw.Draw(im)

font = ImageFont.truetype(os.path.join(
dir, 'fonts/DeFonarts-Bold.otf'), 280)
lines = textwrap.wrap(text, 20)
line_dimensions = [draw.textsize(line, font=font) for line in lines]
offset = (size[1] - sum(h for w, h in line_dimensions)) // 2
for line in lines:
Expand All @@ -60,7 +83,6 @@ def filter_quotes(self, quotes, lenQ, lenA):
_quotes = []
for quote in quotes:
lQ = quote['quoteText'].__len__()
lA = quote['quoteAuthor'].__len__()
if lQ <= lenQ and lA <= lenA:
if lQ <= lenQ:
_quotes.append(quote)
return _quotes
41 changes: 12 additions & 29 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from config import *
from functions import *
from InstagramAPI import InstagramAPI
from PIL import Image, ImageDraw, ImageFont
from pypin import PyPin


Expand All @@ -24,7 +25,7 @@ class Colour:
mins = timer / 60
if loop:
print(Colour.White + 'Posting every {}'.format(int(mins)),
'minutes\n\nPress Ctrl + C to exit\n')
'minutes\n\nPress Ctrl + C to exit\n')
else:
print(Colour.White + '\nPress Ctrl + C to exit\n')

Expand All @@ -45,20 +46,13 @@ class Colour:
print(Colour.Green + 'Generating image')
generate()
else:
try:
d = 'data/images/'
pic = d + random.choice(os.listdir(d))
except Exception:
print(Colour.Red + 'No images')
yes = set(['YES', 'yes', 'y', 'Y'])
no = set(['NO', 'no', 'n', 'N'])
choice = input(Colour.White + 'Use test image? Y/n ')
if choice in no:
print(Colour.White + '\nExiting\n')
sys.exit(1)
elif choice in yes:
testimg = 1
pic = 'data/test.jpg'
d = 'data/images/'
pick = d + random.choice(os.listdir(d))
os.system('convert -define jpeg:size=1280x1280 -resize 4000x4000^ -gravity center -extent 4000x4000 ' +
pick + ' data/cropped.jpg')
os.system('composite -blend 30 data/overlay.jpg data/cropped.jpg data/result.jpg')
print(Colour.Green + 'Cropping image')
generate()

if not testmode:

Expand Down Expand Up @@ -126,29 +120,18 @@ def get_api(failbork):
})
except Exception:
print(Colour.Red + 'Pinterfail')
if not gen:
try:
os.remove(pic)
except Exception:
print(Colour.Red + 'No images')
yes = set(['YES', 'yes', 'y', 'Y'])
no = set(['NO', 'no', 'n', 'N'])
choice = input(Colour.White + 'Use test image? Y/n ')
if choice in no:
print(Colour.White + '\nExiting\n')
sys.exit(1)
elif choice in yes:
testimg = 1
pic = 'data/test.jpg'

else:
print('test mode')

if not loop:
print(Colour.White + '\nExiting\n')
break
else:
time.sleep(timer)



except KeyboardInterrupt:
print(Colour.White + '\nExiting\n')
sys.exit(1)

0 comments on commit 5818155

Please sign in to comment.