Exemple #1
0
    def makeImage(self, pfn, filename, suffix, BT, query):
        "ask blockchain server, calculate text, make image - and save it to disk"

        try:
            _, text = chaincountdown.doTheChaChaCha(BT, pfn)
        except Exception as e:
            self.send_error(self.INTERNAL_SERVER_ERROR, "Querying the blockchain server failed (%s)" % concatArgs(e))
            return False

        try:
            o = helpers.replacedInDict(IMAGEDEFAULTS, query)
            o["fontsizefooter"] = max(FONTSIZEFOOTER_MIN, int(o["fontsize"]) / 2)
            imaging.textToImage(filename, text=text, **o)
        except Exception as e:
            self.send_error(
                self.BAD_REQUEST, "Creating the image did not work (%s) " % concatArgs(e.args) + self.suggest()
            )
            return False

        return True
Exemple #2
0
def textToImage(filename, text, **options):
    """Says what it does. 
       One long subroutine only because it is a simply a serial process.
       N.B. filetype polymorphism depending on suffix of filename"""
       
    # defaults are overwritten by user choices
    o = replacedInDict(IMAGEDEFAULTS, options)
    correctTheTypes(o) # type string to whatever needed here.
    
    # get the fontface:
    fontfilename = findMatchingFont( o["font"], FONTS)
    fontface=os.path.join(FONTDIR, fontfilename)
    
    # allow empty heading
    if o["heading"]!="":
        fontHeading = ImageFont.truetype(fontface, o["headingfontsize"])
        headingLength=fontHeading.getsize(o["heading"])[0]
    else:
        headingLength=0
        o["headingfontsize"]=0

    # footer font & length:
    #print o["footerfontsize"]
    o["footerfontsize"] = minFooter(o["footerfontsize"], fontfilename) 
    #print o["footerfontsize"]
    fontFooter = ImageFont.truetype(fontface,o["footerfontsize"])
    footerLength = fontFooter.getsize(o["footer"])[0]
    
    # prepare text
    text  = cutUpText(text, o["maxcharsperline"])
    lines = text.rstrip().split("\n")
    
    # font -> line lengths -> pixel width & height
    font = ImageFont.truetype(fontface, o["fontsize"])
    lineDistance        = int ( o["fontsize"]         * o["linedistanceratio"] )
    lineDistanceHeading = int ( o["headingfontsize"]  * o["linedistanceratio"] )
    border       = lineDistance
    lineWidths = [font.getsize(line)[0] for line in lines]

    width=max(max(lineWidths), headingLength, footerLength) + 2 * border
    
    # this reflects the line structure of the image:  
    height =  border
    height += (o["headingfontsize"] + lineDistanceHeading) if o["heading"]!="" else 0
    height += (o["fontsize"]  + lineDistance) * len(lines) 
    height += int(0.5 * o["footerfontsize"]) + o["footerfontsize"] + border 
    
    # PIL stuff
    img  = Image.new("RGBA", (width, height), o["bgcolor"])
    draw = ImageDraw.Draw(img)
    
    startPos=lineDistance
    
    # heading:
    if o["heading"]!="":
        draw.text(((width-headingLength)/2, startPos),o["heading"],o["color"],font=fontHeading)
        startPos += o["headingfontsize"] + lineDistanceHeading 
        
    # main text:
    for i, line in enumerate(lines):
        draw.text(((width-lineWidths[i])/2, startPos),line,o["color"],font=font)
        startPos += o["fontsize"] + lineDistance
    
    # footer:
    startPos += int(0.5 * o["footerfontsize"])
    draw.text((width-footerLength-border, startPos),o["footer"],o["color"],font=fontFooter)
    
    # PIL stuff
    draw = ImageDraw.Draw(img)
    draw = ImageDraw.Draw(img)
    
    # save to file
    if filename.endswith("jpg"):
        img.save(filename, quality=o["jpgquality"])
    else:
        img.save(filename)