2017年3月2日木曜日

ランダムに円を描画する【Python】


画像処理の基礎になりそうな図ができました。

# -*- coding: utf-8 -*-
import numpy as np
import random
import scipy as sp
sizex = 1000
sizey = 1000
r = 100
en = np.zeros([sizex,sizey])
for m in range(sizex):
for n in range(sizey):
seeds = random.randint(1,50000)
if seeds == 1:
en += np.fromfunction(lambda x,y: (x-m)**2+(y-n)**2<r**2, (sizex,sizey))
sp.misc.imsave("testing.png",en)
view raw 03023 hosted with ❤ by GitHub


sizex = 1000
sizey = 1000
#画像サイズ

r = 100
#円の半径

en = np.zeros([sizex,sizey])
#np.arreyを0で埋め尽くす

for m in range(sizex):
    for n in range(sizey):
        seeds = random.randint(1,50000)
#1pixelに対して50000通りの種を作る

        if seeds == 1:
#種が1であれば(確率は1/50000,総pixel数は1000^2=1000000)

        en += np.fromfunction(lambda x,y: (x-m)**2+(y-n)**2<r**2, 
(sizex,sizey))
#半径rに応じて行列をつくる

sp.misc.imsave("testing.png",en)
#np.arrayをpngファイルとして保存


画像作ったり編集するのも楽しいですね







0 件のコメント:

コメントを投稿