在Python中以一定角度(旋转)绘制文本
我正在用Python(使用自定义字体)将文本绘制到数组图像上。目前我正在将图像转换为PIL,绘制文本,然后转换回numpy数组在Python中以一定角度(旋转)绘制文本,python,python-3.x,fonts,rotation,python-imaging-library,Python,Python 3.x,Fonts,Rotation,Python Imaging Library,我正在用Python(使用自定义字体)将文本绘制到数组图像上。目前我正在将图像转换为PIL,绘制文本,然后转换回numpy数组 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 pillow
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)
在给定的角度(即33度)上是否仍有绘制文本的方法
在绘制文本后旋转图像不是一个选项您可以使用PIL绘制旋转的文本。我建议将文本绘制到空白图像上,旋转该图像,然后将旋转后的图像粘贴到主图像中。比如: 代码: 它是如何工作的:
使用matplotlib,首先可视化阵列并在其上绘制,然后从图形中获取原始数据。 赞成:这两种工具都非常高级,可以让您处理流程的许多细节。提供了在何处以及如何绘制和设置字体属性的灵活性,并提供了允许您处理阵列可视化方面的灵活性
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))
您是否尝试过使用允许从某个角度绘制文本的库,例如pangocairo?不,是经验丰富的程序员,但python新手。花了太多的时间重新设计轮子,不想偏离错误的切线,所以我要寻找一些正统的代码来分析。我以前做过一些测试,发现在光栅化后旋转文本会导致它失去一些形状,因此它不那么干净。只有当我找不到一种方法来栅格化已经旋转的形状时,我才会使用这种方法。你应该检查这些结果。经过仔细的重采样,这里的光栅化效果非常好。可以,这绝对是一种比我目前使用的(cv2.warpAffine)更好的方法。这会创建一种时髦的背景色,我不确定在哪里可以设置字体。@Bug我以为你想在某些东西的上面绘制,所以我只取了一些随机数。现在它只是被零替换了…是的,很抱歉,这是对的,只是被抛出了一点。这是一个随机颜色的好技巧。谢谢你的更新,看起来很清新。我知道它看起来不那么干净,但你知道这是否可以在没有消除混叠的情况下完成吗?
plt.matshow()
默认情况下不使用插值。您可以使用关键字interpolation=biliner
(例如,请参阅)来更改该值。编辑:嗯,我误解你了。我以为你在要求抗锯齿(因为现在,它不应该有任何…)
import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
char_image = np.zeros((100, 150, 3), np.uint8)
# convert to pillow image
pillowImage = Image.fromarray(char_image)
# draw the text
font = ImageFont.truetype("arial.ttf", 32)
draw_rotated_text(pillowImage, 35, (50, 50), 'ABC', (128, 255, 128), font=font)
pillowImage.show()
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))