我得到的是黑白图像,而不是所需的输出,是吗?
from PIL import Image
def draw_nested_rectangles():
height = int(input('Please enter the overall height: '))
width = int(input('Please enter the overall width: '))
size = (width,height)
pic = Image.new('RGB',size,'white')
firstRec(width,height)
secondRec(width,height)
thirdRec(width,height)
fourthRec(width,height)
pic.show()
def firstRec(width,height):
size = (width,height)
pic = Image.new('RGB', size, 'white')
blueProgression = 0
for x in range(width,width):
color = (0,0,blueProgression)
for y in range(height,height):
pic.putpixel((x,y),color)
blueProgression += 5
def secondRec(width,height):
size = (width,height)
pic = Image.new('RGB', size, 'white')
greenProgression = 255
for x in range(int(width*0.15),int(width*0.85)):
color = (0,greenProgression,0)
for y in range(int(height*0.15),int(height*0.85)):
pic.putpixel((x,y),color)
greenProgression -= 5
def thirdRec(width,height):
size = (width,height)
pic = Image.new('RGB', size, 'white')
greenProgression = 255
for x in range(int(width*0.30),int(width*0.70)):
color = (255,0,0)
for y in range(int(height*0.30),int(height*0.70)):
pic.putpixel((x,y),color)
def fourthRec(width,height):
size = (width,height)
pic = Image.new('RGB', size, 'white')
greenProgression = 255
for x in range(int(width*0.45),int(width*0.55)):
color = (255,255,255)
for y in range(int(height*0.45),int(height*0.55)):
pic.putpixel((x,y),color)
我希望输出为四个嵌套的三角形,第一个为宽度和高度的100%(从右侧的黑色开始,逐渐增加为蓝色),第二个嵌套在第一个的70%原始宽度和高度(从左侧开始为黑色,然后逐渐增加为绿色),第三个嵌套在其中,占原来的40%(纯红色),最后一个嵌套在其中,占10%(纯白色)>
答案 0 :(得分:1)
尝试:-
from PIL import Image
def draw_nested_rectangles():
global pic
height = int(input('Please enter the overall height: '))
width = int(input('Please enter the overall width: '))
size = (width,height)
pic = Image.new('RGB', size, 'white')
firstRec(width, height)
secondRec(width, height)
thirdRec(width, height)
fourthRec(width, height)
pic.show()
def firstRec(width,height):
global pic
size = (width,height)
blueProgression = 0
for x in range(width,width):
color = (0,0,blueProgression)
for y in range(height,height):
pic.putpixel((x,y),color)
blueProgression += 5
def secondRec(width,height):
global pic
size = (width,height)
greenProgression = 255
for x in range(int(width*0.15),int(width*0.85)):
color = (0,greenProgression,0)
for y in range(int(height*0.15),int(height*0.85)):
pic.putpixel((x,y), color)
greenProgression -= 5
def thirdRec(width,height):
global pic
size = (width,height)
greenProgression = 255
for x in range(int(width*0.30),int(width*0.70)):
color = (255,0,0)
for y in range(int(height*0.30),int(height*0.70)):
pic.putpixel((x,y),color)
def fourthRec(width,height):
global pic
size = (width,height)
greenProgression = 255
for x in range(int(width*0.45),int(width*0.55)):
color = (255,255,255)
for y in range(int(height*0.45),int(height*0.55)):
pic.putpixel((x,y),color)
pic = 0
draw_nested_rectangles()
不良输出的原因:-
之所以获得空白图像而不是预期的图像,是因为您在每个函数内创建了一个单独的pic
图像对象,因此所有功能都在单独的图像对象上运行而不是一个。
FIX:-
为了获得所需的输出,必须使用语法pic
global pic
变量定义为全局变量,在每个要使用它的函数中
样品输入:-
Please enter the overall height: 500
Please enter the overall width: 500
样品输出:-