网页上搜索 “python绘制国际象棋棋盘”,索引结果均为调用 turtle 库绘制棋盘结果;为了填充使用 python pil 图像处理库绘制国际象棋棋盘的空白,今日分享此文。
1 pil绘制国际象棋棋盘流程
1.1 思路秒懂
步骤1:创建空白图片和绘画对象
步骤2:绘制网格
步骤3:填充颜色
1.2 分块解析
步骤1:创建空白图片和绘画对象
1
2
|
imagetemp = image.new( "rgb" , size, bgcolor) draw = imagedraw.draw(imagetemp) # 允许在imagetemp图片上画画 |
步骤2:绘制网格
绘制网格的关键是使用 python pil imagedraw.draw.line() 方法。
具体而言,本文采用分别绘制平均横直线和平均竖直线的方法。
下例为绘制平均竖直线:
1
2
3
4
5
6
7
8
9
|
for i in range ( 7 ): for j in range ( 7 ): i = i + 1 j = j + 1 everage_line = size[ 0 ] / 8 everage_line = everage_line * j start = (everage_line, 0 ) end = (everage_line, size[ 1 ]) draw.line([start, end], fill = ( 0 , 0 , 0 ), width = 3 ) |
注:采用 for 循环遍历列
因为 i,j 在分母,避免为0可能的报错,故+1
计算每两条竖线之间的距离
start起始值为“左”、“上”两值,end结束值为“右”,“下”两值
循环绘制7条竖线将白底绘板均分为8份
并设置绘制线条颜色及线条宽度等
步骤3:填充颜色
值得注意的是,填充颜色要保证相邻两块颜色不一致。
填充颜色的关键是使用 python pil imagedraw.draw.rectangle() 方法。
具体做法是先填充第一、第二行,再将生成图像复制粘贴。
填充第一、第二行方格时要注意填充矩形的起始点和结束点值,保证相邻两块颜色不一致。
下例为填充第一行方格(间隔填充):
1
2
3
4
|
draw.rectangle(( 0 , 0 , 50 , 50 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 100 , 0 , 150 , 50 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 200 , 0 , 250 , 50 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 300 , 0 , 350 , 50 ), fill = ( 0 , 0 , 0 )) |
填充好前两行方格后接下来就进入复制粘贴部分了;
对于第三第四行,就直接粘贴第一、二行图像就可以了:
1
2
|
region = imagetemp.crop(( 0 , 0 , 400 , 100 )) imagetemp.paste(region, ( 0 , 100 )) |
对于第五到第八行,在进行一遍复制粘贴,这回复制的是前四行的图像结果:
1
2
|
region = imagetemp.crop(( 0 , 0 , 400 , 200 )) imagetemp.paste(region, ( 0 , 200 )) |
2 完整代码
2.1 方法一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# coding=utf-8 from pil import image, imagedraw # 定义颜色、大小 size = ( 400 , 400 ) bgcolor = ( 255 , 255 , 255 ) # 创建空白图片和绘画对象 imagetemp = image.new( "rgb" , size, bgcolor) draw = imagedraw.draw(imagetemp) # 绘制平均竖直线 for i in range ( 7 ): for j in range ( 7 ): i = i + 1 j = j + 1 everage_line = size[ 0 ] / 8 everage_line = everage_line * j start = (everage_line, 0 ) end = (everage_line, size[ 1 ]) draw.line([start, end], fill = ( 0 , 0 , 0 ), width = 3 ) # 绘制平均横直线 for i in range ( 7 ): for j in range ( 7 ): i = i + 1 j = j + 1 everage_line = size[ 0 ] / 8 everage_line = everage_line * i start = ( 0 , everage_line) end = (size[ 0 ], everage_line) draw.line([start, end], fill = ( 0 , 0 , 0 ), width = 3 ) # 先涂两行方格 # 第一行间隔涂黑 draw.rectangle(( 0 , 0 , 50 , 50 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 100 , 0 , 150 , 50 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 200 , 0 , 250 , 50 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 300 , 0 , 350 , 50 ), fill = ( 0 , 0 , 0 )) # 第二行间隔涂黑 draw.rectangle(( 50 , 50 , 100 , 100 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 150 , 50 , 200 , 100 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 250 , 50 , 300 , 100 ), fill = ( 0 , 0 , 0 )) draw.rectangle(( 350 , 50 , 400 , 100 ), fill = ( 0 , 0 , 0 )) # 复制粘贴 # 操作第三行和第四行 region = imagetemp.crop(( 0 , 0 , 400 , 100 )) imagetemp.paste(region, ( 0 , 100 )) # 操作第五行到第八行 region = imagetemp.crop(( 0 , 0 , 400 , 200 )) imagetemp.paste(region, ( 0 , 200 )) # 显示 imagetemp.show() |
2.2 方法二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
from pil import image, imagedraw imagetemp = image.new( 'rgb' , ( 400 , 400 ), 0 ) draw = imagedraw.draw(imagetemp) h,w = imagetemp.size for x in range ( 7 ): for y in range ( 7 ): x = x + 1 y = y + 1 x_zuobiao = w / 8 x_zuobiao = x_zuobiao * x start = (x_zuobiao, 0 ) end = (x_zuobiao, h) draw.line([start, end], fill = ( 256 , 256 , 256 ), width = 3 ) for x in range ( 7 ): for y in range ( 7 ): x = x + 1 y = y + 1 y_zuobiao = h / 8 y_zuobiao = y_zuobiao * y start = ( 0 , y_zuobiao) end = (w, y_zuobiao) draw.line([start, end], fill = ( 256 , 256 , 256 ), width = 3 ) x = 0 y = 0 z = 50 t = 50 for i in range ( 4 ): for i in range ( 2 ): for j in range ( 4 ): if (y< 450 ): draw.rectangle((x, y, z, t), fill = ( 255 , 255 , 255 )) x = x + 100 z = z + 100 for i in range ( 4 ): x = x - 100 z = z - 100 x = x + 50 y = y + 50 z = z + 50 t = t + 50 x = x - 100 z = z - 100 imagetemp.show() |
2.3 方法三(精简版)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from pil import image, imagedraw imagetemp = image.new( 'rgb' ,( 400 , 400 ), 0 ) draw = imagedraw.draw(imagetemp) h,w = imagetemp.size x = 0 ; y = 0 ;z = 50 ;t = 50 for a in range ( 4 ): for i in range ( 2 ): for j in range ( 4 ): if (y< 450 ): draw.rectangle((x, y, z, t), fill = ( 255 , 255 , 255 )) x = x + 100 z = z + 100 x = 0 ;z = 50 ;x = x + 50 ;y = y + 50 ;z = z + 50 ;t = t + 50 x = 0 ;z = 50 imagetemp.show() |
3 结果展示
到此这篇关于python实现pil图像处理库绘制国际象棋棋盘的文章就介绍到这了,更多相关python 国际象棋棋盘内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/IT_charge/article/details/118734453