Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report