简体   繁体   English

在python中绘制彩色棋盘格

[英]Draw colored checkers grid in python

i need to draw a colored checkers grid in an existing image, with colors taken from a list. 我需要在现有图像中绘制彩色棋盘格,并从列表中选取颜色。 This is the code i have: 这是我的代码:

import image as im
def draw_gen_checkers(img, s, colors):
    n = 0
    for i in range(len(img)/s):
        for j in range(len(img[0])/s):
            for ii in range(i*s, i*s+s):
                for jj in range(j*s,j*s+s):
                    img[ii][jj] = colors[n]
            if n == len(colors)-1:
                n = 0
            else:
                n = n + 1
    im.visd(img)

img = im.create(300, 200, (0,0,0))
colors = [(255,128,0),(0,0,255),(0,255,0),(255,0,0)]
s = 25

With this code this is the result i get: Image1 使用此代码,这是我得到的结果: Image1

When the image should look like this: Image 当图像应该是这样的: 图片

Ty all in advance! 提前输入!

Glad to see your work progress :) 很高兴看到您的工作进度:)

Let's do the math : your picture is 300px long, your cases are 25px long, so you have 12 cases per line, since you have 4 colors you have 12 // 4 == 3 color rotations by line, but since 12 % 3 == 0, when you start your new line, your n is back to 0. And nowhere you say that you want an increasing offset for colors in each line, so all your lines are the same. 让我们做个数学:您的图片长300像素,案例是25像素长,因此每行有12个案例,因为有4种颜色,所以每行有12 // 4 == 3个颜色旋转,但是由于12%3 = = 0,当您开始新的一行时,您的n返回到0。并且在任何地方都没有说要增加每一行中颜色的偏移量,因此所有行都是相同的。

Just adding an increment at the start of each line should do the trick. 只需在每行的开头添加一个增量即可。

For your n increment, instead of using an if-else people often use the modulo operator n = (n+1) % len(colors) # 0, 1, 2, 3, 0, 1, ... 对于您的n增量,人们经常使用模运算符n = (n+1) % len(colors) # 0, 1, 2, 3, 0, 1, ... ,而不是使用if-else n = (n+1) % len(colors) # 0, 1, 2, 3, 0, 1, ...

edit: when I said 'adding an increment at the start of each line' I was thinking of something like that (i think it should be good): 编辑:当我说“在每行的开头添加一个增量”时,我想到的是这样的东西(我认为应该不错):

def draw_gen_checkers(img, s, colors):
    n = 0 # Alternatively n = len(colors)
    for i in range(len(img)/s):
        n = (n + 1) % len(colors)
        for j in range(len(img[0])/s):
            for ii in range(i*s, i*s+s):
                for jj in range(j*s,j*s+s):
                    img[ii][jj] = colors[n]
            n = (n + 1) % len(colors)

The drawback would be that the first color you use will be the second in your list since it increments n before everything else. 缺点是您使用的第一种颜色将是列表中的第二种颜色,因为它在所有其他内容之前先增加n。 Depending on what you do it could be a problem or not, if it is you can initialize n to len(colors)-1 to fix it. 取决于您执行的操作,是否可能会出现问题,如果可以,则可以将n初始化为len(colors)-1进行修复。

I would also create a function draw_square(i, j, s, c), with your 2 most inner loops, that would make the code more readable and more modular (this function would probably be useful somewhere else at some point). 我还将创建一个函数draw_square(i,j,s,c),使用您的2个最内部的循环,这将使代码更具可读性和模块化(此功能有时可能在其他地方有用)。

def draw_gen_checkers(img, s, colors):
n = 0
for i in range(len(img)/s + 1):
    for j in range(len(img[0])/s + 1):
        for ii in range(i*s, i*s+s):
            for jj in range(j*s,j*s+s):
                if 0 <= ii < len(img) and 0 <= jj < len(img[0]):
                    img[ii][jj] = colors[n]
        n = (n+1) % len(colors)
im.visd(img)

img = im.create(300, 200, (0,0,0))
colors = [(255,128,0),(0,0,255),(0,255,0),(255,0,0)]
s = 25

It prints: img 打印: img

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM