Python PIL.ImageFont() Examples

The following are 10 code examples of PIL.ImageFont(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PIL , or try the search function .
Example #1
Source File: Fun.py    From NotSoBot with MIT License 6 votes vote down vote up
def generate_ascii(self, image):
		font = PIL.ImageFont.truetype(self.files_path('FreeMonoBold.ttf'), 15, encoding="unic")
		image_width, image_height = image.size
		aalib_screen_width= int(image_width/24.9)*10
		aalib_screen_height= int(image_height/41.39)*10
		screen = aalib.AsciiScreen(width=aalib_screen_width, height=aalib_screen_height)
		im = image.convert("L").resize(screen.virtual_size)
		screen.put_image((0,0), im)
		y = 0
		how_many_rows = len(screen.render().splitlines()) 
		new_img_width, font_size = font.getsize(screen.render().splitlines()[0])
		img = PIL.Image.new("RGBA", (new_img_width, how_many_rows*15), (255,255,255))
		draw = PIL.ImageDraw.Draw(img)
		for lines in screen.render().splitlines():
			draw.text((0,y), lines, (0,0,0), font=font)
			y = y + 15
		imagefit = PIL.ImageOps.fit(img, (image_width, image_height), PIL.Image.ANTIALIAS)
		return imagefit 
Example #2
Source File: weather.py    From EInk-Calendar with MIT License 6 votes vote down vote up
def __init__(self, height: int, width: int, icon_font: ImageFont,
                 text_font: ImageFont, icon_lookup: WeatherIconLookup) -> None:
        super().__init__(height, width)

        self.weather_icon = TextWidget(height // 2, width, font=icon_font)
        self.weather_icon.row = 0
        self.weather_icon.col = 0
        self.weather_icon.text = icon_lookup.look_up_with_name('wi-na')
        self.add_child(self.weather_icon)

        self.high_temp_text = TextWidget(height // 4, width, font=text_font)
        self.high_temp_text.row = height // 2
        self.high_temp_text.col = 0
        self.high_temp_text.text = '--'
        self.add_child(self.high_temp_text)

        self.low_temp_text = TextWidget(height // 4, width, font=text_font)
        self.low_temp_text.row = height // 4 * 3
        self.low_temp_text.col = 0
        self.low_temp_text.text = '--'
        self.add_child(self.low_temp_text)

        self.icon_lookup = icon_lookup 
Example #3
Source File: event.py    From EInk-Calendar with MIT License 6 votes vote down vote up
def __init__(self, height: int, width: int, header_font: ImageFont,
                 event_font: ImageFont) -> None:
        super().__init__(height, width)
        header = TextWidget(height // 10, width, font=header_font)
        header.row = 0
        header.col = 0
        header.text = 'Events'
        header.foreground = self.background
        header.background = self.foreground
        header.is_draw_border(True)
        self.add_child(header)

        event_top = height // 5
        self.event_widgets = []
        while event_top < self.width:
            event = EventWidget(height // 10, width, event_font=event_font)
            event.row = event_top
            event.col = 0
            self.event_widgets.append(event)
            self.add_child(event)
            event_top += height // 10 
Example #4
Source File: __init__.py    From GeoVis with MIT License 5 votes vote down vote up
def __init__(self):
        global PIL
        import PIL, PIL.Image, PIL.ImageDraw, PIL.ImageTk, PIL.ImageFont
        self.upscaled = False
        self.sysfontfolders = dict([("windows","C:/Windows/Fonts/"),
                                    ("darwin", "/Library/Fonts/"),
                                    ("linux", "/usr/share/fonts/truetype/") ])
        self.fontfilenames = dict([("default", "TIMES.TTF"),
                                   ("times new roman","TIMES.TTF"),
                                   ("arial","ARIAL.TTF")]) 
Example #5
Source File: __init__.py    From GeoVis with MIT License 5 votes vote down vote up
def _BasicText(self, relx, rely, text, options):
        """
draws basic text, no effects
"""
        fontlocation = self.sysfontfolders[OSSYSTEM]+self.fontfilenames[options["textfont"]]
        font = PIL.ImageFont.truetype(fontlocation, size=options["textsize"])
        fontwidth, fontheight = self.drawer.textsize(text, font)
        textanchor = options.get("textanchor")
        if textanchor:
            textanchor = textanchor.lower()
            if textanchor == "center":
                x = int(MAPWIDTH*relx) - int(fontwidth/2.0)
                y = int(MAPHEIGHT*rely) - int(fontheight/2.0)
            else:
                x = int(MAPWIDTH*relx) - int(fontwidth/2.0)
                y = int(MAPHEIGHT*rely) - int(fontheight/2.0)
                if "n" in textanchor:
                    y = int(MAPHEIGHT*rely)
                elif "s" in textanchor:
                    y = int(MAPHEIGHT*rely) - int(fontheight)
                if "e" in textanchor:
                    x = int(MAPWIDTH*relx) - int(fontwidth)
                elif "w" in textanchor:
                    x = int(MAPWIDTH*relx)
        if options.get("textboxfillcolor") or options.get("textboxoutlinecolor"):
            relfontwidth, relfontheight = (fontwidth/float(MAPWIDTH), fontheight/float(MAPHEIGHT))
            relxmid,relymid = (x/float(MAPWIDTH)+relfontwidth/2.0,y/float(MAPHEIGHT)+relfontheight/2.0)
            relupperleft = (relxmid-relfontwidth*options["textboxfillsize"]/2.0, relymid-relfontheight*options["textboxfillsize"]/2.0)
            relbottomright = (relxmid+relfontwidth*options["textboxfillsize"]/2.0, relymid+relfontheight*options["textboxfillsize"]/2.0)
            options["fillcolor"] = options["textboxfillcolor"]
            options["outlinecolor"] = options["textboxoutlinecolor"]
            options["outlinewidth"] = options["textboxoutlinewidth"]
            self.RenderRectangle(relupperleft, relbottomright, options)
        self.drawer.text((x,y), text=text, font=font, fill=options["textcolor"]) 
Example #6
Source File: wrapper.py    From GadioVideo with MIT License 5 votes vote down vote up
def __init__(self, font:ImageFont):
        self.font = font
        self.tokens = list() 
Example #7
Source File: text.py    From EInk-Calendar with MIT License 5 votes vote down vote up
def __init__(self,
                 height: int,
                 width: int,
                 font: ImageFont = None) -> None:
        super().__init__(height, width)
        self._text = ''
        self._font = font
        self._vertical_align = Alignments.CENTER
        self._horizontal_align = Alignments.CENTER 
Example #8
Source File: event.py    From EInk-Calendar with MIT License 5 votes vote down vote up
def __init__(self, height: int, width: int, event_font: ImageFont) -> None:
        super().__init__(height, width)
        self.date = datetime.datetime.now()
        self.font = event_font
        self.event = ''
        self._show = False 
Example #9
Source File: weather.py    From EInk-Calendar with MIT License 4 votes vote down vote up
def __init__(self, height: int, width: int, icon_font: ImageFont,
                 small_icon_font: ImageFont, text_font: ImageFont,
                 icon_lookup: WeatherIconLookup) -> None:
        super().__init__(height, width)
        self.icon_lookup = icon_lookup

        self.weather_icon = TextWidget(height // 2, width // 2, font=icon_font)
        self.weather_icon.row = 0
        self.weather_icon.col = 0
        self.weather_icon.text = icon_lookup.look_up_with_name('wi-na')
        self.add_child(self.weather_icon)

        detail_width = width * 0.2
        self.temperature_icon = TextWidget(height // 4,
                                           width // 4,
                                           font=small_icon_font)
        self.temperature_icon.row = 0
        self.temperature_icon.col = width // 2
        self.temperature_icon.text = icon_lookup.look_up_with_name(
            'wi-thermometer')
        self.add_child(self.temperature_icon)

        self.temperature_text = TextWidget(height // 4,
                                           width // 4,
                                           font=text_font)
        self.temperature_text.row = 0
        self.temperature_text.col = width // 4 * 3
        self.temperature_text.text = '--'
        self.add_child(self.temperature_text)

        self.humidity_icon = TextWidget(height // 4,
                                        width // 4,
                                        font=small_icon_font)
        self.humidity_icon.row = height // 4
        self.humidity_icon.col = width // 2
        self.humidity_icon.text = icon_lookup.look_up_with_name('wi-humidity')
        self.add_child(self.humidity_icon)

        self.humidity_text = TextWidget(height // 4,
                                        width // 4,
                                        font=text_font)
        self.humidity_text.row = height // 4
        self.humidity_text.col = width // 4 * 3
        self.humidity_text.text = '--'
        self.add_child(self.humidity_text)

        self.forecasts: List[ForecastWidget] = []
        for i in range(4):
            forecast = ForecastWidget(height // 2, width // 4, small_icon_font,
                                      text_font, icon_lookup)
            forecast.row = height // 2
            forecast.col = i * width // 4
            self.add_child(forecast)
            self.forecasts.append(forecast) 
Example #10
Source File: create_dataset.py    From deep-fonts with Apache License 2.0 4 votes vote down vote up
def read_font(fn):
    font = PIL.ImageFont.truetype(fn, min(w0, h0))

    # We need to make sure we scale down the fonts but preserve the vertical alignment
    min_ly = float('inf')
    max_hy = float('-inf')
    max_width = 0
    imgs = []

    for char in chars:
        print '...', char
        # Draw character
        img = PIL.Image.new("L", (w0*5, h0*3), 255)
        draw = PIL.ImageDraw.Draw(img)
        draw.text((w0, h0), char, font=font)

        # Get bounding box
        diff = PIL.ImageChops.difference(img, blank)
        lx, ly, hx, hy = diff.getbbox()
        min_ly = min(min_ly, ly)
        max_hy = max(max_hy, hy)
        max_width = max(max_width, hx - lx)
        imgs.append((lx, hx, img))

    print 'crop dims:', max_hy - min_ly, max_width
    scale_factor = min(1.0 * h / (max_hy - min_ly), 1.0 * w / max_width)
    data = []

    for lx, hx, img in imgs:
        img = img.crop((lx, min_ly, hx, max_hy))

        # Resize to smaller
        new_width = (hx-lx) * scale_factor
        new_height = (max_hy - min_ly) * scale_factor
        img = img.resize((int(new_width), int(new_height)), PIL.Image.ANTIALIAS)

        # Expand to square
        img_sq = PIL.Image.new('L', (w, h), 255)
        offset_x = (w - new_width)/2
        offset_y = (h - new_height)/2
        print offset_x, offset_y
        img_sq.paste(img, (int(offset_x), int(offset_y)))

        # Convert to numpy array
        matrix = numpy.array(img_sq.getdata()).reshape((h, w))
        matrix = 255 - matrix
        data.append(matrix)

    return numpy.array(data)