2017-02-10 6 views
-1
from PIL import Image 
import time 
start = time.time() 
import random as r 
x = int(input("Enter the preferred width of your image:")) 
y = int(input("Enter the preferred length of your image:")) 
suggested = (x*y) 
print("We suggest you use:",suggested,"pixels") 
amount = int(input("How many pixels do you want to generate?")) 
while amount > suggested: 
    amount = int(input("Please choose the number suggested or smaller.  How many pixels do you want to generate?")) 
numbers = [] 
numbs = (r.randint(0,255),r.randint(0,255),r.randint(0,255)) 
while len(numbers)<(amount): 
    numbers.append(numbs) 
    numbs = (r.randint(0,255),r.randint(0,255),r.randint(0,255)) 
print(numbers) 
print('It took', time.time()-start, 'seconds') 
im2 = Image.new('RGB', (x,y)) 
im2.putdata(numbers) 
im2.show() 
im2.save("out.png") 

私が使ったコードでは、1つのタプルに3つの乱数を入れ、そのタプルをリストに入れます。各タプルはピクセルです。ユーザーが入力する寸法は、常に幅xと幅yの直線形状になります。形状を丸くし、曲線を描きたいと思います。どうやって?ありがとう。シェイプを円にするにはどうすればいいですか?

答えて

0

あなたのコードで遊んでいて、開いている緑色の円と青い色の円を追加できました。試してみてください:

from PIL import Image, ImageDraw 
import time 
import math 
# 
start = time.time() 
import random as r 
#x = int(input("Enter the preferred width of your image:")) 
x = 100 
#y = int(input("Enter the preferred length of your image:")) 
y = 150 
suggested = (x*y) 
print("We suggest you use:",suggested,"pixels") 
#amount = int(input("How many pixels do you want to generate?")) 
amount = 15000 
while amount > suggested: 
    amount = int(input("Please choose the number suggested or smaller.  How many pixels do you want to generate?")) 
numbers = [] 
numbs = (r.randint(0,255),r.randint(0,255),r.randint(0,255)) 
while len(numbers)<(amount): 
    numbers.append(numbs) 
    numbs = (r.randint(0,255),r.randint(0,255),r.randint(0,255)) 
print(numbers) 
# 
print('It took', time.time()-start, 'seconds') 
im2 = Image.new('RGB', (x,y)) 
im2.putdata(numbers) 
#----------------------------------------------------------- 
numb_test = (255,0,0) # red circle 
numb_test = (0,255,0) # green circle 
radius_test = min(x,y) * 0.4 
print "radius_test = " + str(radius_test) 
x_center = x/2 
y_center = y/2 
angle_test = 0 
print "pi = " + str(math.pi) 
while angle_test < 360: 
    angle_rads = angle_test * math.pi/180.0 
    x_test = int(radius_test * math.cos(angle_rads)) + x_center 
    y_test = int(radius_test * math.sin(angle_rads)) + y_center 
    #print "x_test, y_test = " + str(x_test) + ", " + str(y_test) 
    im2.putpixel((x_test, y_test),numb_test) 
    angle_test += 1 
# 
draw = ImageDraw.Draw(im2) 
draw.ellipse((20, 20, 40, 40), fill = 'blue', outline ='blue') 
#----------------------------------------------------------- 
im2.show() 
im2.save("out.png") 

ImageDrawもPILからインポートされ、数学がインポートされています。 ここに記載されている楕円を描く方法が見つかりました: http://effbot.org/imagingbook/imagedraw.htm

関連する問題