Python module series - Pilot Library - image processing library - get font size enclosing rectangle

Plug in development 2022-05-14 13:58:39 阅读数:429


1. summary

PIL, Full name Python Imaging Library, It's already Python In fact, the image processing standard library of the platform .PIL Very powerful , but API It's very easy to use .Pillow yes PIL A derivative of , But now it has become more than PIL A more dynamic image processing library of its own .pillow It can be said that it has replaced PIL, Encapsulate it as python The library of (pip You can install ), And support python2 and python3.

2. Common objects

Image color :Pillow One of the colors used in 3 Tuples of elements represent , Such as (R,G,B). It can also be represented by a string with a specific meaning , Like black :‘black’.
Image: It is Pillow The core class in the library , Represents an image . It has some common methods , Such as reading images , Save image , Image parameters, etc , As shown below :

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
# Reading images from a file 
img ='E:/ Photo album / nikon / Flower .jpg');
print((img.format, img.mode, img.size)) # Output picture basic information 
data = list(img.getdata()) # Return the image sequence data and convert it to rgb list;
img_resize = img.resize((256, 256)) # Adjust the size" Flower 256.jpg")
img_rotate = img.rotate(45) # The center rotates counterclockwise" Flower 45.jpg")
# Custom new image 
img2 ="RGB", (256, 256), "black") # Black background 
print((img2.format, img2.mode, img2.size)) # Output picture basic information 

There are comments on the content in the above code , There are several common function descriptions .Pillow The image processing function of the library is very powerful , The above is just a simple example , The functions involved in character recognition will be updated step by step later .

ImageFont: It represents a font object . Used to draw text on an image . A simple example of creating a font , As shown below :

from PIL import ImageFont, ImageDraw, Image
image ="RGB", (128, 128), "black") # Black background 
draw = ImageDraw.Draw(image) # Draw objects 
# use a truetype font
font = ImageFont.truetype('.\chinese_fonts\DroidSansFallbackFull.ttf', 15)
draw.text((10, 25), "world", font=font);

ImageDraw: It is based on Image 2D drawing class . It can be used to generate text containing , Graphics and other required custom images . As shown above , It draws a text in image On .text Method , The prototype is as follows :
PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None)

Draws the string at the given position.– Draw text at the specified position .
Parameters :
xy – Top left corner of the text.– The upper left corner of the text
text – Text to be drawn.– Text to be drawn
font – An ImageFont instance.– Font object
fill – Color to use for the text.– Fill color

3. Gets the font size enclosing rectangle

The source code is as follows :

def Pt2MM(val):
ret = val * 25.4 / 72;
return ret;
def GetFontRenderSize(text, fontsize):
canvas ='RGB', (512, 512));
draw = ImageDraw.Draw(canvas);
monospace = ImageFont.truetype(r"msyh.ttc", fontsize);
draw.text((0, 0), text, font=monospace, fill="white");
bbox = canvas.getbbox();
# Wide and high 
sizept = (bbox[2] - bbox[0], bbox[3] - bbox[1]);
fontsizewmm = Pt2MM(sizept[0]);
fontsizehmm = Pt2MM(sizept[1]);
return (fontsizewmm, fontsizehmm);

4. Author Q & A

If you have any questions , Please leave a message .

版权声明:本文为[Plug in development]所创,转载请带上原文链接,感谢。