Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw Chess Board with PIL Image processing Library based on Python

Shulou Source: shulou.com Published: 2022-06-01 02:48:41 09月24日 Update

In this article Xiaobian for you to introduce in detail "Python how to achieve PIL image processing database to draw chess chessboard", detailed content, clear steps, details handled properly, I hope that this "Python how to achieve PIL image processing database to draw chess board" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

1 PIL draw chess board flow 1.1 second understand

Step 1: create blank pictures and paint objects

Step 2: draw the mesh

Step 3: fill Color

1.2 Block parsing

Step 1: create blank pictures and paint objects

ImageTemp = Image.new ("RGB", size, bgcolor) draw = ImageDraw.Draw (imageTemp) # allows you to draw on imageTemp pictures

Step 2: draw the mesh

The key to drawing the mesh is to use the Python PIL ImageDraw.Draw.line () method.

Specifically, this paper adopts the method of drawing the average horizontal line and the average vertical line respectively.

The following example is to draw an average vertical line:

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)

Note: use for to cycle through the column

Because iMagol j is in the denominator, avoid reporting possible errors for 0, so + 1

Calculate the distance between every two vertical lines

The starting values of start are "left" and "top", and the end values of end are "right" and "bottom".

The white background drawing board is evenly divided into 8 parts by drawing 7 vertical lines in a cycle.

And set the color and width of the drawing line.

Step 3: fill Color

It is worth noting that the fill color should ensure that the colors of the two adjacent blocks are not consistent.

The key to fill colors is to use the Python PIL ImageDraw.Draw.rectangle () method.

The specific method is to fill the first and second lines first, and then copy and paste the generated image.

When filling the first and second rows of squares, we should pay attention to the starting point and end point values of the filling rectangle to ensure that the colors of the two adjacent blocks are inconsistent.

The following example is to fill the first row of squares (interval padding):

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))

After filling in the first two rows of squares, we will go to the copy and paste section.

For the third and fourth lines, just paste the first and second lines of images directly:

Region = imageTemp.crop ((0j0400100)) imageTemp.paste (region, (0100))

For lines 5 to 8, copy and paste again, this time copying the image results from the first four lines:

Region = imageTemp.crop ((0J0400200)) imageTemp.paste (region, (0200)) 2 complete Code 2.1Method # coding=utf-8from PIL import Image, ImageDraw # define color and size size = (400,400) bgcolor = (255,255,255) # create blank pictures and drawing objects imageTemp = Image.new ("RGB", size) Bgcolor) draw = ImageDraw.Draw (imageTemp) # draw an average vertical line 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) # draw average horizontal lines 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) # first two lines of squares # first line interval blackening 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)) # second line interval blackening draw.rectangle (50) 50,100,100), fill = (0jue 0,0) draw.rectangle ((150,50,200,100), fill = (0je 0,0)) draw.rectangle ((250,50,300,100), fill = (0jue 0,0)) draw.rectangle ((350,50,400,100), fill = (0jue 0,0)) # copy and paste # the third and fourth lines region = imageTemp.crop ((0400100)) imageTemp.paste (region, (0) Region = imageTemp.crop ((0j0400200)) imageTemp.paste (region, (0200)) # shows imageTemp.show () 2.2 method II 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 x_zuobiao*x start 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 y_zuobiao 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 = 0y = 0z = 50t = 50for i in range (4): for i in range (2): for j in range (4): if (y

Tags: Color step image method international chess board chess processing picture box image processing object article blank painting grid four lines consistent one line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft Shulou Technology Xiaomi Apple