作品展示
背景需求:
2年前制作了蒙德里安随机线条
【教学类-18-02】20221124《蒙德里安“红黄蓝黑格子画”-A4竖版》(大班)_蒙德里安模版-CSDN博客文章浏览阅读1k次。【教学类-18-02】20221124《蒙德里安“红黄蓝黑格子画”-A4竖版》(大班)_蒙德里安模版https://blog.csdn.net/reasonsummer/article/details/128016371
虽然孩子们都对矩形格子涂色了,但都是N*N格子的格子涂色,
不像蒙德里安原作有“大色块小色块”的大小对比感觉(跨越两行),
这几天要上随堂课,我想就用“蒙德里安”做一个欣赏活动,全体孩子只要涂色就可以了。
一、彩色蒙德里安
测试方式:
1、一开始我尝试生成随机线条,遇到其他线条就停止,但是最终还是N*N格子的样式,填充时,整个画布都变成一种颜色。
2、后来,我准备在画布上随机生成12个黑框矩形(填充色3红3黄3蓝3黑),然后将每个矩形的左上角、右上角、左下角、右下角的位置延伸出两条黑线到画布边框(如果中途遇到黑色,就停止生成线条)
这么复杂的需求,多亏了AI对话大师,才能生成!!!
(矩形四个坐标的黑色线条延伸,问了三十几次才获得。)
代码展示:
'''
目的:蒙德里安(大小不规律格子)三原色
作者:AI对话大师
时间:2024年5月8日
'''
from PIL import Image, ImageDraw
import random
for xx in range(40):
# 创建一个新的空白图片
width, height = 2900, 2100
min_rect_size = 100
image = Image.new('RGB', (width, height), color=(255, 255, 255))
draw = ImageDraw.Draw(image)
# 已放置的矩形列表
placed_rects = []
# 尝试放置多个不重叠的矩形
num_rects = 12 # 尝试放置的矩形数量
# 控制3个红、3个黄、3个蓝、3个黑
colors = [(255, 0, 0), (255, 0, 0), (255, 0, 0), # 红色 (3次)
(255, 255, 0), (255, 255, 0), (255, 255, 0), # 黄色 (3次)
(0, 0, 255), (0, 0, 255), (0, 0, 255), # 蓝色 (3次)
(0, 0, 0), (0, 0, 0), (0, 0, 0)] # 黑色 (3次)
# colors = [(255, 0, 0), (255, 255, 0), (0, 0, 255), (0, 0, 0),(255, 255, 255)] # 颜色列表
# 左上角向左 左下角向左
def extend_line_to_boundary_left(start_x, start_y, direction_x, stop_color):
x = start_x
y = start_y
while 0 <= x <= width and image.getpixel((x, y)) != stop_color:
x += direction_x
return x
# 右上角向右 右下角向右
def extend_line_to_boundary_right(start_x, start_y, direction_x, stop_color, width, image):
x = start_x
y = start_y
while 0 <= x < width and image.getpixel((x, y)) != stop_color:
x += direction_x
return x
# 左上角向上 右上角向上
def extend_line_to_boundary_up(start_x, start_y, direction_y, stop_color, height, draw):
x = start_x
y = start_y
while 0 <= y < height:
try:
if draw.getpixel((x, y)) == stop_color:
break
except IndexError:
break
y += direction_y
return y
# 左下角向下 右下角向下
def extend_line_to_boundary_down(start_x, start_y, direction_y, stop_color, height, draw):
x = start_x
y = start_y
while 0 <= y < height:
try:
if draw.getpixel((x, y)) == stop_color:
break
except IndexError:
break
y += direction_y
return y
for _ in range(num_rects):
success = False
while not success:
# 随机生成矩形位置和大小
left = random.randint(0, width - min_rect_size)
top = random.randint(0, height - min_rect_size)
right = left + random.randint(min_rect_size, width - left)
bottom = top + random.randint(min_rect_size, height - top)
# 检查新矩形是否与已放置的矩形重叠
for rect in placed_rects:
if left < rect[2] and right > rect[0] and top < rect[3] and bottom > rect[1]:
# 如果重叠,则重新生成新的矩形位置和大小
break
else:
# 如果没有重叠,则绘制矩形并添加到已放置的矩形列表
# color = random.choice(colors)
color =colors[_]
outline_color = (0, 0, 0) # 外框线颜色为黑色
outline_width = 30 # 外框线宽度
draw.rectangle([(left, top), (right, bottom)], fill=color, outline=(0, 0, 0),width=outline_width)
placed_rects.append((left, top, right, bottom))
success = True
# 延长矩形边界至画布边框
# 延长矩形边界至画布边框
# draw.line([(left, top), (0, top)], fill=outline_color, width=outline_width)
# draw.line([(right, top), (width, top)], fill=outline_color, width=outline_width)
# draw.line([(right, bottom), (width, bottom)], fill=outline_color, width=outline_width)
# draw.line([(left, bottom), (0, bottom)], fill=outline_color, width=outline_width)
# draw.line([(left, top), (left, 0)], fill=outline_color, width=outline_width)
# draw.line([(right, top), (right, 0)], fill=outline_color, width=outline_width)
# draw.line([(right, bottom), (right, height)], fill=outline_color, width=outline_width)
# draw.line([(left, bottom), (left, height)], fill=outline_color, width=outline_width)
# 检测矩形右上角的坐标(right+1、top+15),向右侧水平延伸画一根黑色线条
# 检测矩形左下角的坐标(left-1、bottom-15),向下垂直延伸画一根黑色线条
y = extend_line_to_boundary_down(left - 1, bottom - 15, 1, outline_color, height, image)
draw.line([(left + 15, bottom - 15), (left + 15, y)], fill=outline_color, width=outline_width)
# # 检测矩形左上角的坐标(left-1、top+15),向上垂直延伸画一根黑色线条
y = extend_line_to_boundary_up(left-1, top+15, -1, outline_color, height, image)
draw.line([(left +15, top + 15), (left +15, y)], fill=outline_color, width=outline_width)
# 检测矩形左上角的坐标(left-1、top+15),向左侧水平延伸画一根黑色线条
x = extend_line_to_boundary_left(left -1, top + 15, -1, outline_color)
draw.line([(left - 1, top + 15), (x, top + 15)], fill=outline_color, width=outline_width)
# 检测矩形右上角的坐标(right+1、top+15),向上垂直延伸画一根黑色线条
y = extend_line_to_boundary_up(right + 1, top + 15, -1, outline_color, height, image)
draw.line([(right -15, top + 15), (right -15, y)], fill=outline_color, width=outline_width)
# 检测矩形左下角的坐标(left-1、top+15),向左侧水平延伸画一根黑色线条
x = extend_line_to_boundary_left(left -1, bottom - 15, -1, outline_color)
draw.line([(left - 1, bottom - 15), (x, bottom - 15)], fill=outline_color, width=outline_width)
# 检测矩形右上角的坐标(right+1、top+15),向右侧水平延伸画一根黑色线条
# # 检测矩形左下角的坐标(left-1、bottom-15),向下垂直延伸画一根黑色线条
# y = extend_line_to_boundary_down(left - 1, bottom - 15, 1, outline_color, height, image)
# draw.line([(left + 15, bottom - 15), (left + 15, y)], fill=outline_color, width=outline_width)
x = extend_line_to_boundary_right(right + 1, top + 15, 1, outline_color, width, image)
draw.line([(right + 1, top + 15), (x, top + 15)], fill=outline_color, width=outline_width)
# 检测矩形右下角的坐标(right+1、bottom-15),向下垂直延伸画一根黑色线条
y = extend_line_to_boundary_down(right + 1, bottom - 15, 1, outline_color, height, image)
draw.line([(right - 15, bottom - 15), (right - 15, y)], fill=outline_color, width=outline_width)
# 检测矩形右下角的坐标(right+1、top+15),向右侧水平延伸画一根黑色线条
x = extend_line_to_boundary_right(right + 1, bottom - 15, 1, outline_color, width, image)
draw.line([(right + 1, bottom - 15), (x, bottom - 15)], fill=outline_color, width=outline_width)
# 显示图片(如果你使用的是图形界面环境)
# image.show()
image.save(fr'C:\Users\jg2yXRZ\OneDrive\桌面\蒙德里安\{xx}.png')
随机生成20张
重点说明:
A4横板打印,所以我让短一点的Y(上下方向线条)生成,然后再生成长一点X(左右方向线条),否则左右线条生成后,上下线条可能都不出现了(上线线条接触到左右方向的黑线就终止)
二、黑白蒙德里安
我的教学目的是“三原色+黑色的涂色”,所以就不要填充颜色了
修改内容(填充白色)
代码展示:
'''
目的:蒙德里安(大小不规律格子)白色
作者:AI对话大师
时间:2024年5月8日
'''
from PIL import Image, ImageDraw
import random
for xx in range(40):
# 创建一个新的空白图片
width, height = 2900, 2100
min_rect_size = 100
image = Image.new('RGB', (width, height), color=(255, 255, 255))
draw = ImageDraw.Draw(image)
# 已放置的矩形列表
placed_rects = []
# 尝试放置多个不重叠的矩形
num_rects = 1 # 尝试放置的矩形数量
# 控制3个红、3个黄、3个蓝、3个黑
colors = [(255, 255, 255)] # 白色
# colors = [(255, 0, 0), (255, 255, 0), (0, 0, 255), (0, 0, 0),(255, 255, 255)] # 颜色列表
# 左上角向左 左下角向左
def extend_line_to_boundary_left(start_x, start_y, direction_x, stop_color):
x = start_x
y = start_y
while 0 <= x <= width and image.getpixel((x, y)) != stop_color:
x += direction_x
return x
# 右上角向右 右下角向右
def extend_line_to_boundary_right(start_x, start_y, direction_x, stop_color, width, image):
x = start_x
y = start_y
while 0 <= x < width and image.getpixel((x, y)) != stop_color:
x += direction_x
return x
# 左上角向上 右上角向上
def extend_line_to_boundary_up(start_x, start_y, direction_y, stop_color, height, draw):
x = start_x
y = start_y
while 0 <= y < height:
try:
if draw.getpixel((x, y)) == stop_color:
break
except IndexError:
break
y += direction_y
return y
# 左下角向下 右下角向下
def extend_line_to_boundary_down(start_x, start_y, direction_y, stop_color, height, draw):
x = start_x
y = start_y
while 0 <= y < height:
try:
if draw.getpixel((x, y)) == stop_color:
break
except IndexError:
break
y += direction_y
return y
for _ in range(num_rects):
success = False
while not success:
# 随机生成矩形位置和大小
left = random.randint(0, width - min_rect_size)
top = random.randint(0, height - min_rect_size)
right = left + random.randint(min_rect_size, width - left)
bottom = top + random.randint(min_rect_size, height - top)
# 检查新矩形是否与已放置的矩形重叠
for rect in placed_rects:
if left < rect[2] and right > rect[0] and top < rect[3] and bottom > rect[1]:
# 如果重叠,则重新生成新的矩形位置和大小
break
else:
# 如果没有重叠,则绘制矩形并添加到已放置的矩形列表
color = random.choice(colors) # 随机选择颜色里的一个白色
outline_color = (0, 0, 0) # 外框线颜色为黑色
outline_width = 30 # 外框线宽度
draw.rectangle([(left, top), (right, bottom)], fill=color, outline=(0, 0, 0),width=outline_width)
placed_rects.append((left, top, right, bottom))
success = True
# 延长矩形边界至画布边框
# 延长矩形边界至画布边框
# draw.line([(left, top), (0, top)], fill=outline_color, width=outline_width)
# draw.line([(right, top), (width, top)], fill=outline_color, width=outline_width)
# draw.line([(right, bottom), (width, bottom)], fill=outline_color, width=outline_width)
# draw.line([(left, bottom), (0, bottom)], fill=outline_color, width=outline_width)
# draw.line([(left, top), (left, 0)], fill=outline_color, width=outline_width)
# draw.line([(right, top), (right, 0)], fill=outline_color, width=outline_width)
# draw.line([(right, bottom), (right, height)], fill=outline_color, width=outline_width)
# draw.line([(left, bottom), (left, height)], fill=outline_color, width=outline_width)
# 检测矩形左下角的坐标(left-1、bottom-15),向下垂直延伸画一根黑色线条
y = extend_line_to_boundary_down(left - 1, bottom - 15, 1, outline_color, height, image)
draw.line([(left + 15, bottom - 15), (left + 15, y)], fill=outline_color, width=outline_width)
# # 检测矩形左上角的坐标(left-1、top+15),向上垂直延伸画一根黑色线条
y = extend_line_to_boundary_up(left-1, top+15, -1, outline_color, height, image)
draw.line([(left +15, top + 15), (left +15, y)], fill=outline_color, width=outline_width)
# 检测矩形左上角的坐标(left-1、top+15),向左侧水平延伸画一根黑色线条
x = extend_line_to_boundary_left(left -1, top + 15, -1, outline_color)
draw.line([(left - 1, top + 15), (x, top + 15)], fill=outline_color, width=outline_width)
# 检测矩形右上角的坐标(right+1、top+15),向上垂直延伸画一根黑色线条
y = extend_line_to_boundary_up(right + 1, top + 15, -1, outline_color, height, image)
draw.line([(right -15, top + 15), (right -15, y)], fill=outline_color, width=outline_width)
# 检测矩形左下角的坐标(left-1、top+15),向左侧水平延伸画一根黑色线条
x = extend_line_to_boundary_left(left -1, bottom - 15, -1, outline_color)
draw.line([(left - 1, bottom - 15), (x, bottom - 15)], fill=outline_color, width=outline_width)
# # 检测矩形右上角的坐标(right+1、top+15),向右侧水平延伸画一根黑色线条
# # 检测矩形左下角的坐标(left-1、bottom-15),向下垂直延伸画一根黑色线条
# y = extend_line_to_boundary_down(left - 1, bottom - 15, 1, outline_color, height, image)
# draw.line([(left + 15, bottom - 15), (left + 15, y)], fill=outline_color, width=outline_width)
x = extend_line_to_boundary_right(right + 1, top + 15, 1, outline_color, width, image)
draw.line([(right + 1, top + 15), (x, top + 15)], fill=outline_color, width=outline_width)
# 检测矩形右下角的坐标(right+1、bottom-15),向下垂直延伸画一根黑色线条
y = extend_line_to_boundary_down(right + 1, bottom - 15, 1, outline_color, height, image)
draw.line([(right - 15, bottom - 15), (right - 15, y)], fill=outline_color, width=outline_width)
# 检测矩形右下角的坐标(right+1、top+15),向右侧水平延伸画一根黑色线条
x = extend_line_to_boundary_right(right + 1, bottom - 15, 1, outline_color, width, image)
draw.line([(right + 1, bottom - 15), (x, bottom - 15)], fill=outline_color, width=outline_width)
# 显示图片(如果你使用的是图形界面环境)
# image.show()
image.save(fr'C:\Users\jg2yXRZ\OneDrive\桌面\蒙德里安\{xx}.png')
稍微选一下图片,不要让孩子涂色面积过大,影响授课时间
也可以修改图形数量12个变成5个,看看小格子是不是不见了?
特别细小的小格子数量变少了。
2个矩形
1个矩形
修改,把代码放到最上面
一个矩形的8个方向线条都有了
再把彩色的蒙德里安也修改一下,确保8个线条都生成