背景需求:
視覺訓練的神奇效果,讓你的寶貝成為焦點 - 小紅書魔法視覺追蹤-視覺訓練—— 🔍視覺訓練🔍 🔹想要提高寶寶的專注力,視覺訓練是個絕佳方法! 🔹讓寶寶仔細觀察數字的路線,鍛煉他們的視覺敏感度。 🔹在空白處填寫數字,可根據線條的方向按順序填寫。 💡注意事項:引導寶寶一步一步進行,不要急于求成哦。 🔍視覺追蹤能力🔍 🔹想讓寶寶的視覺追蹤能力更強?這里有個小技巧! 🔹讓寶寶追蹤圖中數字的線條,提高他們的追蹤能力。 🔹練習后,寶寶將變得更加敏銳,追蹤能力也會有長足進步哦。 💡注意事項:適當調整難度,讓寶寶充分體驗追蹤的樂趣。 🔍視覺線性空間感🔍 🔹想要培養寶寶的視覺線性空間感?這里有個妙招! 🔹讓寶寶仔細觀察數字的線條,理解線性空間感的變化。 🔹幫助寶寶感知線條的長度、方向和位置,提升他們的空間感知能力。 💡注意事項:讓寶寶在放松愉快的狀態下進行訓練。 🎉快來試試這些訓練方法吧!讓寶寶的專注力更上一層樓!🎉 #兒童教育 #視覺訓練 #數字敏感度 #視覺追蹤能力 #線性空間感#不懂就問有問必答 #右腦開發 #注意力訓練 #提高孩子學習的專注力 #孩子注意力不集中 #早期教育 #幼兒早教 #啟蒙 #益智早教 #早教日常 #素材組 #專注力 #家庭教育 #兒童專注力訓練 #游戲日常https://www.xiaohongshu.com/discovery/item/661642dd000000001a01101d?secondshare=weixin&share_from_user_hidden=true&appuid=&apptime=1716809845
模仿以上樣式制作10*10宮格的圓圈,隨機生成一根線貫通所有的圓圈。并根據路線生成1-100的數字,隨機空缺N個。
'''
目標:圓點百數圖,線路隨機
作者:AI對話大師 阿夏
日期:2024年5月29日
# 的代碼基本上是正確的,我只進行了一些微小的調整。現在,無論S是奇數還是偶數,都可以成功生成圖像。如果S是奇數,將會生成斜線路徑,如果S是偶數,將會生成直線路徑。請注意,由于路徑的生成是隨機的,每次運行代碼,生成的路徑都可能有所不同。
'''
import random
from PIL import Image, ImageDraw, ImageFontpath = r'C:\Users\jg2yXRZ\OneDrive\桌面\圓點百數圖'
image_folder = path + r'\jpg'max_attempts = 1000 # 最大嘗試次數for xx in range(10):s = 4k = int(s * s * 50 / 100) + 1number_list = list(range(1, s * s + 1))random_numbers = random.sample(number_list, k)result = [str('' if i + 1 in random_numbers else i + 1) for i in range(s * s)]width = 2100height = 2100image = Image.new('RGB', (width, height), color=(255, 255, 255))draw = ImageDraw.Draw(image)wide = 10# 如果宮格等于7-10,就150,否則就是200if s==6:dot_size = 200if s==5:dot_size = 250if s==4:dot_size =300if s==3:dot_size =350if s==2:dot_size =350else:dot_size = 150margin = 20dot_padding = int((width - 2 * margin - dot_size * s) / s)canvas_width = width - 2 * margincanvas_height = height - 2 * marginmatrix_width = s * dot_size + (s - 1) * dot_paddingmatrix_height = s * dot_size + (s - 1) * dot_paddingstart_x = margin + (canvas_width - matrix_width) // 2start_y = margin + (canvas_height - matrix_height) // 2directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]attempts = 0 # 當前嘗試次數success = False # 是否成功生成圖像while attempts < max_attempts:start_point = (random.randint(0, s - 1), random.randint(0, s - 1))current_point = start_pointvisited_points = set([start_point])path = [start_point]def dfs():global current_pointglobal visited_pointsglobal pathif len(visited_points) == s * s:return Truefor direction in directions:neighbor_x = current_point[0] + direction[0]neighbor_y = current_point[1] + direction[1]neighbor = (neighbor_x, neighbor_y)if neighbor in visited_points:continueif 0 <= neighbor_x < s and 0 <= neighbor_y < s:visited_points.add(neighbor)path.append(neighbor)current_point = neighborif dfs():return Truevisited_points.remove(neighbor)path.pop()current_point = path[-1]return Falseif dfs():success = Truebreakattempts += 1if success:font = ImageFont.truetype(r'C:\Windows\Fonts\simsun.ttc', 100)for i in range(len(path) - 1):if i < len(result) - 1:center_x1 = start_x + path[i][1] * (dot_size + dot_padding) + dot_size // 2center_y1 = start_y + path[i][0] * (dot_size + dot_padding) + dot_size // 2center_x2 = start_x + path[i + 1][1] * (dot_size + dot_padding) + dot_size // 2center_y2 = start_y + path[i + 1][0] * (dot_size + dot_padding) + dot_size // 2draw.line([(center_x1, center_y1), (center_x2, center_y2)], fill=(0, 0, 0), width=wide)for i in range(s):for j in range(s):center_x = start_x + j * (dot_size + dot_padding) + dot_size // 2center_y = start_y + i * (dot_size + dot_padding) + dot_size // 2draw.ellipse([(center_x - dot_size // 2, center_y - dot_size // 2),(center_x + dot_size // 2, center_y + dot_size // 2),],fill=(255, 255, 255),outline=(0, 0, 0),width=wide,)# 給所有起始點添加數字1-35for i in range(len(result) - 1):center_x1 = start_x + path[i][1] * (dot_size + dot_padding) + dot_size // 2center_y1 = start_y + path[i][0] * (dot_size + dot_padding) + dot_size // 2center_x2 = start_x + path[i + 1][1] * (dot_size + dot_padding) + dot_size // 2center_y2 = start_y + path[i + 1][0] * (dot_size + dot_padding) + dot_size // 2number = result[i]text_width, text_height = draw.textsize(str(number), font=font)text_x = center_x1 - text_width // 2text_y = center_y1 - text_height // 2draw.text((text_x, text_y), str(number), font=font, fill=(0, 0, 0))# 給最后一個圓點坐標添加數字 36if len(result) == s * s:last_x = start_x + path[-1][1] * (dot_size + dot_padding) + dot_size // 2last_y = start_y + path[-1][0] * (dot_size + dot_padding) + dot_size // 2draw.ellipse([(last_x - dot_size // 2, last_y - dot_size // 2),(last_x + dot_size // 2, last_y + dot_size // 2),],fill=(255, 255, 255),outline=(0, 0, 0),width=wide,)text_width, text_height = draw.textsize(result[-1], font=font)text_x = last_x - text_width // 2text_y = last_y - text_height // 2draw.text((text_x, text_y), result[-1], font=font, fill=(0, 0, 0))image_path = image_folder + fr'\{xx:03d}.png'image.save(image_path)else:print(f'無法生成圖像:s={s}')
結果:
一、s=2-6可以按照指定數量生成。
其中雙數的線條是水平垂直,單數線條是水平垂直還有部分傾斜
10次里面6次生成失敗(出現傾斜線條),4次生成順利