本文主要是介绍颜色找茬-0-255随机底色,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码展示
'''
颜色找茬:找出不同颜色的色块
AI对话大师,阿夏
2025年6月7日
'''
from PIL import Image, ImageDraw
import randompath = 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+ag1=g+ab1=b+afill_color2=(r1, g1, b1)print(fill_color2)# 定义画布尺寸和边距canvas_width = 800canvas_height = 800margin = 50spacing = 20g = 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) * jy = margin + (cell_size + spacing) * iif 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 ospath = 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 + ag1 = g + ab1 = b + afill_color2 = (r1, g1, b1)print(fill_color2)# 定义画布尺寸和边距canvas_width = 800canvas_height = 800margin = 50spacing = 20g = 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) * jy = margin + (cell_size + spacing) * iif 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 ospath = 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 + ag1 = g + ab1 = b + afill_color2 = (r1, g1, b1)print(fill_color2)# 定义画布尺寸和边距canvas_width = 800canvas_height = 800margin = 50spacing = 20g = 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) * jy = margin + (cell_size + spacing) * iif 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随机颜色只有红、紫色、蓝、黑,
这篇关于颜色找茬-0-255随机底色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!