Beispiel #1
0
def my_draw(request, bg, text, font, textColor, shadowColor, border, shadow, highlight):
    """draw avatar. Core part of the program."""
    from hashlib import md5
    text_md5=md5(text.encode('utf-8')).hexdigest()
    pure=md5(text_md5+bg+font+textColor+shadowColor+str(border)+str(shadow)+str(highlight)).hexdigest()+".png"
    filename=findPath('media/result/')+pure
    pFont=fontFile(font)

    (textPosition, fontSize)=fontPosition(pFont)

    #generate new pic only when not existing
    if not os.path.exists(filename):
        # when debugging, set if to 1, so as to gen pic each time;
        # when done, set if to not os.path.exists(filename), and only gen new pics.

        image_a=Image.open(findPath("colors/%s" % bg))
        image_b=Image.open(findPath("colors/%s" % bg))
        font=ImageFont.truetype(pFont, fontSize)
        width=180
        height=180
        img_draw=ImageDraw.Draw(image_b)
        shadow=int(shadow)
        if shadow ==0:    #no shadow at all
            img_draw.text(textPosition,text,font=font, fill=textColor)
            image_b.save(filename)
        else  :
            if shadow==2: #shadow offset
                side=15
            else:         #shadow but no offset
                side=10

            box=(10,10,width+10, height+10)
            position=(0+side,0+side, width+side, height+side)

            img_draw.text(textPosition,text,font=font,fill=shadowColor)
            imgfilted=image_b.filter(ImageFilter.BLUR)
            region=imgfilted.crop(box)
            img_draw=ImageDraw.Draw(image_a)
            image_a.paste(region,position)
            img_draw.text(textPosition,text,font=font, fill=textColor)
            image_a.save(filename)


    image_path_on_server="""./media/result/%s""" % pure
    return image_path_on_server
Beispiel #2
0
def draw_hat(bg, hatName, angle, offsetLeft, offsetTop, hatWidth, hatHeight):

    filename=md5("".join([str(x) for x in [bg, hatName, angle, offsetLeft, offsetTop, hatWidth, hatHeight]])).hexdigest()+".png"
    filename=findPath('media/result/')+filename

    #cut filename from
    # #/site_media/result/bc98ff0348403f8098a3f7207c29f94f.png
    # to
    # media/result/bc98ff0348403f8098a3f7207c29f94f.png
    bg=bg[-49:] 
    bg=findPath(bg)
    bg=Image.open(bg)
    
    hat=findPath("media/images/"+hatName)
    hat=hat[hat.index("media"):hat.index("png")+3]
    hat=findPath(hat)
    hat=Image.open(hat)

    #resize the hat
    hat=hat.resize((hatWidth, hatHeight), Image.ANTIALIAS)
    hat=hat.rotate(angle, Image.BILINEAR)
    box=(0, 0, hat.size[0], hat.size[1])
    pisition=(offsetLeft, offsetTop, hat.size[0]+offsetLeft, hat.size[1]+offsetTop)
    bg.paste(hat,pisition, hat)

    #add snowflakes
    snowfile=findPath('media/images/snowflakes.png')
    snow=Image.open(snowfile)
    bg.paste(snow, (0, 0, snow.size[0], snow.size[1]), snow)

    #save as file
    bg.save(filename, "PNG")
    filename=filename[-49:]

    url="""/site_%s""" % filename
    html="""<a href="/result?url=%s"><img src="%s"></a>""" % (url, url)
    
#    image_path_on_server="""./media/result/%s""" % filename
    image_path_on_server = filename
    return image_path_on_server
Beispiel #3
0
def delete_pic(remain=REMAIN_PICS):
    '''delete old pics so as to reduce the disk usage'''

    p=findPath("media/result/*.png")

    files=glob.glob(p)
    s={}
    for f in files:
        statinfo=os.stat(f)
        s[f]=statinfo.st_mtime

    c= s.items()
    c.sort(key=lambda x:x[1])

    if len(c)<=remain:
        pass
    else:
        for (x,y) in c[0:len(c)-remain]:
            os.unlink(x)
Beispiel #4
0
    #return HttpResponse("<pre>%r</pre>" % locals())
    image_path_on_server = my_draw(request,
            bg=bg,
            text=text,
            font=font,
            textColor=textColor,
            shadowColor=shadowColor,
            border=border,
            shadow=shadow,
            highlight=highlight)

    #hat processing, a loop procedure, for several hats(horns)
    image_path_on_server=hat(request, bg=image_path_on_server)
    saveImage4Upload(request, image_path_on_server)
    image_path_on_server=findPath(image_path_on_server)
    image_data = open(image_path_on_server, "rb").read()

    #if save==1, download:
    save = _safeGetBoolean(request, "save")
    if save:
        response = HttpResponse(image_data, mimetype="image/png")
        response['Content-Disposition'] = 'attachment; filename=%s' % image_path_on_server[-36:]
        return response

    return HttpResponse(image_data, mimetype="image/png")

def my_draw(request, bg, text, font, textColor, shadowColor, border, shadow, highlight):
    """draw avatar. Core part of the program."""
    from hashlib import md5
    text_md5=md5(text.encode('utf-8')).hexdigest()