Oct2020
Oct2020

Reputation: 49

How to find the font size in Python from an image?

I am currently trying to find out how I can find out the font size from an image, using tesseract ocr or maybe something else within Python.

My current image here: Image 1. Within the image, at the top I know for certain it is a font 6 and the bottom is font 7.

I am starting out a side project of scanning the image and seeing if it has a minimum legal font requirement (which is a font 7).

How can I determine whether all text within the image is at font 7 and not below 7?

Here is what I'm thinking to do:

legal_font = 7

if legal_font > 6:
    print("Illegal")
else:
    print("Legal")

the number 6 is the one that will wary, due to loads of text around the image.

Upvotes: 4

Views: 5587

Answers (1)

Mohit Reddy
Mohit Reddy

Reputation: 141

This seems like a valid answer to your question:

from PIL import ImageFont, ImageDraw, Image

def find_font_size(text, font, image, target_width_ratio):
    tested_font_size = 100
    tested_font = ImageFont.truetype(font, tested_font_size)
    observed_width, observed_height = get_text_size(text, image, tested_font)
    estimated_font_size = tested_font_size / (observed_width / image.width) * target_width_ratio
    return round(estimated_font_size)

def get_text_size(text, image, font):
    im = Image.new('RGB', (image.width, image.height))
    return draw.textsize(text, font)

width_ratio = 0.5
font_family = "arial.ttf"
text = "Hello World"

image = Image.open('pp.png')
editable_image = ImageDraw.Draw(image)
font_size = find_font_size(text, font_family, image, width_ratio)
font = ImageFont.truetype(font_family, font_size)
print(f"Font size found = {font_size} - Target ratio = {width_ratio} - Measured ratio = {get_text_size(text, image, font)[0] / image.width}")

editable_image.text((10, 10), text, font=font)
image.save('output.png')

You do have to install the PIL package tho, comment here if you need help doing that.

You will also have to download this: https://github.com/JotJunior/PHP-Boleto-ZF2/blob/master/public/assets/fonts/arial.ttf?raw=true

Upvotes: 2

Related Questions