我正在尝试将网格叠加到图像上,以便我可以使用它来定位文本。我正在使用 PIL (Python) 来绘制网格。理想情况下,我会使用虚线且相当透明的次网格线,以便我可以看到图像,并使用实线且不太透明的主网格线。
下面是我正在尝试做的一个工作示例。
我的问题涉及以下几行(为了演示而简化):
draw.line( (10, 0, 10, imgHeight), fill = (180, 180, 255, 1) )
draw.line( (10, 0, 10, imgHeight), fill = (180, 180, 255, 200) )
1) 我理解填充的第四个参数控制线条的透明度,0 表示完全透明,255 表示完全不透明。但是,我无法检测到值或 1 或 200 之间的任何差异,事实上,当我使用 1 时,当我认为我能够看到它时,它会隐藏它下面的文本。
2) 如何在 PIL 中制作虚线?我还没有看到如何。可能吗?
最后,我对编程和Python还比较陌生,如果您可以对下面的MWE提供任何提示以使其达到更好的标准,我将不胜感激。
注意:下面的示例使用红线作为主要网格线,但我希望它们与次要网格线颜色相同,次要网格线透明且虚线。
import PIL
from PIL import Image, ImageFont, ImageDraw
grid = 'on'
minor = 5
major = 50
font = ImageFont.truetype('arial.ttf', 10)
textColor = (38, 23, 255)
img = Image.open('2013MCS7.jpg')
draw = ImageDraw.Draw(img)
def gridlines(img, gridWidth, color, lineWidth=1, direction='b', label='n', labelRepeat=None, LabelFont='arial', labelFontSize=10):
'''
Draws gridlines on an image.
img : the image to be modified
gridwith : (int > 0) : size of gridlines in pixels
color : tuple of length 3 or 4 (4th argument controls transparency) : color of gridlines and labels
lineWidth : (int > 0) : width of gridlines
direction : ('v','h','b') : specifies either vetical gridlines, horizontal gridlines or both
label : ('y','n') : turns grid labels on or off
labelRepeat :
'''
imgWidth, imgHeight = img.size
draw = ImageDraw.Draw(img)
textColor = (color[0], color[1], color[2])
textFont = ImageFont.truetype(LabelFont+'.ttf', labelFontSize)
# add gridlines
if direction.lower() == 'b' or direction.lower() == 'v':
for v in range(1, int(imgWidth/gridWidth)+1):
draw.line( (v*gridWidth, 0, v*gridWidth, imgHeight), fill = color, width = lineWidth )
if direction.lower() == 'b' or direction.lower() == 'h':
for h in range(1, int(imgHeight/gridWidth)+1):
draw.line( (0, h*gridWidth, imgWidth, h*gridWidth), fill = color, width = lineWidth )
# add labels
if label.lower() == 'y':
for v in range(1, int(imgWidth/gridWidth)+1):
for h in range(1, int(imgHeight/gridWidth)+1):
if v == 1:
draw.text( ( 3, 1+h*gridWidth), str(h), fill = textColor, font = textFont )
if h == 1:
draw.text( ( 1+v*gridWidth, 3), str(v), fill = textColor, font = textFont )
if labelRepeat is not None:
if ( h % labelRepeat == 0 ) and ( v % labelRepeat == 0 ):
draw.text( ( 1+v*gridWidth, 1+h*gridWidth), '('+str(h)+','+str(v)+')', fill = textColor, font = textFont )
# draw gridlines
if grid == 'on':
gridlines(img, minor, (180, 180, 255, 1))
gridlines(img, major, (255, 0, 0, 100), label='Y', labelRepeat=3)
# populate form
draw.text( (6*major+2*minor+3, 6*major+5*minor+2), 'econ_total', fill=textColor, font=font)
draw.text( (6*major+2*minor+3, 7*major+1*minor+2), 'notional_taxed', fill=textColor, font=font)
draw.text( (6*major+2*minor+3, 7*major+7*minor+2), 'notional_employer', fill=textColor, font=font)
draw.text( (6*major+2*minor+3, 8*major+4*minor+3), 'supca_total', fill=textColor, font=font)
draw.text( (6*major+2*minor+3, 9*major+2*minor-1), 'cgt_exempt_sb_ret', fill=textColor, font=font)
draw.text( (6*major+2*minor+3, 9*major+7*minor+0), 'cgt_exempt_sb_15yr', fill=textColor, font=font)
del draw
img.save('marked-up - 2013MCS7.jpg')
最佳答案
首先,将grid = 'on'
替换为 bool 值。您应该使用 True
或 False
(而不是字符串比较)来检查该设置是否已通过 if grid:
启用。
其次,PIL默认不具备绘制虚线的能力。要绘制虚线,您必须绘制几条彼此间隔开的小线。然而,对于您想要做的事情,虚线不是必需的;设置线条的不透明度可以通过高度控制来完成。
最后,您应该能够通过您的方法对线条的不透明度进行非常精细的控制。 gridlines() 函数做了很多额外的工作;它无缘无故地重绘图像、绘制文本等。所以最有可能发生的情况是你的网格线被多次绘制,导致它们变得越来越不透明。
如果您的网格绘图没问题,那么您应该将图像保存为 PNG,而不是 JPEG。 JPEG 不能很好地渲染红色,因为它旨在保存摄影图像,这意味着它专用于存储绿色和蓝色的更多信息,而我们的眼睛看到的更多。
关于Python PIL - 透明线和虚线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601777/