Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use ImageDraw.textsize () and ImageDraw.text ()

Shulou Source: shulou.com Published: 2022-06-01 07:54:57 09月25日 Update

This article introduces the relevant knowledge of "how to use ImageDraw.textsize () and ImageDraw.text ()". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Instructions for the ImageDraw.textsize () function:

Returns the image size required to display a given string with the specified font object.

Instructions for the ImageDraw.text () function:

Use the specified font object to display text in the image, you can specify where the text is displayed and the color displayed.

Examples of code experiments:

From PIL import Image,ImageFont, ImageDrawimport numpy as npimport colorsysimage = Image.open ("bryant.jpg") class_names =\ ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train" "tvmonitor"] hsv_tuples = [(x / len (class_names), 1.1.) for x in range (len (class_names))] # get different chromaticity of hsv format colors = list (map (lambda x: colorsys.hsv_to_rgb (* x), hsv_tuples)) # get different colors of rgb format colors = list (lambda x: (int (x [0] * 255), int (x [1] * 255)) Int (x [2] * 1000), colors) # adjust the chromaticity of the corresponding borders of different categories through the hsv format font= ImageFont.truetype (font='simhei.ttf',size=np.floor (3e-2 * 1000 + 0.5). Astype ('int32')) # to obtain the font label =' {} {: .2f} '.format (' Lin Ma Zu Quan', 0.982) draw = ImageDraw.Draw (image) label_size = draw.textsize (label) Font) print ('label_size:',label_size) # label_size: (255,30) label = label.encode (' utf-8') draw.rectangle ([200,50,200 + label_size [0], 50 + label_size [1]], outline= colors [class _ names.index ('motorbike')], width=1,fill= colors [class _ names.index (' diningtable')] # for filling) draw.text ([200,50], label.decode (), fill= (255,0,0) Font=font) image.show ()

The output result of the console shows:

Copyright (C) Windows PowerShell Microsoft Corporation. All rights reserved. It took 1006 milliseconds to try a new cross-platform PowerShell https://aka.ms/pscore6 to load personal and system profiles. (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > &'D:\ Anaconda3\ envs\ pytorch_1.7.1_cu102\ python.exe''c:\ Users\ chenxuqi\ .vscode\ extensions\ ms-python.python-2020.12.424452561\ pythonFiles\ lib\ python\ debugpy\ launcher' '54048' -' c:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq\ test15.py'label_size: 30) (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > conda activate pytorch_1.7.1_cu102 (pytorch_1.7.1_cu102) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq >

Screenshot of the running result:

That's all for "how to use ImageDraw.textsize () and ImageDraw.text ()". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Different font format usage description content function image object text more knowledge result chromaticity color output practical learned next code location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Apple Xiaomi OPPO Reno