iT邦幫忙

0

設計python程式 產生西洋棋盤的灰階影像,影像大小為512*512,灰階值僅包含0或255,稱為二值影像,並儲存數位影像檔案 怎麼做???

```python
import numpy as np
import matplotlib.pyplot as plt

board = np.zeros((8,8))

board[1::2, 0::2] = 1
board[0::2, 1::2] = 1

plt.imshow(board, cmap="binary")
```
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
小魚
iT邦大師 1 級 ‧ 2021-04-17 00:10:20

直接外包比較快.

1
waazxc77548
iT邦新手 4 級 ‧ 2021-04-17 01:15:36

去google啊

4
Maso的萬事屋
iT邦新手 4 級 ‧ 2021-04-20 09:49:13
from cv2 import cv2 as cv 
import numpy as np 

chess_size = 512
unit = int(chess_size / 8)
chess_shape = (chess_size,chess_size)

chess = np.ones((chess_shape),dtype=np.uint8)
print(chess.shape)

for i in range(8):
    for j in range(8):
        #判斷第幾行第一格為黑或白
        if i % 2 == 0:
            #判斷是否為第一格
            if j % 2 == 0:
                chess[i*unit:(i+1)*unit,j*unit:(j+1)*unit] = 0
            elif j % 2 == 1:
                chess[i*unit:(i+1)*unit,j*unit:(j+1)*unit] = 255
        elif i % 2 == 1:
            #判斷是否為第一格
            if j % 2 == 0:
                chess[i*unit:(i+1)*unit,j*unit:(j+1)*unit] = 255
            elif j % 2 == 1:
                chess[i*unit:(i+1)*unit,j*unit:(j+1)*unit] = 0

#cv.imshow('chess',chess)
#cv.waitKey()
#cv.destroyAllWindows()
cv.imwrite('chess.png',chess)

https://ithelp.ithome.com.tw/upload/images/20210420/20121176SnQxIcWanp.png

我要發表回答

立即登入回答