python画旋转圆_在Python中以一定角度绘制文本(旋转)

1586010002-jmsa.png

I am drawing text onto a numpy array image in Python (using a custom font). Currently I am converting the image to PIL, drawing the text and then converting back to a numpy array.

import numpy as np

import cv2

from PIL import Image

from PIL import ImageDraw

from PIL import ImageFont

char_image = np.zeros((200, 300, 3), np.uint8)

# convert to pillow image

pillowImage = Image.fromarray(char_image)

draw = ImageDraw.Draw(pillowImage)

# add chars to image

font = ImageFont.truetype("arial.ttf", 32)

draw.text((50, 50), 'ABC', (255, 255, 255), font=font)

# convert back to numpy array

char_image = np.array(pillowImage, np.uint8)

# show image on screen

cv2.imshow('myImage', char_image)

cv2.waitKey(0)

Is there anyway to draw the text on a given angle, ie. 33 degrees?

Rotating the image once the text has been drawn is not an option

解决方案

Using matplotlib, first visualize array and draw on it, get the raw data from the figure back.

Pro: both tools are quite high level and let you deal with many details of the process. ax.annotate() offers flexibility for where and how to draw and set font properties, and plt.matshow() offers flexibility that lets you deal with aspects of array visualization.

import matplotlib.pyplot as plt

import scipy as sp

# make Data array to draw in

M = sp.zeros((500,500))

dpi = 300.0

# create a frameless mpl figure

fig, axes = plt.subplots(figsize=(M.shape[0]/dpi,M.shape[1]/dpi),dpi=dpi)

axes.axis('off')

fig.subplots_adjust(bottom=0,top=1.0,left=0,right=1)

axes.matshow(M,cmap='gray')

# set custom font

import matplotlib.font_manager as fm

ttf_fname = '/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf'

prop = fm.FontProperties(fname=ttf_fname)

# annotate something

axes.annotate('ABC',xy=(250,250),rotation=45,fontproperties=prop,color='white')

# get fig image data and read it back to numpy array

fig.canvas.draw()

w,h = fig.canvas.get_width_height()

Imvals = sp.fromstring(fig.canvas.tostring_rgb(),dtype='uint8')

ImArray = Imvals.reshape((w,h,3))

Wqje2.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值