Python PIL - 透明和虚线? [英] Python PIL - transparent and dashed lines?

查看:66
本文介绍了Python PIL - 透明和虚线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将网格叠加到图像上,以便我可以使用它来定位文本.我正在使用 PIL (Python) 来绘制网格.理想情况下,我会使用虚线且相当透明的次要网格线,以便我可以看到图像,以及实心且透明度较低的主要网格线.

I am trying to superimpose a grid onto an image so that I can use it to position text. I am using PIL (Python) to draw the grid. Ideally I would have dashed and reasonably transparent minor gridlines so that I can see through to the image, and solid and less transparent major gridlines.

我正在尝试做的工作示例如下.

A working example of what I am trying to do is below.

我的问题参考以下几行(为演示而简化):

My questions refer to the following lines (simplified for presentation):

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 时,它会在我认为我能够看到它时隐藏它下面的文本.

1) I understood the fourth parameter of the fill controls the transparency of the line, with 0 being completely transparent and 255 being completely opaque. However, I cannot detect any difference between a value or 1 or 200, and indeed, when I use 1, it hides the text beneath it when I thought I would be able to see it.

2) 如何在 PIL 中制作虚线?我没有看到如何.可能吗?

2) How can you make dashed lines in PIL? I have not seen how. Is it possible?

最后,我对编程和 python 比较陌生,如果您可以在下面的 MWE 上提供任何提示以使其达到更好的标准,我将不胜感激.

Finally, I am relatively new to programming and python, if there are any tips you can offer on the below MWE to bring it up to a better standard, I would be most grateful.

注意:下面的示例使用红线作为主要网格线,但我希望它们与次要网格线颜色相同,次要网格线为透明和虚线.

Note: The example below uses red lines for the major gridlines, but I would like them to be the same colour as the minor gridlines, with the minor gridlines transparent and dashed.

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' 替换为布尔值.您应该使用 TrueFalse,而不是字符串比较,来检查是否使用 if grid: 启用设置.

First, replace grid = 'on' with a boolean. You should be using True or False, not string comparisons, to check if the setting is enabled with if grid:.

其次,PIL 默认没有绘制虚线的能力.要绘制虚线,您必须绘制几条彼此间隔开的小线.但是,对于您要执行的操作,虚线不是必需的;可以高度控制地设置线条的不透明度.

Second, PIL doesn't have the ability to draw dashed lines by default. To draw a dashed line you have to draw several small lines spaced apart from each other. However, for what you're trying to do, dashed lines should not be necessary; Setting the opacity of a line can be done with a high degree of control.

最后,您应该能够通过您的方法对线条的不透明度进行非常精细的控制.gridlines() 函数做了很多额外的工作;它会无缘无故地重新绘制图像、绘制文本等.因此,最有可能发生的是您的网格线被多次相互绘制,导致它们变得越来越不透明.

Finally, you should be able to achieve very fine control over the opacity of the lines with your approach. The gridlines() function is doing a lot of extra work; It redraws the image for no reason, draws text, etc. So what's most likely going on is your grid lines are being drawn over each other several times, causing them to get more and more opaque.

如果它使我们的网格绘图正常,那么您应该将图像保存为 PNG,而不是 JPEG.JPEG 不能很好地呈现红色,因为它旨在保存摄影图像,这意味着它将更多信息用于存储我们的眼睛看到更多的绿色和蓝色.

If it turns our your grid drawing is fine, then you should save your image as a PNG, not a JPEG. JPEG does not render red very well since it's designed to save photographic imagery and that means it dedicates more information to storing greens and blues, which our eyes see more of.

这篇关于Python PIL - 透明和虚线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