如何制作numpy棋盘?

时间:2010-01-30 21:19:53

标签: python numpy

我正在使用numpy将像素数组初始化为灰色棋盘格(“无像素”或透明的经典表示)。似乎应该有一种奇怪的方式来做numpy的惊人的阵列分配/切片/切割操作,但这是我提出的最好的:

w, h = 600, 800
sq = 15    # width of each checker-square
self.pix = numpy.zeros((w, h, 3), dtype=numpy.uint8)
# Make a checkerboard
row = [[(0x99,0x99,0x99),(0xAA,0xAA,0xAA)][(i//sq)%2] for i in range(w)]
self.pix[[i for i in range(h) if (i//sq)%2 == 0]] = row
row = [[(0xAA,0xAA,0xAA),(0x99,0x99,0x99)][(i//sq)%2] for i in range(w)]
self.pix[[i for i in range(h) if (i//sq)%2 == 1]] = row

它有效,但我希望有更简单的东西。

27 个答案:

答案 0 :(得分:12)

这应该这样做

你想要的任何尺寸棋盘(只需传递宽度和高度,如w,h);我也将硬编码的单元格高度/宽度设置为1,但当然这也可以参数化,以便传递任意值:

>>> import numpy as NP

>>> def build_checkerboard(w, h) :
      re = NP.r_[ w*[0,1] ]              # even-numbered rows
      ro = NP.r_[ w*[1,0] ]              # odd-numbered rows
      return NP.row_stack(h*(re, ro))


>>> checkerboard = build_checkerboard(5, 5)

>>> checkerboard
 Out[3]: array([[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
               [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
               [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
               [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
               [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
               [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
               [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
               [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
               [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
               [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]])

使用这个2D数组,渲染棋盘图像非常简单,如下所示:

>>> import matplotlib.pyplot as PLT

>>> fig, ax = PLT.subplots()
>>> ax.imshow(checkerboard, cmap=PLT.cm.gray, interpolation='nearest')
>>> PLT.show()

答案 1 :(得分:9)

我使用Kronecker product kron

np.kron([[1, 0] * 4, [0, 1] * 4] * 4, np.ones((10, 10)))

本例中的棋盘每个方向有2 * 4 = 8个大小为10x10的字段。

答案 2 :(得分:6)

以下是另一种使用ogrid进行此操作的方法:

import numpy as np
import Image

w, h = 600, 800
sq = 15
color1 = (0xFF, 0x80, 0x00)
color2 = (0x80, 0xFF, 0x00)

def use_ogrid():
    coords = np.ogrid[0:w, 0:h]
    idx = (coords[0] // sq + coords[1] // sq) % 2
    vals = np.array([color1, color2], dtype=np.uint8)
    img = vals[idx]
    return img

def use_fromfunction():
    img = np.zeros((w, h, 3), dtype=np.uint8)
    c = np.fromfunction(lambda x, y: ((x // sq) + (y // sq)) % 2, (w, h))
    img[c == 0] = color1
    img[c == 1] = color2
    return img

if __name__ == '__main__':
    for f in (use_ogrid, use_fromfunction):
        img = f()
        pilImage = Image.fromarray(img, 'RGB')
        pilImage.save('{0}.png'.format(f.func_name))

以下是时间结果:

% python -mtimeit -s"import test" "test.use_fromfunction()"
10 loops, best of 3: 307 msec per loop
% python -mtimeit -s"import test" "test.use_ogrid()"
10 loops, best of 3: 129 msec per loop

答案 3 :(得分:4)

我不确定这是否比我的更好:

c = numpy.fromfunction(lambda x,y: ((x//sq) + (y//sq)) % 2, (w,h))
self.chex = numpy.array((w,h,3))
self.chex[c == 0] = (0xAA, 0xAA, 0xAA)
self.chex[c == 1] = (0x99, 0x99, 0x99)

答案 4 :(得分:4)

迟到了,但后人:

def check(w, h, c0, c1, blocksize):
  tile = np.array([[c0,c1],[c1,c0]]).repeat(blocksize, axis=0).repeat(blocksize, axis=1)
  grid = np.tile(tile, ( h/(2*blocksize)+1, w/(2*blocksize)+1, 1))
  return grid[:h,:w]

答案 5 :(得分:2)

你不能使用hstack和vstack吗?见here。 像这样:

>>> import numpy as np
>>> b = np.array([0]*4)
>>> b.shape = (2,2)
>>> w = b + 0xAA
>>> r1 = np.hstack((b,w,b,w,b,w,b))
>>> r2 = np.hstack((w,b,w,b,w,b,w))
>>> board = np.vstack((r1,r2,r1,r2,r1,r2,r1))

答案 6 :(得分:2)

import numpy as np
x = np.ones((3,3))
print("Checkerboard pattern:")
x = np.zeros((8,8),dtype=int)
# (odd_rows, even_columns)
x[1::2,::2] = 1
# (even_rows, odd_columns)
x[::2,1::2] = 1
print(x)

答案 7 :(得分:2)

import numpy as np

a=np.array(([1,0]*4+[0,1]*4)*4).reshape((8,8))
print(a)


[[1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]]

答案 8 :(得分:2)

def checkerboard(shape):
    return np.indices(shape).sum(axis=0) % 2

最紧凑的解决方案,可能是最快的解决方案,也是唯一发布到n维的解决方案。

答案 9 :(得分:1)

perfplot 分析表明最佳(最快、最易读、内存效率最高)的解决方案是通过切片,

def slicing(n):
    A = np.zeros((n, n), dtype=int)
    A[1::2, ::2] = 1
    A[::2, 1::2] = 1
    return A

堆叠解决方案在处理大矩阵时速度要快一些,但可以说可读性较差。得票最多的答案也是最慢

enter image description here

重现情节的代码:

import numpy as np
import perfplot


def indices(n):
    return np.indices((n, n)).sum(axis=0) % 2


def slicing(n):
    A = np.zeros((n, n), dtype=int)
    A[1::2, ::2] = 1
    A[::2, 1::2] = 1
    return A


def tile(n):
    return np.tile([[0, 1], [1, 0]], (n // 2, n // 2))


def stacking(n):
    row0 = np.array(n // 2 * [0, 1] + (n % 2) * [0])
    row1 = row0 ^ 1
    return np.array(n // 2 * [row0, row1] + (n % 2) * [row0])


def ogrid(n):
    coords = np.ogrid[0:n, 0:n]
    return (coords[0] + coords[1]) % 2


b = perfplot.bench(
    setup=lambda n: n,
    kernels=[slicing, indices, tile, stacking, ogrid],
    n_range=[2 ** k for k in range(14)],
    xlabel="n",
)
b.save("out.png")
b.show()

答案 10 :(得分:1)

import numpy as np
n = int(input())
arr = ([0, 1], [1,0])
print(np.tile(arr, (n//2,n//2)))

对于输入6,输出:

   [[0 1 0 1 0 1]
    [1 0 1 0 1 0]
    [0 1 0 1 0 1]
    [1 0 1 0 1 0]
    [0 1 0 1 0 1]
    [1 0 1 0 1 0]]

答案 11 :(得分:1)

非常非常,但是我需要一种解决方案,允许在任意大小的棋盘上使用非单位的棋盘大小。这是一个简单快捷的解决方案:

import numpy as np

def checkerboard(shape, dw):
    """Create checkerboard pattern, each square having width ``dw``.

    Returns a numpy boolean array.
    """
    # Create individual block
    block = np.zeros((dw * 2, dw * 2), dtype=bool)
    block[dw:, :dw] = 1
    block[:dw, dw:] = 1

    # Tile until we exceed the size of the mask, then trim
    repeat = (np.array(shape) + dw * 2) // np.array(block.shape)
    trim = tuple(slice(None, s) for s in shape)
    checkers = np.tile(block, repeat)[trim]

    assert checkers.shape == shape
    return checkers

要将棋盘格正方形转换为颜色,您可以执行以下操作:

checkers = checkerboard(shape, dw)
img = np.empty_like(checkers, dtype=np.uint8)
img[checkers] = 0xAA
img[~checkers] = 0x99

答案 12 :(得分:1)

我修改了hass的答案如下。

import math
import numpy as np

def checkerboard(w, h, c0, c1, blocksize):
        tile = np.array([[c0,c1],[c1,c0]]).repeat(blocksize, axis=0).repeat(blocksize, axis=1)
        grid = np.tile(tile,(int(math.ceil((h+0.0)/(2*blocksize))),int(math.ceil((w+0.0)/(2*blocksize)))))
        return grid[:h,:w]

答案 13 :(得分:0)

n = int(input())
import numpy as np
m=int(n/2)
a=np.array(([0,1]*m+[1,0]*m)*m).reshape((n,n))

print (a)

因此,如果输入为n = 4,那么输出将类似于:

[[0 1 0 1]
 [1 0 1 0]
 [0 1 0 1]
 [1 0 1 0]]

答案 14 :(得分:0)

使用图块功能:

import numpy as np
n = int(input())
x = np.tile(arr,(n,n//2))
x[1::2, 0::2] = 1
x[0::2, 1::2] = 1
print(x)

答案 15 :(得分:0)

这是一个小巧的解决方案,需要进行一些检查,以确保宽度和高度可以被正方形大小均分。

def make_checkerboard(w, h, sq, fore_color, back_color):
    """
    Creates a checkerboard pattern image
    :param w: The width of the image desired
    :param h: The height of the image desired
    :param sq: The size of the square for the checker pattern
    :param fore_color: The foreground color
    :param back_color: The background color
    :return:
    """
    w_rem = np.mod(w, sq)
    h_rem = np.mod(w, sq)
    if w_rem != 0 or h_rem != 0:
        raise ValueError('Width or height is not evenly divisible by square '
                         'size.')
    img = np.zeros((h, w, 3), dtype='uint8')
    x_divs = w // sq
    y_divs = h // sq
    fore_tile = np.ones((sq, sq, 3), dtype='uint8')
    fore_tile *= np.array([[fore_color]], dtype='uint8')
    back_tile = np.ones((sq, sq, 3), dtype='uint8')
    back_tile *= np.array([[back_color]], dtype='uint8')
    for y in np.arange(y_divs):
        if np.mod(y, 2):
            b = back_tile
            f = fore_tile
        else:
            b = fore_tile
            f = back_tile
        for x in np.arange(x_divs):
            if np.mod(x, 2) == 0:
                img[y * sq:y * sq + sq, x * sq:x * sq + sq] = f
            else:
                img[y * sq:y * sq + sq, x * sq:x * sq + sq] = b
    return img

答案 16 :(得分:0)

Numpy的Tile函数获取大小为n * n的棋盘阵列

import numpy

n = 4

list_0_1 = [ [ 0, 1], [ 1, 0] ]

checkerboard = numpy.tile( list_0_1, ( n//2, n//2) ) 

print( checkerboard)

[[0 1 0 1]
 [1 0 1 0]
 [0 1 0 1]
 [1 0 1 0]]

答案 17 :(得分:0)

使用tile()编写棋盘格矩阵的最简单方法

array = np.tile([0,1],n//2)
array1 = np.tile([1,0],n//2)
finalArray = np.array([array, array1], np.int32)
finalArray = np.tile(finalArray,(n//2,1))

答案 18 :(得分:0)

假设我们需要一个长度和宽度(偶数)为l,b的模式。

base_matrix = np.array([[0,1],[1,0]])

由于此基本矩阵(将用作图块)的长度和宽度为2 X 2,因此需要除以2。

print np.tile(base_matrix, (l / 2, b / 2))

print (np.tile(base,(4/2,6/2)))
[[0 1 0 1 0 1]
 [1 0 1 0 1 0]
 [0 1 0 1 0 1]
 [1 0 1 0 1 0]]

答案 19 :(得分:0)

n = int(input())
import numpy as np
a = np.array([0])
x = np.tile(a,(n,n))
x[1::2, ::2] = 1
x[::2, 1::2] = 1
print(x)

我想使用numpy.tile()函数可以很好地完成此工作。

答案 20 :(得分:0)

最简单的实现。

import numpy as np

n = int(input())
checkerboard = np.tile(np.array([[0,1],[1,0]]), (n//2, n//2))
print(checkerboard)

答案 21 :(得分:0)

这是在numpy中使用tile函数的解决方案。

import numpy as np

x = np.array([[0, 1], [1, 0]])
check = np.tile(x, (n//2, n//2))
# Print the created matrix
print(check)
  1. 对于输入2,输出为
     [[0 1]
     [1 0]]
  1. 对于输入4,输出为
     [[0 1 0 1] 
     [1 0 1 0]
     [0 1 0 1]
     [1 0 1 0]]

答案 22 :(得分:0)

对于那些想要任意大小的正方形/矩形的人:

import numpy as np
# if you want X squares per axis, do squaresize=[i//X for i in boardsize]
def checkerboard(boardsize, squaresize):
    return np.fromfunction(lambda i, j: (i//squaresize[0])%2 != (j//squaresize[1])%2, boardsize).astype(int)

print(checkerboard((10,15), (2,3)))
[[0 0 0 1 1 1 0 0 0 1 1 1 0 0 0]
 [0 0 0 1 1 1 0 0 0 1 1 1 0 0 0]
 [1 1 1 0 0 0 1 1 1 0 0 0 1 1 1]
 [1 1 1 0 0 0 1 1 1 0 0 0 1 1 1]
 [0 0 0 1 1 1 0 0 0 1 1 1 0 0 0]
 [0 0 0 1 1 1 0 0 0 1 1 1 0 0 0]
 [1 1 1 0 0 0 1 1 1 0 0 0 1 1 1]
 [1 1 1 0 0 0 1 1 1 0 0 0 1 1 1]
 [0 0 0 1 1 1 0 0 0 1 1 1 0 0 0]
 [0 0 0 1 1 1 0 0 0 1 1 1 0 0 0]]

答案 23 :(得分:0)

将numpy导入为np
b = np.array([[0,1],[1,0]])

将n替换为偶数,您将得到答案。容易吧?

np.tile(b,(n,n))

答案 24 :(得分:0)

基于 Eelco Hoogendoorn 的 answer,如果您想要具有各种瓷砖尺寸的棋盘格,您可以使用它:

def checkerboard(shape, tile_size):
    return (np.indices(shape) // tile_size).sum(axis=0) % 2

答案 25 :(得分:0)

我最近想要相同的功能,我修改了doug的答案,如下所示:

def gen_checkerboard(grid_num, grid_size):
    row_even = grid_num/2 * [0,1]
    row_odd = grid_num/2 * [1,0]
    checkerboard = numpy.row_stack(grid_num/2*(row_even, row_odd))
    return checkerboard.repeat(grid_size, axis = 0).repeat(grid_size, axis = 1)

答案 26 :(得分:-1)

n = int(input())
oput=np.tile(0,(n,n))
oput[::2, 1::2] = 1
oput[1::2, ::2] = 1
print(oput)