I have an image I would like to add text to but I want to use both a different style font and a something bigger than the smallest thing known to mankind. Heck you can hardly read the text it's so darn small. I can print the text to the screen but I can't change the font style or the size of it as I don't know where the fonts are stored. I would think there would be more than one font/font size standardly available for use with Python/PIL. Where are they stored at? I am on a Linux.
Adapted from this answer:
image = Image.new("RGBA", (600,150), (255,255,255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", fontsize)
draw.text((10, 0), txt, (0,0,0), font=font)
Looks like you can specify any .ttf file and any fontsize you want using the above modules and functions.
Related
I'm drawing text into an image using a specific font, with Pillow for Python.
If the font I am using doesn't contain a glyph for a specific character, Pillow draws a ?. I'd like to get notified if the font is missing that specific item. Then I can either skip it or use a different font.
from PIL import Image, ImageDraw, ImageFont
image = Image.new("RGB", (100, 100), "white")
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("unifont_upper-14.0.04.ttf", size=16)
draw.text((1, 1), "ð’€€", font=font, fill="black")
image.save("cuneiform.png")
The specific font I'm using, doesn't contain "Cuneiform Sign A" so Pillow writes a ? - how can I get Pillow (or something else) to alert me that the character won't be drawn properly?
I am using pygame to make a game, and I want to display some text, but the font doesn't change to any fonts that I've downloaded. I am using a Mac and the font that I downloaded appears in Font Book. I've used pygame.font.init, define the font as font = pygame.font.SysFont('font name', size), and use font.render(message, False, colour).
When defining the font, I tried using both the absolute and relative paths. When I run the program, it displays the text in the default font instead of the font I have downloaded. Why does this happen?
Use pygame.font.Font() instead of SysFont to use downloaded fonts
Using this syntax:
font = pygame.font.Font("font name/path", size)
use the font.render() function to create a font surface you can display:
font.render(yourMessage, True/False, colour)
Is there a way to render a tick mark using pygame? I tried using the unicode directly like this:
def write(screen, text, color, position, size):
font = pygame.font.Font(pygame.font.get_default_font(), size)# Defining a font with font and size
text_surface = font.render(text, True, color)# Defining the text color which will be rendered
screen.blit(text_surface, (position[0], position[1])) # Rendering the font
write(window, u'\u2713', self.color, position, size)
But this just draws a rectangle.
This 'rectangle' that is being written is in fact the representation of the ✓ sign using a pygame.font.get_default_font(). In order to display a ✓ sign you need to be sure, that this sign is included in a font that you are using. I propose the following solution:
Download the font that includes this symbol (e.g. Segoe UI Symbols from here)
Unzip the font package into the data folder (or main applicaation folder)
Change the font that you are using to the downloaded one
font = pygame.font.Font("seguisym.ttf", size)
Use a write function either using a write(window, u'\u2713', self.color, position, size) method or directly using write(screen, "✓", self.color, position, size)
This question is inline with the answer in my previous question in Stackoverflow.
I am creating a program that converts a text to image. I want to render it in using the font OCR A. But since OCR A font, has no corresponding font file for italics, I have to do the slanting of the upright font manually.
Upright Font
Slanted Font
Below is my initial code :
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import numpy as np
#Returns the text size in terms of width and height.
def getSize(txt, font):
testImg = Image.new('RGB', (1, 1))
testDraw = ImageDraw.Draw(testImg)
return testDraw.textsize(txt, font)
text = 'CNN Font Recognition Model'
font_size = 12
font = ImageFont.truetype('ocra.ttf' , font_size)
#ocra.ttf is a font file for OCR A Regular font (I have to render it slanted)
width, height = getSize(text, font)
#Creates an image with white background of constant size.
img = Image.new('RGB',(width, height), 'white')
d = ImageDraw.Draw(img)
d.text((0,0), text, fill='black', font=font)
img.save('slanted_ocr_a.png')
How do I manually slant the upright OCR A font? Thanks.
You can download a sample ocra.ttf file here.
This answer to a similar question is probably the easiest way, although it is not exactly what you want to do. Basically you render the upright font into an image then slant (shear) the entire image using PIL's Image.transform method.
To accomplish exactly what you are asking (slanting the font before imaging it) is possible, but would require a lot of work overriding PIL's truetype scheme to have the underlying font engine (FreeType) perform the slant transformation at the font level. PIL's truetype interface simply doesn't have a way to express a transform, so you'd have to override/patch it to pass that all the way down to FreeType where the font is set up.
Another option might be to skip PIL's truetype scheme and use a Python font-rendering library that allows you to set the font transform directly, then scrape the pixel data from that into your PIL Image. One such library is freetype-py.
How to draw bold/italic text with PIL? ImageFont.truetype(file, size) has an option to specify font size only.
Use the bold/italic version of the font
There is no bold features as parameter till now but easily you can solve by add stroke to the text with same color of text. it will make sense as bold font next code elaborate how to use stroke
draw.text((x, y), text, fill=color, font=font, stroke_width=2,
stroke_fill="black")
A rather hacky solution to make a font bold if (for whatever reason) you don't have a separate bold version of the font is to print the same text several times with a slight offset.
andaleMono = ImageFont.truetype(ANDALE_MONO_PATH,16)
text = "hello world"
mainOffset = (50,50)
xoff, yoff = mainOffset
draw.text(mainOffset,text,font=andaleMono,fill='black')
draw.text((xoff+1,yoff+1),text,font=andaleMono,fill='black')
draw.text((xoff-1,yoff-1),text,font=andaleMono,fill='black')
Many fonts use different TTF files for their bold/italic versions, so I'd imagine if you just specify that file it would work.
Well, this is my first comment. Here we go.
I'll try to clarify the procedure. At first What I did was use the "name" of the font like this
font = ImageFont.truetype("C:\Windows\Fonts\\Arial Negrita.ttf",25)
but only got some errors like this:
Traceback (most recent call last):
File "C:/Users/555STi/PycharmProjects/PIL/img.py", line 8, in <module>
font = ImageFont.truetype("C:\Windows\Fonts\Arial negrita.ttf",25)
File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 262, in truetype
return FreeTypeFont(font, size, index, encoding)
File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 142, in __init__
self.font = core.getfont(font, size, index, encoding)
IOError: cannot open resource
Then I remembered that sometimes fonts has other "names" or "filenames", so, what I did was going to fonts folder, then opened the Arial Font wich displayed all the styles like negrita (bold], cursiva(italic), etc.
Did a right click on the "negrita" style, selected "properties" and then there was the "real name" of the font.
In my case, the name was "ariblk"
Then, finally, just used the name like this.
font = ImageFont.truetype("C:\Windows\Fonts\\ariblk.ttf",25)
I know this post is old, but today helped me to get to the solution. So I hope to help anybody.
=)
With reference to the other answers here, my search for the name for the bold variant of Arial produced the following (arialbd.ttf):
def FindFontsVariantsWithBase(fontBase="arial"):
import matplotlib
system_fonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
# for font in np.sort(system_fonts):
# print(font)
fonts = np.sort(system_fonts).tolist()
res = [i for i in fonts if fontBase in i]
print(res)
FindFontsVariantsWithBase("arial")
['C:\WINDOWS\Fonts\arial.ttf', 'C:\WINDOWS\Fonts\arialbd.ttf', 'C:\WINDOWS\Fonts\arialbi.ttf', 'C:\WINDOWS\Fonts\ariali.ttf', 'C:\Windows\Fonts\arial.ttf', 'C:\Windows\Fonts\arialbd.ttf', 'C:\Windows\Fonts\arialbi.ttf', 'C:\Windows\Fonts\ariali.ttf'] (