#!/usr/bin/env python from PIL import Image, ImageDraw, ImageFont import datetime import time from random import randint import os def hash(mode=0): x='' letters=['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E' , 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'nO', 'o', 'P', 'p', 'Q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z'] numbers = [1,2,3,4,5,6,7,8,9,0] a = [letters,numbers] if mode == 0: two=randint(20,33) else: two = 10 for i in range(0,two): ii = randint(0,1) array=a[ii] letter=array[randint(0,len(array)-1)] x = str(x)+str(letter) return x def ru(x): # return(x.decode('utf-8')) unicode(x,'utf-8') def main(img=None,header=None,descr=None,bg=None,mode=None): if img == None or header == None or descr == None or bg == None: print "demotivator.py imagefile header description [bg_color]" print "\tExample: python demotivator.py image.png 'Demotivator' 'This is demotivator'" exit(0) file =img TOP_BORDER=40 BOTTOM_BORDER=20 LEFT_BORDER=40 RIGHT_BORDER=40 BIG_FONT_SIZE=48 SMALL_FONT_SIZE=16 BG_COLOR="#010101" BG_COLOR=bg # Loading fonts big_font = ImageFont.truetype("/usr/share/fonts/droid/DroidSansFallback.ttf", BIG_FONT_SIZE, encoding="unic") header = header big_font_size = big_font.getsize(header) small_font = ImageFont.truetype("/usr/share/fonts/droid/DroidSansFallback.ttf", SMALL_FONT_SIZE, encoding="unic") descr = descr small_font_size = small_font.getsize(descr) # Calculating size of demotivator src_img = Image.open(img) src_size = src_img.getbbox()[2:] dst_size = list(src_size) dst_size[0] += LEFT_BORDER + RIGHT_BORDER dst_size[1] += TOP_BORDER + BOTTOM_BORDER + \ big_font_size[1] + small_font_size[1] + 5 # Making border dst_img = Image.new("RGB", dst_size, "black") dst_draw = ImageDraw.Draw(dst_img) dst_draw.rectangle([0, 0, dst_size[0], dst_size[1]], fill=BG_COLOR) dst_img.paste(src_img, (LEFT_BORDER, TOP_BORDER)) # Drawing border lines dst_draw.line( (LEFT_BORDER - 3, TOP_BORDER - 3, dst_size[0] - RIGHT_BORDER + 3, TOP_BORDER - 3), width=1) dst_draw.line( (dst_size[0] - RIGHT_BORDER + 3, TOP_BORDER - 3, dst_size[0] - RIGHT_BORDER + 3, TOP_BORDER + src_size[1] + 3), width=1) dst_draw.line( (LEFT_BORDER - 3, TOP_BORDER + src_size[1] + 3, dst_size[0] - RIGHT_BORDER + 3, TOP_BORDER + src_size[1] + 3), width=1) dst_draw.line( (LEFT_BORDER - 3, TOP_BORDER + src_size[1] + 3, LEFT_BORDER - 3, TOP_BORDER - 3), width=1) # Drawing text text_pos_x = (dst_size[0] - big_font_size[0]) / 2 text_pos_y = src_img.getbbox()[3] + TOP_BORDER + 5 dst_draw.text((text_pos_x, text_pos_y), header, font=big_font) text_pos_x = (dst_size[0] - small_font_size[0]) / 2 text_pos_y += big_font_size[1] + 5 dst_draw.text((text_pos_x, text_pos_y), descr, font=small_font) # Saving and showing image dt = datetime.datetime.now() date=time.mktime(dt.timetuple()) n= '/var/www/' name = str(hash())+'-'+str(date)+'.png' dst_img.save(n+name, "PNG") img = Image.open(n+name) img.thumbnail((350, 400), Image.ANTIALIAS) img.save(n+'2-'+name,"PNG")