我在Python中使用numpy数组图像绘制文本(使用自定义字体)。目前我正在将图像转换为PIL,绘制文本然后转换回numpy数组。
string args = string.Format("/s /o /h /t \"{0}\" \"{1}\"", filepath, printerName);
var startInfo = new ProcessStartInfo {
FileName = Properties.Settings.Default.AdobeReaderPath,
Arguments = args,
CreateNoWindow = true,
ErrorDialog = false,
UseShellExecute = false,
Verb = "print",
WindowStyle = ProcessWindowStyle.Minimized,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
var process = Process.Start(startInfo);
无论如何都要在给定的角度上绘制文本,即。 33度?
绘制文本后旋转图像不是一个选项
答案 0 :(得分:2)
您可以使用PIL绘制旋转的文本。我建议将文本绘制到空白图像上,旋转该图像,然后将旋转后的图像粘贴到主图像中。类似的东西:
def draw_rotated_text(image, angle, xy, text, fill, *args, **kwargs):
""" Draw text at an angle into an image, takes the same arguments
as Image.text() except for:
:param image: Image to write text into
:param angle: Angle to write text at
"""
# get the size of our image
width, height = image.size
max_dim = max(width, height)
# build a transparency mask large enough to hold the text
mask_size = (max_dim * 2, max_dim * 2)
mask = Image.new('L', mask_size, 0)
# add text to mask
draw = ImageDraw.Draw(mask)
draw.text((max_dim, max_dim), text, 255, *args, **kwargs)
if angle % 90 == 0:
# rotate by multiple of 90 deg is easier
rotated_mask = mask.rotate(angle)
else:
# rotate an an enlarged mask to minimize jaggies
bigger_mask = mask.resize((max_dim*8, max_dim*8),
resample=Image.BICUBIC)
rotated_mask = bigger_mask.rotate(angle).resize(
mask_size, resample=Image.LANCZOS)
# crop the mask to match image
mask_xy = (max_dim - xy[0], max_dim - xy[1])
b_box = mask_xy + (mask_xy[0] + width, mask_xy[1] + height)
mask = rotated_mask.crop(b_box)
# paste the appropriate color, with the text transparency mask
color_image = Image.new('RGBA', image.size, fill)
image.paste(color_image, mask)
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()
答案 1 :(得分:1)
使用matplotlib,首先可视化数组并在其上绘制,从图中获取原始数据。
亲:这两个工具都是相当高的水平,让你处理过程的许多细节。 ax.annotate()
为绘制和设置字体属性的位置和方式提供了灵活性,plt.matshow()
提供了灵活性,使您可以处理数组可视化的各个方面。
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))