我写了一个自己会玩秘密花园的程序

Everyone listen to me! please call me ZhenHuiwan after today.

我拿Python写了一个小程序,可以自己玩《秘密花园》。

前言:
写这个小玩具的时候,也遇到了许多困难,我意识到这是我打倒敌人的决心不够造成的,于是我对自己进行了思想教育,拿出《毛泽东选集》认真翻看,当我读到“改造我们的学习”这一章节时我突然意识到,只要坚信毛泽东思想,问题就一定能解决,果然,问题很快就解决了,感谢毛爷爷的指导,共产党万岁!
mao.jpg

这张图是我拿来测试用的,线条用PS随手画了两道,但是填充之后也很漂亮。
原图:
my.jpg

填充之后:
new.jpg

关于我是怎样确定每块颜色的这个问题,我想到了教我艺术色彩课的王某某老师讲的“要渐变,要有过度,要有明暗对比”,于是,我毅然决然的…………用了随机生成。。说人话就是胡乱填

填充完成的秘密花园:
1.jpg

new (1).jpg

new_38.jpg

new_64.jpg

new_33.jpg

new_14 - Copy (2).jpg

new_10.jpg

Python 代码如下:
最新版代码请到github查看PythonFillPicture
需要PIL第三方库

from PIL import Image
import random

fileName = "33.jpg"

img = Image.open(fileName)
imgArray = img.load()

width = img.size[0]
height = img.size[1]

flag = [[0 for i in range(height)] for j in range(width)]

dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

def fill(x, y, thisColor, colorRgb):
        #BFS fill color block
        head = 0
        tail = 1

        wayx = []
        wayy = []

        wayx.append(x)
        wayy.append(y)

        while (head < tail):
                for i in range(0, 4):
                        tempx = wayx[head] + dx[i]
                        tempy = wayy[head] + dy[i]
                        
                        #if  (tempx >= 0) and (tempy >= 0) and (tempx < width) and (tempy < height):
                        if (imgArray[tempx, tempy] != (0, 0, 0)) and (flag[tempx][tempy] == 0):
                                imgArray[tempx, tempy] = colorRgb
                                flag[tempx][tempy] = thisColor
                                wayx.append(tempx)
                                wayy.append(tempy)
                                tail += 1
                head += 1
                
        return thisColor + 1
        
color = 1

for i in range(0, width):
        imgArray[i, 0] = (0,0,0)
        imgArray[i, height-1] = (0,0,0)
for j in range(0, height):
        imgArray[0, j] = (0,0,0)
        imgArray[width-1, j] = (0,0,0)
        
for i in range(1, width-1):
        for j in range(1, height-1):
                if (imgArray[i, j] == (255,255,255)) and (flag[i][j] == 0):
                        colorRgb = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
                        color = fill(i, j, color, colorRgb)
                        print "color", color

img.save('new_' + fileName)

print 'Complete!'

Tag: none

3 comments

  1. CIN CIN

    还行啊,随机颜色看着很colorful啊

  2. 筱爷 筱爷

    你真的是学艺术的吗( ̄▽ ̄)

Leave a new comment