Drawing chess board with PIL image processing library in Python

Time:2021-11-24
catalogue
  • 1. PIL drawing chess board process
    • 1.1 understanding
    • 1.2 block analysis
  • 2 complete code
    • 2.1 method I
    • 2.2 method II
    • 2.3 method 3 (simplified version)
  • 3 result display

    Search “Python drawing chess board” on the web page, and the index results are the results of drawing chess board by calling the turtle library; In order to fill the blank of drawing a chess board using Python PIL image processing library, share this article today.

    1. PIL drawing chess board process

    1.1 understanding

    Step 1: create blank pictures and painting objects

    Step 2: draw the mesh

    Step 3: fill color

    1.2 block analysis

    Step 1: create blank pictures and painting objects

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

    Step 2: draw the mesh

    The key to drawing a mesh is to use the python PIL ImageDraw. Draw. Line () method.

    Specifically, this paper adopts the method of drawing average horizontal line and 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)

    notes: use the for loop to traverse the column
            Because I and j are in the denominator, avoid possible error reporting of 0, so + 1
            Calculate the distance between each two vertical lines
            The start value is left and top, and the end value is right and bottom
            Circle 7 vertical lines to divide the white background drawing board into 8 copies
            And set the drawing line color and line width

    Step 3: fill color

    It should be noted that the filling color should ensure that the adjacent two colors are inconsistent.

    The key to fill color is to use the python PIL ImageDraw. Draw. Rectangle () method.

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

    When filling the first and second rows of squares, pay attention to the values of the start and end points of the filled 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 filling):

    
    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, you will enter the copy and paste section;

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

    
    region = imageTemp.crop((0,0,400,100))
    imageTemp.paste(region, (0, 100))

    For the fifth to eighth lines, copy and paste again. This time, copy the image results of the first four lines:

    
    region = imageTemp.crop((0,0,400,200))
    imageTemp.paste(region, (0, 200))

    2 complete code

    2.1 method I

    # coding=utf-8
    from PIL import Image, ImageDraw
     
    #Define color, size
    size = (400, 400)
    bgcolor = (255, 255, 255)
     
    #Create blank pictures and painting objects
    imageTemp = Image.new("RGB", size, bgcolor)
    draw = ImageDraw.Draw(imageTemp)
     
    #Draw 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 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 * i
            start = (0, everage_line)
            end = (size[0], everage_line)
            draw.line([start, end], fill=(0, 0, 0), width=3)
     
    #Paint two lines of squares first
    #The first line is blackened
    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))
    #The second line is blackened
    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))
     
    #Copy paste
    #Operate on the third and fourth lines
    region = imageTemp.crop((0,0,400,100))
    imageTemp.paste(region, (0, 100))
    #Operation lines 5 to 8
    region = imageTemp.crop((0,0,400,200))
    imageTemp.paste(region, (0, 200))
     
    #Show
    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/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 method 3 (simplified version)

    
    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 result display

    This is the end of this article about Python’s implementation of PIL image processing library and drawing chess board. For more information about Python chess board, please search for previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

    Recommended Today

    Apache sqoop

    Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]