Beispiel #1
0
def layer02():
    draw = Drawing()
    draw.font = FONT_REGULAR
    draw.font_size = 144
    draw.fill_color = Color('rgb(216,224,34)')
    draw.text(0, 120, "21")

    draw.font = FONT_BLACK
    draw.text(170, 120, "CA")

    draw.font = FONT_BOLD
    draw.font_size = 18
    draw.text(480, 30, "2014/01")
   
    draw.font = FONT_REGULAR
    draw.font_size = 36
    draw.fill_color = Color('white')
    draw.text(0, 160, "twenty-first century astronomer")
 
    req_width = 570
    req_height = 170
    masthead_image = new_blank_png(req_width, req_height)
    draw(masthead_image)

#    opacity_mask = new_blank_png(570, 170, Color('rgb(200,200,200'))
#    masthead_image.composite_channel(channel='alpha', image=opacity_mask, operator='copy_opacity')

    return masthead_image
Beispiel #2
0
def layer05():
    req_width = 240
    req_height = 240
    contents_panel_text = new_blank_png(req_width, req_height)

    draw = Drawing()

    draw.font = FONT_BOLD
    draw.font_size = 18
    draw.fill_color = Color('black')
    draw.text(20, 45, "In this issue:")

    draw.font = FONT_ITALIC
    draw.font_size = 18
    draw.fill_color = Color('black')

    line_spacing = int(float(draw.font_size) * 1.22)
    (line_x, line_y) = (20, 81)
    lines = ["How to keep", "your toddler safe from",
        "unwanted event horizons",
        " ",
        "Best five telescopes", "for kids under six"]
    for l in lines:
        draw.text(line_x, line_y, l)
        line_y = line_y + line_spacing 

    draw(contents_panel_text)
    return contents_panel_text
Beispiel #3
0
def generate_meme(upper_text, lower_text, picture_name_orig,
                  picture_name_target):
    main_image = Image(filename=join(TEMPLATES_FOLDER, picture_name_orig))
    main_image.resize(
        1024,
        int(((main_image.height * 1.0) / (main_image.width * 1.0)) * 1024.0))

    upper_text = "\n".join(wrap(upper_text,
                                get_warp_length(main_image.width))).upper()
    lower_text = "\n".join(wrap(lower_text,
                                get_warp_length(main_image.width))).upper()
    lower_margin = MARGINS[lower_text.count("\n")]

    text_draw = Drawing()

    text_draw.font = join(getcwd(), "impact.ttf")
    text_draw.font_size = 70
    text_draw.text_alignment = "center"
    text_draw.stroke_color = Color("black")
    text_draw.stroke_width = 3
    text_draw.fill_color = Color("white")

    if upper_text:
        text_draw.text(int(main_image.width / 2), 80, upper_text)
    if lower_text:
        text_draw.text(int(main_image.width / 2),
                       main_image.height - lower_margin, lower_text)

    text_draw(main_image)

    main_image.save(filename=join(getcwd(), MEME_FOLDER, picture_name_target))
 def draw_text(self,
               image,
               x,
               y,
               text,
               font_size=15,
               font_style='normal',
               font=None,
               text_alignment='left',
               text_color='Black',
               filename=None,
               is_display=False):
     draw = Drawing()
     print('draw text={} in x={}, y={}'.format(text, x, y))
     if font is not None:
         draw.font = font
     draw.fill_color = Color(text_color)
     draw.font_size = font_size
     draw.font_style = font_style
     draw.text_alignment = text_alignment
     draw.text(x, y, text)
     draw(image)
     if is_display:
         display(image)
     if filename is not None:
         image.save(filename=filename)
     return image
Beispiel #5
0
def wand1():
    """This is Python Wand example 1"""
    the_time = t.asctime()

    print "Importing image ", IFILE
    img_1 = Image(filename=IFILE)

    print "Cropping and resizing the image"
    img_1.crop(300, 0, width=300, height=282)
    img_1.resize(width=600, height=564)

    print "Creating a drawing and overlaying on it"
    draw = Drawing()

    draw.circle((100, 100), (120, 120))

    draw.rectangle(left=img_1.width-300, top=img_1.height-45, width=230,
               height=40, radius=5)

    draw.font_size = 17
    draw.fill_color = Color('white')
    draw.text_color = Color('white')
    draw.text(img_1.width-290, img_1.height-20, the_time)
    draw(img_1)

    print "Displaying, close the XTERM when done"
    display(img_1)
