关于#python#的问题:有关python中图片方面的问题

有关python中图片方面的问题,需要画出一下的棋盘,想问下怎么解决?

img

用for循环

代码实现如下,望采纳

import numpy as np
from PIL import Image

def ChessImage (cubsize, numcub):
  # Create a new image with the specified size
  img = Image.new('RGB', (numcub*cubsize, numcub*cubsize))

  # Loop through each pair of rows and columns
  for i in range(numcub):
    for j in range(numcub):
      # If the current pair of rows and columns is even, fill the square with white
      if (i+j) % 2 == 0:
        img.paste((255,255,255), (i*cubsize, j*cubsize, (i+1)*cubsize, (j+1)*cubsize))
      # If the current pair of rows and columns is odd, fill the square with black
      else:
        img.paste((0,0,0), (i*cubsize, j*cubsize, (i+1)*cubsize, (j+1)*cubsize))
  
  return img

# Create a chessboard with 50x50 squares and 5 pairs of repeating black/white squares
img = ChessImage(50, 5)

# Save the image
img.save('chessboard.png')