Pillow - detect if font contains glyph for a character - python

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?

Related

How could I add text to an image using PIL?

I've seen many questions and answers here, but they're not for Python. I'm trying to make a meme generator, and I have a folder full of emme templates. I want to make the user get a meme template, view it then add text. Here is my code so far:
from PIL import Image
im = Image.open("Two-Buttons.jpg")
im.show()
Yeah, I know it's not much but I need help with getting user input on a certain part of the text.
First, you would need to import the necessary objects to draw on images from the PIL library:
from PIL import Image, ImageDraw, ImageFont
Then, you instantiate the ImageDraw class:
draw = ImageDraw.Draw(im)
Then you can simply do the following to draw on the original image im:
text = 'Insert meme here'
font_type = ImageFont.truetype("arial.ttf", 18) # 18 -> font size
draw.text((100, 100), text, (255, 255, 0), font=font_type) # "draw.text(LOCATION, TEXT, COLOR, FONT_TYPE)"
im.show()

How to display an utf-8 character in pygame?

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)

Python - Render a Slanted Font

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.

Python PIL using different font

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.

python: reportlab, how to remove black borders from image

I am trying to generate a pdf file, using python reportlab, but is seems image is displayed with strange black border in pdf.
Here is the code:
# Standalone script to generate pdf lessons
from reportlab.pdfgen import canvas
def hello(c):
c.drawImage("./media/files/1.png", 0, 600, 350, 350)
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
The image I am trying to add is here
Can somebody advice why the black line on the left have appeared, and how to fix it?
The problem isn't with a border, rather your chessboard has transparent pixels on the right and bottom sides, and reportlab isn't recognizing the alpha channel and is painting the transparent section as black:
Using mask='auto' tells drawImage to use the alpha channel in your PNG, so the background shows through:
c.drawImage("./media/files/1.png", 0, 600, 350, 350, mask='auto')

Categories

Resources