Beispiel #6
0
async def remix_meeme(upper_text, lower_text, picture_name, endname):
    main_image = remiximage(filename=picture_name)
    main_image.resize(
        1024,
        int(((main_image.height * 1.0) / (main_image.width * 1.0)) * 1024.0))
    upper_text = "\n".join(wrap(upper_text,
                                get_warp_length(main_image.width))).upper()
    lower_text = "\n".join(wrap(lower_text,
                                get_warp_length(main_image.width))).upper()
    lower_margin = MARGINS[lower_text.count("\n")]
    text_draw = Drawing()
    text_draw.font = join(getcwd(),
                          "userbot/utils/styles/MutantAcademyStyle.ttf")
    text_draw.font_size = 100
    text_draw.text_alignment = "center"
    text_draw.stroke_color = Color("black")
    text_draw.stroke_width = 3
    text_draw.fill_color = Color("white")
    if upper_text:
        text_draw.text((main_image.width) // 2, 80, upper_text)
    if lower_text:
        text_draw.text((main_image.width) // 2,
                       main_image.height - lower_margin, lower_text)
    text_draw(main_image)
    main_image.save(filename=endname)
Beispiel #7
0
def animate_slice(sliceviewer,
                  name,
                  start,
                  end,
                  filename,
                  num_frames=10,
                  font_size=24):
    """Generate an animated gif of a 2D slice moving through a third dimension.

    Args:
        sliceviewer (SliceViewer): A sliceviewer instance.
        name (str): The name of the third dimension to use.
        start (float): The starting value of the third dimension.
        end (float): The end value of the third dimension.
        filename (str): The file to save the gif to.

    Kwargs:
        num_frames (int): The number of frames the gif should contain.
        font_size: (int): The size of the caption.

    Example:
        ws = CreateMDWorkspace(3, Extents=[-10,10,-10,10,-10,10], Names=["X","Y","Z"], Units=["u","u","u"])
        FakeMDEventData(ws, PeakParams=[10000,0,0,0,1])
        sv = plotSlice(ws)
        #Resize and configure the slice viewer how you want the output to look
        sv.setNormalization(1) # We need to normalize by volume in this case, or the data won't show up
        #This will create a gif iterating from Z = -1 to Z = 1
        animate_slice(sv, "Z", -1, 1, "output.gif")
    """
    #Generate all the individual frames
    images = []
    for slice_point in numpy.linspace(start, end, num_frames):
        sliceviewer.setSlicePoint(name, slice_point)
        sliceviewer.refreshRebin()
        qimg = sliceviewer.getImage().toImage()
        data = QByteArray()
        buf = QBuffer(data)
        qimg.save(buf, "PNG")
        image = Image(blob=str(data))
        captionstrip_size = font_size + 10
        #To add whitespace to the top, we add a vertical border,
        #then crop out the bottom border
        image.border(Color("#fff"), 0, captionstrip_size)
        image.crop(0, 0, image.width, image.height - captionstrip_size)
        #Write the caption into the whitespace
        draw = Drawing()
        draw.font_size = font_size
        draw.text(5, font_size, "%s = %g" % (name, slice_point))
        draw(image)
        images.append(image)
    #Create a new image with the right dimensions
    animation = Image(width=images[0].width, height=images[0].height)
    #Copy in the frames from all the generated images
    for image in images:
        animation.sequence.append(image.sequence[0])
    #Drop the initial blank frame
    del animation.sequence[0]
    #Write the animation to disk
    animation.save(filename=filename)
Beispiel #8
0
def layer03():
    draw = Drawing()
    draw.font = FONT_HAIRLINE
    draw.font_size = 69
    draw.fill_color = Color('white')
    draw.text(0, 69, "Liam Mulcahy")

    draw.font = FONT_LIGHT
    draw.font_size =50 
    draw.text(0, 125, "Our next Einstein?")
 
    req_width = 570
    req_height = 130
    blurb_image = new_blank_png(req_width, req_height)
    draw(blurb_image)

    return blurb_image
Beispiel #9
0
 def draw_watermark(self, img):
     watermark_draw = Drawing()
     watermark_draw.stroke_color = Color('black')
     watermark_draw.font = os.path.join(os.path.dirname(__file__), "Colombia-Regular.ttf")
     watermark_draw.font_size = 3 * POINT_PER_MM
     watermark_draw.text_antialias = True
     metrics = watermark_draw.get_font_metrics(img, WATERMARK)
     watermark_draw.text(max(0, math.floor((POINT_PER_MM * self.paper['width']) - metrics.text_width)),
                         max(0, math.floor((POINT_PER_MM * self.paper['height']) - metrics.text_height * 2)),
                         WATERMARK)
     watermark_draw.draw(img)
def animate_slice(sliceviewer, name, start, end, filename, num_frames=10, font_size=24):
    """Generate an animated gif of a 2D slice moving through a third dimension.

    Args:
        sliceviewer (SliceViewer): A sliceviewer instance.
        name (str): The name of the third dimension to use.
        start (float): The starting value of the third dimension.
        end (float): The end value of the third dimension.
        filename (str): The file to save the gif to.

    Kwargs:
        num_frames (int): The number of frames the gif should contain.
        font_size: (int): The size of the caption.

    Example:
        ws = CreateMDWorkspace(3, Extents=[-10,10,-10,10,-10,10], Names=["X","Y","Z"], Units=["u","u","u"])
        FakeMDEventData(ws, PeakParams=[10000,0,0,0,1])
        sv = plotSlice(ws)
        #Resize and configure the slice viewer how you want the output to look
        sv.setNormalization(1) # We need to normalize by volume in this case, or the data won't show up
        #This will create a gif iterating from Z = -1 to Z = 1
        animate_slice(sv, "Z", -1, 1, "output.gif")
    """
    #Generate all the individual frames
    images = []
    for slice_point in numpy.linspace(start, end, num_frames):
        sliceviewer.setSlicePoint(name, slice_point)
        sliceviewer.refreshRebin()
        qimg = sliceviewer.getImage().toImage()
        data = QByteArray()
        buf = QBuffer(data)
        qimg.save(buf, "PNG")
        image = Image(blob=str(data))
        captionstrip_size = font_size + 10
        #To add whitespace to the top, we add a vertical border,
        #then crop out the bottom border
        image.border(Color("#fff"), 0, captionstrip_size)
        image.crop(0, 0, image.width, image.height - captionstrip_size)
        #Write the caption into the whitespace
        draw = Drawing()
        draw.font_size = font_size
        draw.text(5, font_size,"%s = %g" % (name,slice_point))
        draw(image)
        images.append(image)
    #Create a new image with the right dimensions
    animation = Image(width=images[0].width, height=images[0].height)
    #Copy in the frames from all the generated images
    for image in images:
        animation.sequence.append(image.sequence[0])
    #Drop the initial blank frame
    del animation.sequence[0]
    #Write the animation to disk
    animation.save(filename=filename)
Beispiel #11
0
def slice01():
    req_width = 595
    req_height = 139
    masthead_image = new_blank_png(req_width, req_height, Color('white'))

    # Cannot yet draw a rectangle. Therefore to get the collection of boxes
    # and their colours, we create images that are then composited.
    box1 = new_blank_png(384, 139, Color('rgb(192,30,45)'))
    box2 = new_blank_png(173, 139, Color('rgb(224,137,145)'))
    masthead_image.composite(box1, 0, 0)
    masthead_image.composite(box2, 384, 0)
    
    draw = Drawing()
    draw.font = FONT_BOLD
    draw.font_size = 72
    draw.fill_color = Color('white')
    draw.text(169, 82, "entre")
    draw.text(60, 124, "preneur")
    
    draw.font = "../fonts/arvo/Arvo-Regular.ttf"
    draw.font_size = 115
    draw.fill_color = Color('black')
    draw.text(390, 125, "2.0")
    
    draw(masthead_image)

    # Cannot yet rotate the text in the same step as the drawing,
    # so draw rectangle with text and then rotate it.
    #
    box3 = new_blank_png(139, 39, Color('rgb(192,30,45)'))
    draw = Drawing()
    draw.font = "../fonts/arvo/Arvo-Regular.ttf"
    draw.font_size = 17
    draw.fill_color = Color('white')
    draw.text(5, 25, "November 2013")
    draw(box3)
    box3.rotate(-90)
    masthead_image.composite(box3, 557, 0)

    return masthead_image
Beispiel #12
0
def generate_meme(upper_text, lower_text, picture_name):
    MEME_FOLDER = "memes"
    MARGINS = [25, 65, 100, 135, 170]

    if not exists(join(getcwd(), MEME_FOLDER)):
        mkdir(join(getcwd(), MEME_FOLDER))

    main_image = Image(filename=picture_name)
    main_image.resize(
        500, int(
            ((main_image.height * 1.0) / (main_image.width * 1.0)) * 500.0))

    upper_text = "\n".join(wrap(upper_text,
                                get_warp_length(main_image.width))).upper()
    lower_text = "\n".join(wrap(lower_text,
                                get_warp_length(main_image.width))).upper()
    lower_margin = MARGINS[lower_text.count("\n")]

    text_draw = Drawing()

    text_draw.font = join(getcwd(), "impact.ttf")
    text_draw.font_size = 40
    text_draw.text_alignment = "center"
    text_draw.stroke_color = Color("black")
    text_draw.stroke_width = 3
    text_draw.fill_color = Color("white")

    if upper_text:
        text_draw.text(main_image.width / 2, 40, upper_text)
    if lower_text:
        text_draw.text(main_image.width / 2, main_image.height - lower_margin,
                       lower_text)

    text_draw(main_image)

    outname = "[MEME] " + picture_name
    main_image.save(filename=join(getcwd(), MEME_FOLDER, outname))

    # if sys.platform.startswith('darwin'):
    #     system('open "%s"' % (join(getcwd(), MEME_FOLDER, outname)))
    # elif name == 'nt':
    #     system('start "%s"' % (join(getcwd(), MEME_FOLDER, outname)))
    # elif name == 'posix':
    #     system('xdg-open "%s"' % (join(getcwd(), MEME_FOLDER, outname)))
    # else:
    #     pass
    return MEME_FOLDER + '/' + outname
Beispiel #13
0
def render_text_line(text, font, size, weight, color=Color('none'), background=None):
    match = re.match("(\w+)(-\w+)?", weight)
    italic = None
    if match:
        weight = match.group(1)
        italic = match.group(2)
    if not italic:
        italic = ""

    #
    # Special case: blank line
    #
    if text == "":
        graphic = new_blank_png(width=5, height=5)
        return (5, 5, graphic)
    
    sequence = lex_line(text, weight)

    images = []
    dummy_image = Image(width=100, height=100, background=None)
    for (level, text) in sequence:
        draw = Drawing()
        draw.font = FONT_INFO[font][level+italic]
        draw.font_size = size
        draw.fill_color = color
        metrics = draw.get_font_metrics(dummy_image, text, False)
        image = new_blank_png(width=int(metrics.text_width), height=int(metrics.y2-metrics.y1))
        draw.text(0, int(metrics.y2), text)
        draw(image)
        images.append((metrics.y2, metrics.y1, image))

    if images == []:
        return None

    max_ascender = max([y2 for (y2,_,_) in images])
    max_descender = -1 * min([y1 for (_,y1,_) in images])
    final_image_width = sum([i.width for (_,_,i) in images])
    final_image_height = int(max_ascender + max_descender)
    final_image = new_blank_png(width=final_image_width, height=final_image_height,
        color=background)

    top_offset = 0
    for (y2,y1,i) in images:
        final_image.composite(i, top_offset, int(max_ascender-y2))
        top_offset = top_offset + i.width

    return (max_ascender, max_descender, final_image)
Beispiel #14
0
 def _caption_seinfeld(self):
     caption = self.text
     drawing = Drawing()
     drawing.font = 'assets/fonts/NimbusSanL-RegIta.otf'
     drawing.font_size = self.width / 14
     text = fill(caption, 25)
     drawing.fill_color = Color('#000')
     drawing.translate(10, self.height / 12)
     # the number of shadows depends on image width, with a min of 1
     shadows = max(int(self.width / 150), 1)
     for _ in range(shadows):
         drawing.translate(-1, 1)
         drawing.gravity = 'south'
         drawing.text(0, 0, text)
     drawing.fill_color = Color('#fff')
     drawing.text(0, 0, text)
     return caption, drawing
Beispiel #15
0
 async def create_text_image(self, ctx: Context, person: str, text: str):
     """
     Creates an image of a given person with the specified text.
     """
     lines = textwrap.wrap(text, 15)
     draw = Drawing()
     image = Image(filename=f"bot/resources/{person}SaysBlank.png")
     draw.font = "bot/resources/Dosis-SemiBold.ttf"
     draw.text_alignment = "center"
     draw.font_size = 34
     offset = 45 - 10 * len(lines)
     for line in lines:
         draw.text(image.width // 5 + 20, image.height // 5 + offset, line)
         offset += 35
     draw(image)
     image.save(filename=f"bot/resources/{person}Says.png")
     await ctx.send(file=File(f"bot/resources/{person}Says.png"))
Beispiel #16
0
    def make_og_meme(self, top, bottom):
        """Generate an OG Meme
        """

        wand_t = Image(blob=self.image.read())
        MARGINS = [50, 130, 200, 270, 340]

        # Set a minimum size
        wand_t.resize(
            1024, int(((wand_t.height * 1.0) / (wand_t.width * 1.0)) * 1024.0))

        use_top = True
        use_bottom = True

        if top == ' ':
            use_top = False
        if bottom == ' ':
            use_bottom = False

        if use_top:
            upper_text = "\n".join(
                wrap(top, self.get_warp_length(int(wand_t.width)))).upper()
        if use_bottom:
            lower_text = "\n".join(
                wrap(bottom, self.get_warp_length(int(wand_t.width)))).upper()
            lower_margin = MARGINS[lower_text.count("\n")]

        text_draw = Drawing()

        text_draw.font = "fonts/Anton-Regular.ttf"
        text_draw.font_size = 70
        text_draw.text_alignment = "center"
        text_draw.stroke_color = Color("black")
        text_draw.stroke_width = 3
        text_draw.fill_color = Color("white")

        if use_top:
            text_draw.text(int(wand_t.width / 2), 80, upper_text)

        if use_bottom:
            text_draw.text(int(wand_t.width / 2),
                           int(wand_t.height - lower_margin), lower_text)

        text_draw(wand_t)

        return (wand_t)
Beispiel #17
0
 def _caption_sanandreas(self):
     caption = self.text
     drawing = Drawing()
     drawing.font = 'assets/fonts/TwCenMTStd-ExtraBold.otf'
     drawing.font_size = self.width / 20
     drawing.text_interline_spacing = drawing.font_size / 5
     drawing.fill_opacity = 0.8
     drawing.gravity = 'south'
     text = fill(caption, 30)
     drawing.fill_color = Color('#000')
     offset = drawing.font_size / 12
     drawing.translate(offset, self.height / 15)
     drawing.text(0, 0, text)
     drawing.translate(-offset, offset)
     drawing.fill_color = Color('#eee')
     drawing.text(0, 0, text)
     return caption, drawing
Beispiel #18
0
 def display_boxes(self,
                   tree,
                   html_path,
                   filename_prefix,
                   alternate_colors=False):
     """
     Displays each of the bounding boxes passed in 'boxes' on images of the pdf
     pointed to by pdf_file
     boxes is a list of 5-tuples (page, top, left, bottom, right)
     """
     imgs = []
     colors = {
         "section_header": Color('blue'),
         "figure": Color('green'),
         "figure_caption": Color('green'),
         "table_caption": Color('red'),
         "list": Color('yellow'),
         "paragraph": Color('gray'),
         "table": Color('red'),
         "header": Color("brown")
     }
     for i, page_num in enumerate(tree.keys()):
         img = self.pdf_to_img(page_num)
         draw = Drawing()
         draw.fill_color = Color('rgba(0, 0, 0, 0.0)')
         for clust in tree[page_num]:
             for (pnum, pwidth, pheight, top, left, bottom,
                  right) in tree[page_num][clust]:
                 draw.stroke_color = colors[clust]
                 draw.rectangle(left=left,
                                top=top,
                                right=right,
                                bottom=bottom)
                 draw.push()
                 draw.font_size = 20
                 draw.font_weight = 10
                 draw.fill_color = colors[clust]
                 if int(left) > 0 and int(top) > 0:
                     draw.text(x=int(left), y=int(top), body=clust)
                 draw.pop()
         draw(img)
         img.save(filename=html_path + filename_prefix + "_page_" + str(i) +
                  '.png')
         imgs.append(img)
     return imgs
Beispiel #19
0
    def make_frame(self, streams):
        frame = WandImage(width=self.frame_d[0],background=Color('white'), height=self.frame_d[1])   

        for n, stream in enumerate(streams):
            img = WandImage(file=stream)
            x = config.photo_w * ((n >> 1) & 1)
            y = config.photo_h * (n & 1)
            frame.composite(img, left=x+18, top=y+18)
            drawing = WandDrawing()
#            drawing.font = '/home/pi/booth4/fonts/Steelworks.otf'
            drawing.font = '/home/pi/booth6/fonts/Vulturemotor.otf'
            drawing.font_size = 20
#            drawing.font_style = 'italic'
#            drawing.fill_color = Color('orange')
#            drawing.stroke_color = Color('brown')
            drawing.text(310, 660, 'The Mighty Booth') 
            drawing(frame)
        return frame
Beispiel #20
0
def generar_meme(texto, nombre_imagen):
    imagen = Image(filename=nombre_imagen)
    imagen.resize(1024,
                  int(((imagen.height * 1.0) / (imagen.width * 1.0)) * 1024.0))

    texto = "\n".join(wrap(texto, get_length(imagen.width))).upper()

    text_draw = Drawing()

    text_draw.font = os.path.join(os.getcwd(), "impact.ttf")
    text_draw.font_size = 60
    text_draw.text_alignment = "center"
    text_draw.stroke_color = Color("black")
    text_draw.stroke_width = 3
    text_draw.fill_color = Color("white")

    if texto:
        text_draw.text(imagen.width * 60//100, imagen.height * 55//100, texto)

    text_draw(imagen)

    imagen.save(filename=os.path.join(os.getcwd(), "memes", "meme.jpg"))
Beispiel #21
0
 def _caption_akari(self):
     caption = 'わぁい{0} あかり{0}大好き'.format(self.text)
     drawing = Drawing()
     drawing.font = 'assets/fonts/rounded-mgenplus-1c-bold.ttf'
     drawing.font_size = self.width / 15
     text = fill(caption, 23)
     drawing.gravity = 'south'
     drawing.text_interline_spacing = drawing.font_size / -5
     offset = max(self.width / 400, 2)
     # first the shadow
     drawing.translate(offset, -offset)
     drawing.fill_color = Color('#000')
     drawing.fill_opacity = 0.5
     drawing.text(0, 0, text)
     # then the text
     drawing.translate(-offset, offset)
     drawing.fill_color = Color('#fff')
     drawing.fill_opacity = 1.0
     drawing.stroke_color = Color('#000')
     drawing.stroke_width = max(self.width / 600, 1)
     drawing.text(0, 0, text)
     return caption, drawing
Beispiel #22
0
    def process(self, area: os.DirEntry, city: os.DirEntry, pic: os.DirEntry,
                save_dir: str):
        # os.DirEntry.name is the picture's filename
        filename = pic.name

        # Store the absolute path of the current directory
        # so that we can return to it when we're done,
        # so that the calling function doesn't end up
        # with a working directory that is not the same
        # as the one it had before the call to process()
        return_dir = os.path.abspath(os.curdir)

        # os.path.splitext gives us both the file name and
        # the extension of the picture. We need the extension
        # because we're going to use it to tell Wand
        # (and, in turn, ImageMagick) what extension
        # to give to the image and we want to retain
        # the original one
        (name, extension) = os.path.splitext(pic)
        text = f'{city.name}({area.name})'
        save_name = f'{self.i}-{text}{extension}'

        if self.edit:
            with Image(filename=filename) as image:
                draw = Drawing()
                draw.fill_color = "white"
                draw.font_size = 90
                draw.gravity = "south_east"
                draw.text(30, 30, text)
                draw.draw(image)
                os.chdir(save_dir)
                image.save(filename=save_name)
            self.i = self.i + 1
        else:
            copyfile(pic.name, f"{save_dir}/{save_name}")

        os.chdir(return_dir)
Beispiel #23
0
def slice03():
    full_slice = new_blank_png(595, 182)

    # Two solid-colour areas, one on left and one on right
    #
    left_field = new_blank_png(299, 182, color=Color('rgb(44,128,64)'))
    right_field = new_blank_png(297, 182, color=Color('rgb(127,184,141)'))
    full_slice.composite(left_field, 0, 0)
    full_slice.composite(right_field, 299, 0)
   
    draw = Drawing()

    # Text on the left field
    #
    draw.font = FONT_BOLD
    draw.font_size = 18
    draw.fill_color = Color('white')
    draw.text(30, 85, "Smartphone Babyproofing")

    # Unfortunately don't yet have strokewidth
    #
    draw.line((29,95), (298,95))
    draw.line((29,96), (298,96))
    draw.line((29,97), (298,97))

    draw.font = FONT_REGULAR
    draw.font_size = 18
    draw.text(30, 125, "Tips on how to use those")
    draw.text(30, 147, "safety features you")
    draw.text(30, 169, "always forget to enable...")

    # Text on the right field
    #
    draw.font = FONT_BOLD
    draw.font_size = 18
    draw.fill_color = Color('black')
    draw.text(328, 85, "School savings")

    # Yada yada yada ...
    #
    draw.line((328,95), (595,95))
    draw.line((328,96), (595,96))
    draw.line((328,97), (595,97))

    draw.font = FONT_REGULAR
    draw.font_size = 18
    draw.text(328, 125, "Successful $$$ strategies")  ## Still have euro-symbol issues
    draw.text(328, 147, "for getting junior into the")
    draw.text(328, 169, "best business school")

    # ... and now drawing the text 
    #
    draw(full_slice)

    # Add the accent images on top
    #
    graphics_slice = new_blank_png(595, 182)
    
    left_image = Image(filename = IMAGE_PATH + "smartphone.png")
    left_image = resize_to_width(left_image, 299)

    right_image = Image(filename = IMAGE_PATH + "building.png")
    right_image = resize_to_width(right_image, 298)

    graphics_slice.composite(left_image, 0, 0)
    graphics_slice.composite(right_image, 299, 0)
    opacity_mask = new_blank_png(595, left_image.height, color=Color('rgb(75,75,75)'))
    graphics_slice.composite_channel(channel='all_channels', image=opacity_mask, 
        operator='copy_opacity')

    full_slice.composite_channel(channel='all_channels', image=graphics_slice, 
        operator='over', top=0, left=0)
 
    # Done.
    #
    return full_slice
Beispiel #24
0
def slice02():
    # Resize image to height (but do not change the aspect ratio)
    #
    req_width = 595
    req_height = 486
    baby_img = Image(filename=IMAGE_PATH + "baby_cc_sh.png")
    baby_img = resize_to_height(baby_img, req_height)

    # For this particular image, reflect image along vertical
    # axis to have face directed towards right of page.
    #
    baby_img.flop()

    # Create the the gradient transition that will later be used
    # in the opacity mask.
    #
    gradient_blob = create_gradient_blob(req_height, 75, gradient([
        (1.0, (0x00, 0x00, 0x00), (0xFF, 0xFF, 0xFF)), # top
    ]))
    gradient_img = Image(blob=gradient_blob)
    gradient_img.rotate(90.0)

    # When building the opacity mask, must start with an image
    # having a solid colour (i.e., begin as a "matte" image).
    # Without this, the later "composite_channel" operations
    # will simply not work.
    #
    opacity_mask = new_blank_png(req_width, req_height, color=Color('white'))
    left_field = new_blank_png(230, req_height, Color('white'))
    right_field = new_blank_png(290, req_height, Color('black'))
    opacity_mask.composite(left_field, 0, 0)
    opacity_mask.composite(right_field, 230+75, 0)
    opacity_mask.composite(gradient_img, 230, 0)

    # Now take the resized baby image and have it fade to the right
    # (in order to blend with later operations).
    #
    face_img = new_blank_png(req_width, req_height)
    face_img.composite(baby_img, 0, 0)
    face_img.composite_channel(channel='all_channels', image=opacity_mask, 
        operator='copy_opacity')

    # Bring in the illustrative image that will eventually be blended in
    # with the child's face.
    #
    accent_img = Image(filename = IMAGE_PATH + "funky_illustration.png")
    accent_img = resize_to_percent(accent_img, 60)
    accent_img = resize_to_height(accent_img, 486)

    cropped_img = accent_img.clone()
    cropped_img.crop(top=0, left=275, width=340, height=486)
    screen_img = new_blank_png(340, 486, color=Color('rgb(150,150,150)'))
    cropped_img.composite_channel(channel='all_channels', image=screen_img, 
        operator='screen')

    accent_img = new_blank_png(req_width, req_height)
    accent_img.composite_channel(channel='all_channels', image=cropped_img, 
        operator='over', left=255, top=0)
    accent_img.gaussian_blur(3.0, 1.0)

    opacity_mask = new_blank_png(req_width, req_height, color=Color('white'))
    left_field = new_blank_png(260, req_height, Color('black'))
    right_field = new_blank_png(290, req_height, Color('white'))
    opacity_mask.composite(left_field, 0, 0)
    opacity_mask.composite(right_field, 260+75, 0)
    gradient_img.rotate(180)
    opacity_mask.composite(gradient_img, 260, 0)
    accent_img.composite_channel(channel='all_channels', image=opacity_mask, 
        operator='copy_opacity')

    # Now layer the accent image with the child's face
    #
    accent_img.composite_channel(channel='all_channels', image=face_img,
        operator='over')
    full_slice = accent_img 

    # Finally, add the text field on the right of the image.
    #
    text_field = new_blank_png(212, req_height, color=Color('rgb(190,30,45)'))
    text_field_mask = new_blank_png(212, req_height, color=Color('rgb(220,220,220)'))
    text_field.composite_channel(channel='all_channels', image=text_field_mask, 
        operator='copy_opacity')
    full_slice.composite(text_field, 384, 0)

    draw = Drawing()
    draw.font = FONT_BOLD
    draw.font_size = 24
    draw.fill_color = Color('white')
    draw.text(395, 175, "Liam Mulcahy")
    draw.font = FONT_REGULAR
    draw.font_size = 20
    draw.text(395, 200, "Eyes to the Future")
    draw.font = FONT_ITALIC
    draw.font_size = 20
    draw.text(395, 250, 'How dreams of')
    draw.text(395, 275, 'future enterprise')
    draw.text(395, 300, 'success are')
    draw.text(395, 325, 'starting while still')
    draw.text(395, 350, 'in nappies!')
    draw(full_slice)
   
    # Done.
    # 
    return full_slice
Beispiel #25
0
import yaml
import re

from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing
from wand.display import display

#full_text = ["The first line", "and a second line", "... finally a third line."]
full_text = ["The first line"]
FONT_SIZE = 48

dummy_image = Image(width=100, height=100, background=Color('black'))
draw = Drawing()
draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf'
draw.font_size = FONT_SIZE
draw.fill_color = Color('black')


rendered_segments = []
for text in full_text:
    metrics = draw.get_font_metrics(dummy_image, text, False)
    yMax = int(metrics.y2)
    yMin = -1 * int(metrics.y1)
    segment_width = int(metrics.text_width)
    draw = Drawing()
    draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf'
    draw.font_size = FONT_SIZE
    draw.fill_color = Color('black')
    draw.text(0, yMax, text)
    image = Image(width=segment_width, height=yMax+yMin, background=None)
Beispiel #26
0
    initialPointsize = 45

    draw = Drawing()
    draw.font = randomFont

    tmp = int(math.floor(abs(random.gauss(0, 1))*6))
    if random.randint(1, 2) == 1:
        rotateX = random.randint(0, tmp)
    else:
        rotateX = random.randint(360-tmp, 360)

    draw.rotate(rotateX)
    # --------------------------------------------------------------
    # --------get suitable FontPointSize----------------------------
    draw.font_size = initialPointsize
    metric = draw.get_font_metrics(image=baseImage, text=word)

    while metric.text_width > 100 or metric.text_height > 36:
        initialPointsize -= 5
        draw.font_size = initialPointsize
        metric = draw.get_font_metrics(image=baseImage, text=word)
    # --------------------------------------------------------------

    # ----------italic----------------------------------------------
    if random.random() > 0.5:
        draw.font_style = 'italic'
    # --------------------------------------------------------------

    # ----------underline-------------------------------------------
    if random.random() > 0.5:
def generate(count):
    # ---------------get three colors-------------------------------
    colorString = random.choice(result)
    color = []
    for i in colorString:
        color += [int(i)]
    # for i in range(len(color)):
    #     color[i] = math.floor(color[i]/255*65535)
    color1 = color[0:3]
    color2 = color[3:6]
    color3 = color[6:9]
    # --------------------------------------------------------------

    # ----------get the base layer texture--------------------------
    Scenes = pathwalk('./SceneData')

    randomScene = random.choice(Scenes)
    randomScene = randomScene[0] + '/' + randomScene[1]
    print(randomScene)
    randomSceneImage = Image(filename=randomScene)

    widthRange = randomSceneImage.size[0] - 100
    heightRange = randomSceneImage.size[1] - 32

    randomSceneImage.crop(left=random.randint(0, widthRange), top=random.randint(0, heightRange), width=100, height=32)
    # randomSceneImage.save(filename='.\\photoWand\\'+str(j+1) + '_texture.jpg')
    # --------------------------------------------------------------

    # ----------create the base layer, base texture +base color-----

    baseImage = Image(width=100, height=32, background=Color('rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')'))

    # print('base_color = ' + 'rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')')
    baseImage.composite_channel(channel='undefined', image=randomSceneImage, operator='blend', left=0, top=0)
    baseImage.gaussian_blur(4, 10)
    baseImage.resolution = (96, 96)
    # --------------------------------------------------------------

    # -----generate font--------------------------------------------
    word = randomWords()
    fonts = pathwalk('./fonts/font_en/')
    randomFont = random.choice(fonts)
    randomFont = randomFont[0] + randomFont[1]

    initialPointsize = 45

    draw = Drawing()
    draw.font = randomFont

    tmp = int(math.floor(abs(random.gauss(0, 1))*6))
    if random.randint(1, 2) == 1:
        rotateX = random.randint(0, tmp)
    else:
        rotateX = random.randint(360-tmp, 360)

    draw.rotate(rotateX)
    # --------------------------------------------------------------
    # --------get suitable FontPointSize----------------------------
    draw.font_size = initialPointsize
    metric = draw.get_font_metrics(image=baseImage, text=word)

    while metric.text_width > 100 or metric.text_height > 36:
        initialPointsize -= 5
        draw.font_size = initialPointsize
        metric = draw.get_font_metrics(image=baseImage, text=word)
    # --------------------------------------------------------------

    # ----------italic----------------------------------------------
    if random.random() > 0.5:
        draw.font_style = 'italic'
    # --------------------------------------------------------------

    # ----------underline-------------------------------------------
    if random.random() > 0.5:
        draw.text_decoration = 'underline'
    # --------------------------------------------------------------

    # ----------gravity---------------------------------------------
    draw.gravity = 'center'
    # --------------------------------------------------------------

    # --------------shadow/border-----------------------------------
    if random.random() < 0.5:
        # shadow
        addx = math.ceil(random.gauss(0, 2))
        addy = math.ceil(random.gauss(0, 2))
        draw.fill_color = Color('black')
        draw.text(x=abs(int(addx)), y=abs(int(addy)), body=word)

    else:
        # border
        draw.stroke_color = Color('rgb('+str(color3[0])+','+str(color3[1])+','+str(color3[2])+')')
        draw.stroke_width = math.ceil(initialPointsize/10)-1
    # --------------------------------------------------------------

    # ----------print word------------------------------------------
    draw.fill_color = Color('rgb('+str(color2[0])+','+str(color2[1])+','+str(color2[2])+')')
    draw.text(x=0, y=0, body=word)
    draw.draw(baseImage)
    # --------------------------------------------------------------

    # ------------gray----------------------------------------------
    baseImage.colorspace = 'gray'
    # --------------------------------------------------------------

    print(word)
    baseImage.save(filename='./photo_en/'+str(count+1)+'_'+word+'.jpg')
Beispiel #28
0
def banner(temp, loss, resultplt=None, date=""):
    txt_color = "white"
    with Image(width=140, height=230, background="transparent") as img:
        draw = Drawing()
        draw.font = 'font/OpenSans-Bold.ttf'
        draw.font_size = 14
        draw.font_antialias = True
        draw.text_alignment = 'center'
        draw.fill_color = txt_color
        draw.text(70, 14, 'Moscow')
        draw(img)

        draw = Drawing()
        draw.font = 'font/OpenSans-Bold.ttf'
        draw.font_size = 10
        draw.font_antialias = True
        draw.text_alignment = 'center'
        draw.fill_color = txt_color
        draw.text(70, 28, date)
        draw(img)

        draw = Drawing()
        draw.font = 'font/OpenSans-SemiBold.ttf'
        draw.font_size = 50
        draw.font_antialias = True
        draw.text_alignment = 'center'
        draw.fill_color = txt_color
        draw.text(70, 80, "%+d" % float(temp) + '°')
        draw(img)

        if resultplt:
            image_data = BytesIO()
            resultplt.axis('off')
            resultplt.gcf().set_size_inches(1.4, 0.7)
            resultplt.gcf().set_dpi(100)
            resultplt.tight_layout()
            resultplt.savefig(image_data, format='png', transparent=True)
            image_data.seek(0)
            result_image = Image(file=image_data)
            img.composite(image=result_image, left=0, top=110)

            draw = Drawing()
            draw.font = 'font/OpenSans-Bold.ttf'
            draw.font_size = 14
            draw.font_antialias = True
            draw.text_alignment = 'center'
            draw.fill_color = txt_color
            draw.text(70, 115, '2020')
            draw(img)

        for i, t in enumerate([
                "loss: " + str(loss), "input: 9 params",
                "train data: 16430 rows"
        ]):
            draw = Drawing()
            draw.font = 'font/OpenSans-Light.ttf'
            draw.font_size = 10
            draw.font_antialias = True
            draw.fill_color = txt_color
            draw.text(4, 190 + (i * 12), t)
            draw(img)

        img.save(filename='weather.png')
Beispiel #29
0
from wand.drawing import Drawing
Beispiel #30
0
import argparse
import sys
import yaml
import re

from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing
from wand.display import display

image_width = 400
image_height = 60

image = Image(width=image_width, height=image_height, background=Color('white'))

draw = Drawing()
draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOTF/TTF/Lato-Reg.ttf'
draw.font_size = 36
draw.fill_color = Color('black')
metrics = draw.get_font_metrics(image, "losing photos", False)

pos_col = int((image_width - metrics.text_width)/2)
pos_row = int(metrics.ascender + (image_height - metrics.text_height)/2) 

print "DEBUG ", pos_col, pos_row

draw.text(pos_col, pos_row, "losing photos")
draw(image)

image.save(filename="_00.png")
Beispiel #31
0
    initialPointsize = 45

    draw = Drawing()
    draw.font = randomFont

    tmp = int(math.floor(abs(random.gauss(0, 1)) * 6))
    if random.randint(1, 2) == 1:
        rotateX = random.randint(0, tmp)
    else:
        rotateX = random.randint(360 - tmp, 360)

    draw.rotate(rotateX)
    # --------------------------------------------------------------
    # --------get suitable FontPointSize----------------------------
    draw.font_size = initialPointsize
    metric = draw.get_font_metrics(image=baseImage, text=word)

    while metric.text_width > 100 or metric.text_height > 36:
        initialPointsize -= 5
        draw.font_size = initialPointsize
        metric = draw.get_font_metrics(image=baseImage, text=word)
    # --------------------------------------------------------------

    # ----------italic----------------------------------------------
    if random.random() > 0.5:
        draw.font_style = 'italic'
    # --------------------------------------------------------------

    # ----------underline-------------------------------------------
    if random.random() > 0.5:
Beispiel #32
0
from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing
from wand.display import display

weight_sequence = ['ultralight', 'light',
    'regular', 'bold', 'black']

image_width = 400
image_height = 60

dummy_image = Image(width=image_width, height=image_height, background=Color('white'))

draw = Drawing()
draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf'
draw.font_size = 36
draw.fill_color = Color('black')
metrics_01 = draw.get_font_metrics(dummy_image, "21", False)

draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Bla.ttf'
draw.font_size = 36
draw.fill_color = Color('black')
metrics_02 = draw.get_font_metrics(dummy_image, "CV", False)

image_01 = Image(width=int(metrics_01.text_width),
    height=int(metrics_01.text_height), background=None)
draw_01 = Drawing()
draw_01.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf'
draw_01.font_size = 36
draw_01.fill_color = Color('black')
draw_01.text(0, int(metrics_01.text_height), "21")
Beispiel #33
0
		img = Image(filename=Settings['FTPFile'])
		OverImg = Image(filename='/boot/TMLogo.png')

		draw = Drawing()
		draw.composite(operator='over',left=img.width - OverImg.width - 5,top=5,width=OverImg.width,height=OverImg.height,image=OverImg)
		draw(img)

                draw = Drawing()
                draw.fill_color = Color('blue')
		draw.fill_opacity = 0.5
		draw.rectangle(0,img.height - 30,img.width,img.height)
                draw(img)

		draw = Drawing()
		draw.font = 'wandtests/assets/League_Gothic.otf'
		draw.font_size = 20
		draw.fill_color = Color('white')
		draw.text_alignment = 'left'
		draw.text(5, img.height - 5, Settings['Description'])
		draw(img)

		draw = Drawing()
		draw.font = 'wandtests/assets/League_Gothic.otf'
		draw.font_size = 20
		draw.fill_color = Color('white')
		draw.text_alignment = 'right'
		draw.text(img.width - 5, img.height - 5, dt.datetime.now().strftime('%d-%m-%Y %H:%M:%S'))
		draw(img)

		img.format = 'jpeg'
		img.save(filename=Settings['FTPFile'])
Beispiel #34
0
from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing

# Create a blank image
image_width = 300
image_height = 400
img = Image(width=image_width, height=image_height, background=Color('rgb(44,128,64)'))

# Need a drawing object for rendering.
# Here there are some canvas (?) settings being established
d = Drawing()
d.fill_color = Color('rgb(255,255,255)')
d.font = 'Arvo-Regular.ttf'
d.font_size = 48

# Want some info on how big the text will be
# when drawn, then use it for calculations
#
fm = d.get_font_metrics(img, 'Hello!')
height = fm.text_height
width = fm.text_width
pos_x = int((image_width - width) / 2)
pos_y = int((image_height) / 2)

# Specify the coordinate of the lower-left
# position of the text (where 0,0 is the
# upper-left hand corner o the canvas).
#
d.text(pos_x, pos_y, 'Hello!')
Beispiel #35
0
def main():
    args = get_args()

    draw = Drawing()
    draw.font = args.font_file
    draw.font_size = args.font_size

    font_name = args.font_name
    out_dir = args.out_dir

    img_ref = Image(width=1000, height=1000)

    if args.verbose:
        print "Writing " + out_dir + "/" + font_name + ".c"
    f = open(out_dir + "/" + font_name + ".c", 'wb+')
    write_comment(f)
    f.write("#include \"font.h\"\n\n")

    font_height = 0
    range_first = 0x20
    range_last = 0x7d
    font_width = []
    max_width = 0
    for x in range(range_first, range_last + 1):
        letter = chr(x)
        metrics = draw.get_font_metrics(img_ref, letter)
        text_height = int(round(metrics.text_height + 2))
        if font_height == 0:
            font_height = text_height
        assert (font_height == text_height), "font height changed!"
        if max_width == 0:
            max_width = metrics.maximum_horizontal_advance + 2
        assert (max_width == metrics.maximum_horizontal_advance + 2), \
         "font advance width changed!"
        text_width = int(round(metrics.text_width + 2))
        font_width.append(text_width)
        img = Image(width=text_width, height=text_height)
        d = draw.clone()
        d.text(0, int(metrics.ascender), letter)
        d(img)

        img.depth = 1

        f.write("static const unsigned char ")
        f.write("letter_" + str(hex(x)[2:]) + "[] = {\n")
        c_hex_print(f, img.make_blob(format='A'))
        f.write("};\n\n")
        img.close()

    f.write("static const struct font_letter letters[] = {\n")
    for x in range(range_first, range_last + 1):
        letter_var_name = "letter_" + str(hex(x)[2:])
        f.write("\t{ " + letter_var_name + ", ")
        f.write("sizeof(" + letter_var_name + "), ")
        f.write(str(font_width[x - range_first]) + "},\n")
    f.write("};\n\n")

    f.write("const struct font font_" + font_name + " = {\n")
    f.write("\t.first = " + str(hex(range_first)) + ",\n")
    f.write("\t.last = " + str(hex(range_last)) + ",\n")
    f.write("\t.letters = letters,\n")
    f.write("\t.height = " + str(font_height) + ",\n")
    f.write("\t.max_width = " + str(max_width) + ",\n")
    f.write("};\n")
    f.close()

    if args.verbose:
        print "Writing " + out_dir + "/" + font_name + ".h"
    f = open(out_dir + "/" + font_name + ".h", 'wb+')
    write_comment(f)
    f.write("#ifndef __" + font_name.upper() + "_H\n")
    f.write("#define __" + font_name.upper() + "_H\n")
    f.write("#include \"font.h\"\n")
    f.write("extern const struct font font_" + font_name + ";\n")
    f.write("#endif /*__" + font_name.upper() + "_H*/\n")
    f.close()
Beispiel #36
0
def main():
	args = get_args()

	draw = Drawing()
	draw.font = args.font_file
	draw.font_size = args.font_size

	font_name = args.font_name
	out_dir = args.out_dir

	img_ref = Image(width=1000, height=1000)

	if args.verbose:
		print "Writing " + out_dir + "/" + font_name + ".c"
	f = open(out_dir + "/" + font_name + ".c", 'wb+')
	write_comment(f)
	f.write("#include \"font.h\"\n\n")

	font_height = 0
	range_first = 0x20
	range_last = 0x7d
	font_width = []
	max_width = 0
	for x in range(range_first, range_last + 1):
		letter = chr(x)
		metrics = draw.get_font_metrics(img_ref, letter)
		text_height = int(round(metrics.text_height + 2))
		if font_height == 0:
			font_height = text_height
		assert (font_height == text_height), "font height changed!"
		if max_width == 0:
			max_width = metrics.maximum_horizontal_advance + 2
		assert (max_width == metrics.maximum_horizontal_advance + 2), \
			"font advance width changed!"
		text_width = int(round(metrics.text_width + 2))
		font_width.append(text_width)
		img = Image(width=text_width, height=text_height)
		d = draw.clone()
		d.text(0, int(metrics.ascender), letter)
		d(img)

		img.depth = 1;

		f.write("static const unsigned char ")
		f.write("letter_" + str(hex(x)[2:]) + "[] = {\n")
		c_hex_print(f, img.make_blob(format='A'))
		f.write("};\n\n")
		img.close()

	f.write("static const struct font_letter letters[] = {\n")
	for x in range(range_first, range_last + 1):
		letter_var_name = "letter_" + str(hex(x)[2:])
		f.write("\t{ " + letter_var_name + ", ")
		f.write("sizeof(" + letter_var_name + "), ")
		f.write(str(font_width[x - range_first]) + "},\n")
	f.write("};\n\n")

	f.write("const struct font font_" + font_name + " = {\n")
	f.write("\t.first = " + str(hex(range_first)) + ",\n")
	f.write("\t.last = " + str(hex(range_last)) + ",\n")
	f.write("\t.letters = letters,\n")
	f.write("\t.height = " + str(font_height) + ",\n")
	f.write("\t.max_width = " + str(max_width) + ",\n")
	f.write("};\n")
	f.close()

	if args.verbose:
		print "Writing " + out_dir + "/" + font_name + ".h"
	f = open(out_dir + "/" + font_name + ".h", 'wb+')
	write_comment(f)
	f.write("#ifndef __" + font_name.upper() + "_H\n");
	f.write("#define __" + font_name.upper() + "_H\n");
	f.write("#include \"font.h\"\n")
	f.write("extern const struct font font_" + font_name + ";\n")
	f.write("#endif /*__" + font_name.upper() + "_H*/\n");
	f.close()
Beispiel #37
0
def generate(count):
    # ---------------get three colors-------------------------------
    colorString = random.choice(result)
    color = []
    for i in colorString:
        color += [int(i)]
    # for i in range(len(color)):
    #     color[i] = math.floor(color[i]/255*65535)
    color1 = color[0:3]
    color2 = color[3:6]
    color3 = color[6:9]
    # --------------------------------------------------------------

    # ----------get the base layer texture--------------------------
    Scenes = pathwalk('./SceneData')

    randomScene = random.choice(Scenes)
    randomScene = randomScene[0] + '/' + randomScene[1]
    print(randomScene)
    randomSceneImage = Image(filename=randomScene)

    widthRange = randomSceneImage.size[0] - 100
    heightRange = randomSceneImage.size[1] - 32

    randomSceneImage.crop(left=random.randint(0, widthRange),
                          top=random.randint(0, heightRange),
                          width=100,
                          height=32)
    # randomSceneImage.save(filename='.\\photoWand\\'+str(j+1) + '_texture.jpg')
    # --------------------------------------------------------------

    # ----------create the base layer, base texture +base color-----

    baseImage = Image(
        width=100,
        height=32,
        background=Color('rgb(' + str(color1[0]) + ',' + str(color1[1]) + ',' +
                         str(color1[2]) + ')'))

    # print('base_color = ' + 'rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')')
    baseImage.composite_channel(channel='undefined',
                                image=randomSceneImage,
                                operator='blend',
                                left=0,
                                top=0)
    baseImage.gaussian_blur(4, 10)
    baseImage.resolution = (96, 96)
    # --------------------------------------------------------------

    # -----generate font--------------------------------------------
    word = randomWords()
    fonts = pathwalk('./fonts/font_en/')
    randomFont = random.choice(fonts)
    randomFont = randomFont[0] + randomFont[1]

    initialPointsize = 45

    draw = Drawing()
    draw.font = randomFont

    tmp = int(math.floor(abs(random.gauss(0, 1)) * 6))
    if random.randint(1, 2) == 1:
        rotateX = random.randint(0, tmp)
    else:
        rotateX = random.randint(360 - tmp, 360)

    draw.rotate(rotateX)
    # --------------------------------------------------------------
    # --------get suitable FontPointSize----------------------------
    draw.font_size = initialPointsize
    metric = draw.get_font_metrics(image=baseImage, text=word)

    while metric.text_width > 100 or metric.text_height > 36:
        initialPointsize -= 5
        draw.font_size = initialPointsize
        metric = draw.get_font_metrics(image=baseImage, text=word)
    # --------------------------------------------------------------

    # ----------italic----------------------------------------------
    if random.random() > 0.5:
        draw.font_style = 'italic'
    # --------------------------------------------------------------

    # ----------underline-------------------------------------------
    if random.random() > 0.5:
        draw.text_decoration = 'underline'
    # --------------------------------------------------------------

    # ----------gravity---------------------------------------------
    draw.gravity = 'center'
    # --------------------------------------------------------------

    # --------------shadow/border-----------------------------------
    if random.random() < 0.5:
        # shadow
        addx = math.ceil(random.gauss(0, 2))
        addy = math.ceil(random.gauss(0, 2))
        draw.fill_color = Color('black')
        draw.text(x=abs(int(addx)), y=abs(int(addy)), body=word)

    else:
        # border
        draw.stroke_color = Color('rgb(' + str(color3[0]) + ',' +
                                  str(color3[1]) + ',' + str(color3[2]) + ')')
        draw.stroke_width = math.ceil(initialPointsize / 10) - 1
    # --------------------------------------------------------------

    # ----------print word------------------------------------------
    draw.fill_color = Color('rgb(' + str(color2[0]) + ',' + str(color2[1]) +
                            ',' + str(color2[2]) + ')')
    draw.text(x=0, y=0, body=word)
    draw.draw(baseImage)
    # --------------------------------------------------------------

    # ------------gray----------------------------------------------
    baseImage.colorspace = 'gray'
    # --------------------------------------------------------------

    print(word)
    baseImage.save(filename='./photo_en/' + str(count + 1) + '_' + word +
                   '.jpg')
Beispiel #38
0
def make_namecard(user_info_list, team_info, template):
    # check team directory exist(if not, make)
    path_team_dir = '/'.join((os.path.abspath(os.path.dirname(__file__)),
                              'deliverable', team_info['name']))
    if not os.path.isdir(path_team_dir):
        os.makedirs(path_team_dir)

    # check icon directory exist(if not, make)
    path_icon_dir = '/'.join((path_team_dir, 'icon'))
    if not os.path.isdir(path_icon_dir):
        os.makedirs(path_icon_dir)

    # check namecard directory exist(if not, make)
    path_card_dir = '/'.join((path_team_dir, 'namecard'))
    if not os.path.isdir(path_card_dir):
        os.makedirs(path_card_dir)

    # make plain card
    base = Image(width=template['width'],
                 height=template['height'],
                 background=Color('rgb(255, 255, 255)'))
    frame = Drawing()
    frame.stroke_color = Color('rgb(0, 0, 0)')
    frame.fill_color = Color('rgba(255, 255, 255, 0)')
    frame.rectangle(left=0,
                    top=0,
                    width=base.width - 1,
                    height=base.height - 1)
    frame(base)

    # draw team logo
    if template['logo']['display']:
        # check team icon exist offline(if not, get)
        path_logo_file = ''.join((path_team_dir, '/logo.jpg'))
        if not os.path.isfile(path_logo_file):
            icon_collector.get_image(team_info['icon'], path_logo_file)
        logo_draw = Image(filename=path_logo_file)
        logo_draw.resize(template['logo']['size'], template['logo']['size'])
        if template['logo']['align'] == 'left':
            logo_left = template['logo']['x']
        elif template['logo']['align'] == 'center':
            logo_left = template['logo']['x'] - int(
                template['logo']['size'] / 2)
        elif template['logo']['align'] == 'right':
            logo_left = template['logo']['x'] - template['logo']['size']
        logo_top = template['logo']['y']
        base.composite(logo_draw, logo_left, logo_top)

    # draw team name
    if template['team']['display']:
        team_draw = Drawing()
        if template['team']['font'] != 'default':
            team_draw.font = template['team']['font']
        if template['team']['size'] != 'default':
            team_draw.font_size = template['team']['size']
        mtr_team = team_draw.get_font_metrics(base, team_info['name'])
        if template['team']['align'] == 'left':
            team_left = template['team']['x']
        elif template['team']['align'] == 'center':
            team_left = template['team']['x'] - int(mtr_team.text_width / 2)
        elif template['team']['align'] == 'center':
            team_left = template['team']['x'] - int(mtr_team.text_width)
        team_top = template['team']['y'] + int(mtr_team.ascender)
        team_draw.text(team_left, team_top, team_info['name'])
        team_draw(base)

    #save dummy card
    base.save(filename=''.join((path_team_dir, '/dummy.png')))

    for user in user_info_list.values():
        base_clone = base.clone()
        # draw user icon
        if template['icon']['display']:
            # check user icon exist offline(if not, get)
            path_icon_file = ''.join(
                (path_icon_dir, '/', user['name'], '.png'))
            if not os.path.isfile(path_icon_file):
                icon_collector.get_image(user['icon'], path_icon_file)
            icon_draw = Image(filename=path_icon_file)
            icon_draw.resize(template['icon']['size'],
                             template['icon']['size'])
            if template['icon']['align'] == 'left':
                icon_left = template['icon']['x']
            elif template['icon']['align'] == 'center':
                icon_left = template['icon']['x'] - int(
                    template['icon']['size'] / 2)
            elif template['icon']['align'] == 'right':
                icon_left = template['icon']['x'] - template['icon']['size']
            icon_top = template['icon']['y']
            base_clone.composite(icon_draw, icon_left, icon_top)

        # draw real name
        if template['real']['display']:
            real_draw = Drawing()
            if template['real']['font'] != 'default':
                real_draw.font = template['real']['font']
            if template['real']['size'] != 'default':
                real_draw.font_size = template['real']['size']
            user_real_name = user['real_name']
            mtr_real = real_draw.get_font_metrics(base_clone, user_real_name)
            if mtr_real.text_width > template['real']['width']:
                user_real_name = user['real_name'].replace(' ', '\n')
                mtr_real = real_draw.get_font_metrics(base_clone,
                                                      user_real_name)
            if template['real']['align'] == 'left':
                real_left = template['real']['x']
            elif template['real']['align'] == 'center':
                real_left = template['real']['x'] - int(
                    mtr_real.text_width / 2)
            elif template['real']['align'] == 'center':
                real_left = template['real']['x'] - int(mtr_real.text_width)
            real_top = template['real']['y'] + int(mtr_real.ascender)
            real_draw.text(real_left, real_top, user_real_name)
            real_draw(base_clone)

        # draw name
        if template['name']['display']:
            name_draw = Drawing()
            if template['name']['font'] != 'default':
                name_draw.font = template['name']['font']
            if template['name']['size'] != 'default':
                name_draw.font_size = template['name']['size']
            user_name = ''.join(('@', user['name']))
            mtr_name = name_draw.get_font_metrics(base_clone, user_name)
            if template['name']['align'] == 'left':
                name_left = template['name']['x']
            elif template['name']['align'] == 'center':
                name_left = template['name']['x'] - int(
                    mtr_name.text_width / 2)
            elif template['name']['align'] == 'center':
                name_left = template['name']['x'] - int(mtr_name.text_width)
            name_top = template['name']['y'] + int(mtr_name.ascender)
            name_draw.text(name_left, name_top, user_name)
            name_draw(base_clone)

        # draw title
        if template['title']['display']:
            title_draw = Drawing()
            if template['title']['font'] != 'default':
                title_draw.font = template['title']['font']
            if template['title']['size'] != 'default':
                title_draw.font_size = template['title']['size']
            user_title = user['title']
            mtr_title = title_draw.get_font_metrics(base_clone, user_title)
            if template['title']['align'] == 'left':
                title_left = template['title']['x']
            elif template['title']['align'] == 'center':
                title_left = template['title']['x'] - int(
                    mtr_title.text_width / 2)
            elif template['title']['align'] == 'center':
                title_left = template['title']['x'] - int(mtr_title.text_width)
            title_top = template['title']['y'] + int(mtr_title.ascender)
            title_draw.text(title_left, title_top, user_title)
            title_draw(base_clone)

        base_clone.save(filename=''.join((path_card_dir, '/', user['name'],
                                          '.png')))