代码展示
'''
颜色找茬:找出不同颜色的色块
AI对话大师,阿夏
2025年6月7日
'''
from PIL import Image, ImageDraw
import random
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\辨认色彩图'
for xx in range(10000):
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
fill_color = (r, g, b)
print(fill_color)
a=random.randint(0,100)
r1=r+a
g1=g+a
b1=b+a
fill_color2=(r1, g1, b1)
print(fill_color2)
# 定义画布尺寸和边距
canvas_width = 800
canvas_height = 800
margin = 50
spacing = 20
g = 5
# 计算单元格的大小和间距
cell_size = int((canvas_width - 2 * margin - (g - 1) * spacing) / g)
# 创建画布
canvas = Image.new('RGB', (canvas_width, canvas_height), 'white')
draw = ImageDraw.Draw(canvas)
# 随机选择一个单元格填充随机颜色
random_row = random.randint(0, g - 1)
random_col = random.randint(0, g - 1)
# 绘制单元格
for i in range(g):
for j in range(g):
x = margin + (cell_size + spacing) * j
y = margin + (cell_size + spacing) * i
if i == random_row and j == random_col:
fill_color = (r1, g1, b1)
else:
fill_color = (r, g, b)
draw.rectangle((x, y, x + cell_size, y + cell_size), fill=fill_color)
# 保存图片
canvas.save(path + fr'\{xx:05d}.png')
手动删除一些不是相近色的图案,随机生成底色
保留色相相近的图案
分类成两类
低难度的色块颜色对比强烈
高难度的色块对比不明显
但是我感觉我自己都看不清不同的色块在哪里?所以我需要做一个有答案的版本。
from PIL import Image, ImageDraw
import random
import os
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\辨认色彩图'
t = ['01题目卡', '02答案卡']
tm = []
for tt in t:
os.makedirs(path + fr'\{tt}', exist_ok=True)
tm.append(path + fr'\{tt}')
print(tm)
for xx in range(500):
# 不要(0,0,0)黑色,不要255,255,255白色
r = random.randint(1, 255)
g = random.randint(1, 255)
b = random.randint(1, 255)
fill_color = (r, g, b)
print(fill_color)
a = random.randint(0, 255)
r1 = r + a
g1 = g + a
b1 = b + a
fill_color2 = (r1, g1, b1)
print(fill_color2)
# 定义画布尺寸和边距
canvas_width = 800
canvas_height = 800
margin = 50
spacing = 20
g = 5
# 计算单元格的大小和间距
cell_size = int((canvas_width - 2 * margin - (g - 1) * spacing) / g)
# 创建画布
canvas1 = Image.new('RGB', (canvas_width, canvas_height), 'white')
draw1 = ImageDraw.Draw(canvas1)
canvas2 = Image.new('RGB', (canvas_width, canvas_height), 'white')
draw2 = ImageDraw.Draw(canvas2)
# 随机选择一个单元格填充随机颜色
random_row = random.randint(0, g - 1)
random_col = random.randint(0, g - 1)
# 绘制单元格
for i in range(g):
for j in range(g):
x = margin + (cell_size + spacing) * j
y = margin + (cell_size + spacing) * i
if i == random_row and j == random_col:
fill_color = (r1, g1, b1)
outline_color = 'black' # 添加黑色边框
else:
fill_color = (r, g, b)
outline_color = None # 不添加边框
draw1.rectangle((x, y, x + cell_size, y + cell_size), fill=fill_color)
draw2.rectangle((x, y, x + cell_size, y + cell_size), fill=fill_color, outline=outline_color, width=20)
# 保存图片
canvas1.save(tm[0] + fr'\{xx:05d}-01图卡.png')
canvas2.save(tm[0] + fr'\{xx:05d}-02答案.png')
黑色底色的就看不见了
改成白色边框
from PIL import Image, ImageDraw
import random
import os
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\辨认色彩图'
t = ['01题目卡', '02答案卡']
tm = []
for tt in t:
os.makedirs(path + fr'\{tt}', exist_ok=True)
tm.append(path + fr'\{tt}')
print(tm)
for xx in range(500):
# 不要(0,0,0)黑色,不要255,255,255白色
r = random.randint(1, 255)
g = random.randint(1, 255)
b = random.randint(1, 255)
fill_color = (r, g, b)
print(fill_color)
a = random.randint(0, 255)
r1 = r + a
g1 = g + a
b1 = b + a
fill_color2 = (r1, g1, b1)
print(fill_color2)
# 定义画布尺寸和边距
canvas_width = 800
canvas_height = 800
margin = 50
spacing = 20
g = 5
# 计算单元格的大小和间距
cell_size = int((canvas_width - 2 * margin - (g - 1) * spacing) / g)
# 创建画布
canvas1 = Image.new('RGB', (canvas_width, canvas_height), 'white')
draw1 = ImageDraw.Draw(canvas1)
canvas2 = Image.new('RGB', (canvas_width, canvas_height), 'white')
draw2 = ImageDraw.Draw(canvas2)
# 随机选择一个单元格填充随机颜色
random_row = random.randint(0, g - 1)
random_col = random.randint(0, g - 1)
# 绘制单元格
for i in range(g):
for j in range(g):
x = margin + (cell_size + spacing) * j
y = margin + (cell_size + spacing) * i
if i == random_row and j == random_col:
fill_color = (r1, g1, b1)
outline_color = 'white' # 添加黑色边框
else:
fill_color = (r, g, b)
outline_color = None # 不添加边框
draw1.rectangle((x, y, x + cell_size, y + cell_size), fill=fill_color)
draw2.rectangle((x, y, x + cell_size, y + cell_size), fill=fill_color, outline=outline_color, width=10)
# 保存图片
canvas1.save(tm[0] + fr'\{xx:05d}_01图卡.png')
canvas2.save(tm[0] + fr'\{xx:05d}_02答案.png')
同样手动不是相近色的
为了生成足够多的,我先生成10000张
生成了20分钟,一共2万张。
1-255随机颜色只有红、紫色、蓝、黑,
标签:canvas,color,random,底色,cell,找茬,255,fill From: https://blog.csdn.net/reasonsummer/article/details/139522756