Image python图像显示
我想在第6行和第6列中填充不同的颜色Image python图像显示,image,Image,我想在第6行和第6列中填充不同的颜色 from PIL import Image, ImageDraw import math IMG_HEIGHT pixels IMG_WIDTH = 1440 IMG_HEIGHT = 1280 img = Image.new("RGB", (IMG_WIDTH, IMG_HEIGHT)) draw = ImageDraw.Draw(img) REC_START_X = 20 #Start (x and y)
from PIL import Image, ImageDraw
import math
IMG_HEIGHT pixels
IMG_WIDTH = 1440
IMG_HEIGHT = 1280
img = Image.new("RGB", (IMG_WIDTH, IMG_HEIGHT))
draw = ImageDraw.Draw(img)
REC_START_X = 20 #Start (x and y) needs to be the same
REC_START_Y = 20
REC_STOP_X = 1020 #Stop (x and y) needs to be the same
REC_STOP_Y = 710
#Calculates the number of circles in one column and calculates the diameter.
circleDiameter = (REC_STOP_X - REC_START_X)/NUMBER_OF_CIRCLES_IN_ONE_ROW
circlesInOneColumn = int((REC_STOP_Y-REC_START_Y))
print("circlesInOneRow: ", NUMBER_OF_CIRCLES_IN_ONE_ROW, " circlesInOneColumn: ", circlesInOneColumn, " circleDiameter: ", circleDiameter)
#Draw the circles from left to right and then starting on the next row
img.show()
代码正在运行,但我无法在行和列颜色的6个圆圈后放置不同的颜色。此解决方案是用opencv制作的,也许它可以提供一个提示 首先定义随机颜色的辅助对象
from random import randrange
def rand_color():
return (randrange(256), randrange(256), randrange(256))
然后预设:
radius = 10
num_circles_per_side = 6
num_cols = 4
num_rows = 3
有用的变量和图像初始化:
side = num_circles_per_side * radius * 2
w = num_cols * side
h = num_rows * side
patch_side = num_circles_per_side * radius * 2
image = np.zeros([h, w, 3], np.uint8)
patch = np.zeros([patch_side, patch_side], np.uint8)
循环:
# draws white circles on the patch
for y in range(num_circles_per_side):
for x in range(num_circles_per_side):
cv2.circle(patch, (x * radius * 2 + radius, y * radius * 2 + radius), radius, 255, cv2.FILLED)
# uses the patch as mask for the square slices
for y in range(num_rows):
for x in range(num_cols):
slice = np.s_[y * side:y * side + side, x * side:x * side + side]
image[slice][patch == 255] = rand_color()
输出图像
:
圆圈忽略了边框,但可以通过修改补丁轻松添加边框。您标记的,对于非随机颜色,使用数据填充列表,对于cv2。圆圈
请参见,对于np.s
请参见