Skip to content

Instantly share code, notes, and snippets.

@nyarurato

nyarurato/rdmpic.py

Last active Aug 29, 2015
Embed
What would you like to do?
# coding:utf-8
from PIL import Image,ImageDraw
from sys import argv,stdout,version_info
from random import randint
def main():
if(len(argv) == 2):
make_pic(argv[1])
else:
print(u"パラメータが不適切です")
exit()
flag = checker("続けますか?")
while (flag):
make_pic(argv[1])
flag = checker("続けますか?")
def make_pic(fname):
org_img = Image.open(fname)
size = org_img.size
print("pictrue size : " + str(size))
color = (255,255,255) #背景色
image = Image.new("RGB",size,color)
draw = ImageDraw.Draw(image)
scale = 3 #繰り返し回数の倍数
loop_count = int(size[0] * size[1] * scale)
max_length = 20 #四角の辺の最大長さ
for i in range(loop_count):
pixel = (randint(0,size[0]-1),randint(0,size[1]-1)) #四角の生成位置をランダムに選ぶ
pixcolor = org_img.getpixel(pixel) #色取得
rectsize = (randint(1,max_length),randint(1,max_length)) #四角形の大きさをランダムに決める
leftup=(pixel[0] - rectsize[0]/2,pixel[1] -rectsize[1]/2)
rightdown=(pixel[0] + rectsize[0]/2,pixel[1] + rectsize[1]/2)
outline_color = (randint(0,255),randint(0,255),randint(0,255)) #四角の枠線の色をランダムに決める
draw.rectangle((leftup,rightdown),outline= outline_color,fill= pixcolor)
show_status(i,loop_count) #コンソールに表示
image.show()
if(checker("\n保存しますか?")):
savename = "test.png" #保存時の名前
image.save(savename)
def show_status(now,end):
status = float(now+1) / end * 100
bar = "="*int(status/10) + " "*(10-int(status/10))
s = "\r status:|" + bar + "|" + "%d" %(status) + " (%d/%d)"%(now,end)
stdout.write(s)
stdout.flush()
def checker(question):
print(question + "y or n")
if(version_info[0] == 2):
yn = raw_input()
else:
yn = input()
if(yn == "y"):
return True
else:
return False
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.