实验十四 生成棋盘纹理图片
实验目的
- 熟悉Python扩展库pillow的安装方法。
- 熟悉Python扩展库pillow的简单使用。
- 理解棋盘网格纹理的生成原理。
实验内容
编写程序,绘制棋盘网格,要求棋盘的宽度和高度、交替的两种颜色以及网格数量都可以通过参数指定,并且两种颜色交替出现,水平方向和垂直方向上的网格数量相同。
特别的,棋盘为正方形,其宽高相等,棋盘边长与每边网格数量均为整数,颜色为RGB格式以元组的形式输入。
参考文章
Python实现PIL图像处理库绘制国际象棋棋盘
![](https://i-blog.csdnimg.cn/blog_migrate/52d5d11fe12fa16df923b8ec847dd3c9.png)
源代码
- 绘制棋盘背景
返回了一个Image对象,可以视为一个指定边长和背景色的正方形图片。def draw_image(width: int, color: tuple) -> Image: # 定义颜色、大小 size = (width, width) bgcolor = color # 创建空白图片和绘画对象 imagetemp = Image.new("RGB", size, bgcolor) return imagetemp
- 绘制纵横线
参数n表示每边方格的数量,因此纵横方向生成的线各有n-1条。
draw.line表示在图片上画线。draw参数就代表着图片。def draw_line_y(draw, n, width, color): for i in range(n-1): for j in range(n-1): i = i + 1 j = j + 1 everage_line = width / n everage_line = everage_line * j start = (everage_line, 0) end = (everage_line, width) draw.line([start, end], fill=color, width=3) def draw_line_x(draw, n, width, color): for i in range(n-1): for j in range(n-1): i = i + 1 j = j + 1 everage_line = width / n everage_line = everage_line * i start = (0, everage_line) end = (width, everage_line) draw.line([start, end], fill=color, width=3)
- 绘制方格
先根据边长和每边方格的个数确定方格尺寸length。
先绘制第一行,隔一个方格涂一个色。
接下来进行判断,每边方格数是否大于等于2:
I.如果小于2(也就是1)说明只有一个方格,输出图片为一单色块。
II.如果大于等于2,那么以不同的方式再绘制一行,以此实现交替的效果。
接下来通过crop和paste即可实现对第一、二行的复制粘贴,实现效果。def draw_block(width, n, imagetemp, color): length = width / n for i in range(n//2+1): print(i) draw.rectangle(((2*i)*length,0,(2*i+1)*length , length), fill=color) if n >= 2: for i in range(n//2+1): draw.rectangle(((2*i+1)*length, length, 2*(i+1)*length, 2*length), fill = color) region = imagetemp.crop((0,0,width,length)) region2 = imagetemp.crop((0,length,width,2*length)) for i in range(2,n): if i % 2 == 1: imagetemp.paste(region2, (0, int((i*length)))) else: imagetemp.paste(region, (0, int(i*length)))
- 总览
import sys from PIL import Image, ImageDraw # 参考自http://zzvips.com/article/212257.html def draw_image(width: int, color: tuple) -> Image: # 定义颜色、大小 size = (width, width) bgcolor = color # 创建空白图片和绘画对象 imagetemp = Image.new("RGB", size, bgcolor) return imagetemp def draw_line_y(draw, n, width, color): for i in range(n-1): for j in range(n-1): i = i + 1 j = j + 1 everage_line = width / n everage_line = everage_line * j start = (everage_line, 0) end = (everage_line, width) draw.line([start, end], fill=color, width=3) def draw_line_x(draw, n, width, color): for i in range(n-1): for j in range(n-1): i = i + 1 j = j + 1 everage_line = width / n everage_line = everage_line * i start = (0, everage_line) end = (width, everage_line) draw.line([start, end], fill=color, width=3) def draw_block(width, n, imagetemp, color): length = width / n for i in range(n//2+1): draw.rectangle(((2*i)*length,0,(2*i+1)*length , length), fill=color) if n >= 2: for i in range(n//2+1): draw.rectangle(((2*i+1)*length, length, 2*(i+1)*length, 2*length), fill = color) region = imagetemp.crop((0,0,width,length)) region2 = imagetemp.crop((0,length,width,2*length)) for i in range(2,n): if i % 2 == 1: imagetemp.paste(region2, (0, int((i*length)))) else: imagetemp.paste(region, (0, int(i*length))) if __name__ == '__main__': n = input("请输入每边的方格数:") if n.isdigit(): n = int(n) else: print("请输入整数!") sys.exit() width = input("请输入每边的边长:") if width.isdigit(): width = int(width) else: print("请输入整数!") sys.exit() color = eval(input("请输入背景颜色,示例:(255,255,255)")) imagetemp = draw_image(width, color) draw = ImageDraw.Draw(imagetemp) color = eval(input("请输入不同颜色,示例:(0,0,0)")) draw_line_x(draw, n, width, color) draw_line_y(draw, n, width, color) draw_block(width, n, imagetemp, color) imagetemp.show()
效果图如下(下面的是VScode终端里的)