你好,我想畫一條虛線或虛線到png,我找不到我該怎么做,有人可以幫忙嗎?
im = Image.new('RGB', (2000,2000),tuple(int(hex_color[i:i 2], 16) for i in (0, 2, 4))) print("...Saving...") im.save('C:\\Users\\th3m1s\\Desktop\\Lejant\\' str(legend_code) '.png', quality=100)
結果是點擊這里
uj5u.com熱心網友回復:
您是否考慮過使用垂直線和水平線創建一個比原始影像略高和寬的新影像,并將原始影像粘貼到該影像上?這樣你就會有一個虛線邊框,它適用于各種尺寸。
這可以按照此處的說明完成:如何在 Python 中使用 PIL 將影像合成到另一個影像上?
from PIL import Image,ImageDraw
#this is your own image
yourimage = Image.open('/home/vancha/Documenten/python/pillu/square.png', 'r')
img_w, img_h = yourimage.size
border_width = 5
#this is the new image which holds the stripes
borderimage = Image.new('RGBA', (2000 (border_width * 2), 2000 (border_width *2)), (255, 255, 255, 255))
# Draw the lines
draw = ImageDraw.Draw(borderimage)
#starts drawing vertical lines form the very top
start = 0
end = borderimage.height#width or height, doens't matter since the image is square
step_size = border_width*4
#starts from border_width * 2, so that the very top and very left aren't made black with lines
for x in range(border_width*2, borderimage.width, step_size):
vertical_line = ((x, start), (x, end))
#the width is the thickness of the "dots" in the border
draw.line(vertical_line, fill=(0,0,0),width=border_width * 2)
horizontal_line = ((start,x), (end, x))
draw.line(horizontal_line, fill=(0,0,0),width=border_width *2)
#for good practice:
del draw
bg_w, bg_h = borderimage.size
#calculate the offset so that the image is centered
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
#paste your old image over the one with the dots
borderimage.paste(yourimage, offset)
#save it wherever you want :)
borderimage.save('./border.png')
在你的情況下,如果你希望你的邊框在你的影像周圍一直是 5px,并且你的影像是 2000,2000,那么到 2010 年將新影像的大小更改為 2010 會在兩邊留出 5px,如果你將您自己的影像粘貼到中心。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497124.html
