""" 像素点随机移动 一个点在一个明面上任意上下左右四个方向移动,留下一个淡淡的颜色,最终n步后生成一张图 2020年3月14日创建 by littlefean """ from PIL import Image from random import randint import time xSize = 500 ySize = 500 x = int(xSize/2) y = int(xSize/2) im = Image.new('RGBA', (xSize, ySize), 'black') im.getpixel((x, y)) step = 5000
defcolor(): """在所在像素下点涂颜色""" global im, x, y add_color = 255 src_str_list = im.load() data = src_str_list[x, y] data_list = list(data) print(data) if data_list[0] < 255: data_list[0] += add_color data_list[1] += add_color data_list[2] += add_color data = tuple(data_list) im.putpixel((x, y), data)
defjudge(): """判断是否已经走到边界上""" global x, y, xSize, ySize b = int(xSize/2) if x >= xSize-1: x -= b if y >= ySize-1: y -= b if x <= 1: x += b if y <= 0: y += b
defdraw(): """根据move返回值来移动图片上的像素点""" global x, y, im xy = move() if xy == 1: y -= 1 color() elif xy == 2: x += 1 color() elif xy == 3: y += 1 color() elif xy == 4: x -= 1 color() elif xy == 5: x += 1 y -= 1 color() elif xy == 6: x += 1 y += 1 color() elif xy == 7: x -= 1 y += 1 color() elif xy == 8: x -= 1 y -= 1 color()
whileTrue: t1 = time.time() for i inrange(step): move() draw() judge() im.save(f'RandomMove2D_{i}.png') t2 = time.time() input(f"end by {step} step/ncost {t2 - t1} second")