python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制
python PIL圖像處理模塊中的ImageDraw類支持各種幾何圖形的繪制和文本的繪制,如直線、橢圓、弧、弦、多邊形以及文字等。
下面直接通過示例來進(jìn)行說明:
#-*- coding: UTF-8 -*-
import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
def draw_test():
#生成深藍(lán)色繪圖畫布
array = np.ndarray((480, 640, 3), np.uint8)
array[:, :, 0] = 0
array[:, :, 1] = 0
array[:, :, 2] = 100
image = Image.fromarray(array)
#創(chuàng)建繪制對象
draw = ImageDraw.Draw(image)
#繪制直線
draw.line((20, 20, 150, 150), 'cyan')
#繪制矩形
draw.rectangle((100, 200, 300, 400), 'black', 'red')
#繪制弧
draw.arc((100, 200, 300, 400), 0, 180, 'yellow')
draw.arc((100, 200, 300, 400), -90, 0, 'green')
#繪制弦
draw.chord((350, 50, 500, 200), 0, 120, 'khaki', 'orange')
#繪制圓餅圖
draw.pieslice((350, 50, 500, 200), -150, -30, 'pink', 'crimson')
#繪制橢圓
draw.ellipse((350, 300, 500, 400), 'yellowgreen', 'wheat')
#外切矩形為正方形時橢圓即為圓
draw.ellipse((550, 50, 600, 100), 'seagreen', 'skyblue')
#繪制多邊形
draw.polygon((150, 180, 200, 180, 250, 120, 230, 90, 130, 100), 'olive', 'hotpink')
#繪制文本
font = ImageFont.truetype("consola.ttf", 40, encoding="unic")#設(shè)置字體
draw.text((100, 50), u'Hello World', 'fuchsia', font)
image.show()
return
首先,通過ImageDraw類創(chuàng)建一個繪制對象draw;
draw.line():直線的繪制,第一個參數(shù)指定的是直線的端點(diǎn)坐標(biāo),形式為(x0, y0, x1, y1),第二個參數(shù)指定直線的顏色;
draw.rectangle():矩形繪制,第一個參數(shù)指定矩形的對角線頂點(diǎn)(左上和右下),形式為(x0, y0, x1, y1),第二個指定填充顏色,第三個參數(shù)指定邊界顏色;
draw.arc():(橢)圓弧的繪制,第一個參數(shù)指定弧所在橢圓的外切矩形,第二、三兩個參數(shù)分別是弧的起始和終止角度, 第四個參數(shù)是填充顏色,第五個參數(shù)是線條顏色;
draw.chord():弦的繪制,和弧類似,只是將弧的起始和終止點(diǎn)通過直線連接起來;
draw.pieslice():圓餅圖的繪制,和弧與弦類似,只是分別將起始和終止點(diǎn)與所在(橢)圓中心相連;
draw.ellipse():橢圓的繪制,第一個參數(shù)指定橢圓的外切矩形, 第二、三兩個參數(shù)分別指定填充顏色和線條顏色,當(dāng)外切矩形是正方形時,橢圓即為圓;
draw.polygon():繪制多邊形,第一個參數(shù)為多邊形的端點(diǎn),形式為(x0, y0, x1, y1, x2, y2,……),第二、三兩個參數(shù)分別指定填充顏色和線條顏色;
draw.text():文字的繪制,第一個參數(shù)指定繪制的起始點(diǎn)(文本的左上角所在位置),第二個參數(shù)指定文本內(nèi)容,第三個參數(shù)指定文本的顏色,第四個參數(shù)指定字體(通過ImageFont類來定義)。
繪制結(jié)果如下:

最后,補(bǔ)充一下python中所支持的顏色,如下圖所示:

