使用pillow库生成一个彩色棋盘
时间: 2024-05-06 22:21:03 浏览: 104
Sure, here is some sample code to generate a colored chessboard using the Pillow library in Python:
``` python
from PIL import Image
# Define image size and block size
size = 400
block_size = size//8
# Create image object
img = Image.new('RGB', (size, size), (255, 255, 255))
# Create color tuples for white and black
white = (255, 255, 255)
black = (0, 0, 0)
# Fill the image with alternating white and black blocks
for i in range(8):
for j in range(8):
if (i+j) % 2 == 0:
color = white
else:
color = black
img.paste(color, (i*block_size, j*block_size, (i+1)*block_size, (j+1)*block_size))
# Save the image
img.save('colored_chessboard.png')
```
Hope this helps!
阅读全文