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")
我使用的代码将三个随机数放入一个元组中,然后将该元组放入一个列表中。每个元组都是一个像素。用户输入的尺寸将始终形成x宽和y长的直线形状,我想使形状呈圆形并具有弯曲边缘。怎么样?感谢。
答案 0 :(得分: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