另外,顏色也可以使用"#"加上6位16進(jìn)制字符串表示如“#ff0000”,則和“red”等價,前兩位表示R通道的值,中間兩位表示G通道的值,最后兩位表示B通道的值。
PS:opencv+python 實(shí)現(xiàn)基本圖形的繪制及文本的添加
import cv2
import numpy as np
import os
class Drawing(object):
"""
使用opencv繪制圖形,支持直線,矩形,圓形,橢圓,多邊形以及被標(biāo)注文字添加
"""
chart_list = ['line', 'rectangle', 'circle', 'ellipse', 'polylines', 'puttext']
def __init__(self, src_img, dst_img, chart, dict_args):
self.src_img = os.path.normpath(src_img)
self.dst_img = os.path.normpath(dst_img)
self.chart = chart
self.dict_args = dict_args
# 顏色不傳默認(rèn)為紅色
self.color = dict_args['color'] if dict_args.has_key('color') else (0,0,255)
# 線條粗細(xì)不傳默認(rèn)為 2
self.thickness = dict_args['thickness'] if dict_args.has_key('thickness') else 2
def handle(self):
# 導(dǎo)入圖片
self.src_img = cv2.imread(self.src_img)
if self.chart not in self.chart_list:
print 'must input your right parameter'
return
if self.chart == 'line':
# 畫直線
self.start = self.dict_args['start']
self.end = self.dict_args['end']
self.draw_line()
elif self.chart == 'rectangle':
# 畫矩形
self.top_left = self.dict_args['top_left']
self.bottom_right = self.dict_args['bottom_right']
self.draw_rectangle()
elif self.chart == 'circle':
# 畫圓形
self.center = self.dict_args['center']
self.radius = self.dict_args['radius']
self.draw_circle()
elif self.chart == 'ellipse':
# 畫橢圓
self.center = self.dict_args['center']
self.axes = self.dict_args['axes']
# 旋轉(zhuǎn)角度,起始角度,終止角度 可不傳參,使用默認(rèn)值
self.angle = self.dict_args['angle'] if self.dict_args.has_key('angle') else 0
self.startangle = self.dict_args['startangle'] if self.dict_args.has_key('startangle') else 0
self.endangle = self.dict_args['endangle'] if self.dict_args.has_key('endangle') else 360
self.draw_ellipse()
elif self.chart == 'polylines':
# 畫多邊形
if not isinstance(self.dict_args['points'], list):
self.pts = list(self.dict_args['points'])
self.pts = np.array(self.dict_args['points'], np.int32)
self.close = self.dict_args['close'] if self.dict_args.has_key('close') else True
self.draw_polylines()
else:
# 標(biāo)注文本
self.text = self.dict_args['text']
self.position = self.dict_args['position']
# 字體,文字大小 可不傳參,使用默認(rèn)值
self.font = self.dict_args['font'] if self.dict_args.has_key('font') else cv2.FONT_HERSHEY_SIMPLEX
self.size = self.dict_args['size'] if self.dict_args.has_key('size') else 1
self.add_text()
cv2.imwrite(self.dst_img, self.src_img)
def draw_line(self):
# 劃線
# 輸入?yún)?shù)分別為圖像,開始坐標(biāo),結(jié)束坐標(biāo),顏色數(shù)組,粗細(xì)
cv2.line(self.src_img, self.start, self.end, self.color, self.thickness)
def draw_rectangle(self):
# 畫矩形
# 輸入?yún)?shù)分別為圖像、左上角坐標(biāo)、右下角坐標(biāo)、顏色數(shù)組、粗細(xì)
cv2.rectangle(self.src_img, self.top_left, self.bottom_right, self.color, self.thickness)
def draw_circle(self):
# 畫圓形
# 輸入?yún)?shù)為圖像,圓心,半徑,線條顏色,粗細(xì)
cv2.circle(self.src_img, self.center, self.radius, self.color, self.thickness)
def draw_ellipse(self):
# 畫橢圓
# 輸入?yún)?shù)為圖像,中心,(長軸,短軸),旋轉(zhuǎn)角度,起始角度,終止角度,線條顏色,粗細(xì)
cv2.ellipse(self.src_img, self.center, self.axes, self.angle, self.startangle,self.endangle, self.color, self.thickness)
def draw_polylines(self):
# 畫多邊形
# 輸入?yún)?shù)為圖像,多邊形各個頂點(diǎn)坐標(biāo),是否連成封閉圖形,線的顏色,粗細(xì)
cv2.polylines(self.src_img, [self.pts], self.close, self.color, self.thickness)
def add_text(self):
# 標(biāo)注文本
# 輸入?yún)?shù)為圖像、文本、位置、字體、大小、顏色數(shù)組、粗細(xì)
cv2.putText(self.src_img, self.text, self.position, self.font, self.size, self.color, self.thickness)
以上就是python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制的詳細(xì)內(nèi)容,更多關(guān)于python 幾何圖形的繪制的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
通過Python實(shí)現(xiàn)一個A/B測試詳解
A/B測試,通過分析兩種不同的營銷策略,以此來選擇最佳的營銷策略,可以高效地將流量轉(zhuǎn)化為銷售額。本文主要介紹了如何通過Python實(shí)現(xiàn)一個A/B測試,感興趣的可以了解一下2023-01-01
PyCharm搭建Spark開發(fā)環(huán)境實(shí)現(xiàn)第一個pyspark程序
這篇文章主要介紹了PyCharm搭建Spark開發(fā)環(huán)境實(shí)現(xiàn)第一個pyspark程序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python實(shí)現(xiàn)自動化域名批量解析分享
這篇文章主要介紹了Python實(shí)現(xiàn)自動化域名批量解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08

