也许这可以帮助你更进一步..
#Import the library
from PIL import Image, ImageDraw
#Creating new Image object for background. The color 'scheme' is 'RGB' and the size 500x500pixels
img = Image.new("RGB", (500, 500))
#Creating object from img to draw on.
draw = ImageDraw.Draw(img)
#First drawing a rectangle from (x,y) to (x,y). With color 'fill=(..)' and border 'outline(..)'
#https://www.geeksforgeeks.org/python-pil-imagedraw-draw-rectangle/
draw.rectangle((50, 50, 450, 450), fill=(0, 192, 192), outline=(255, 255, 255))
#Variables for the color of the circle/ellipse
r = 0
g = 255
b = 0
NUMBER_OF_PICTURES = 5 #Change this variable for more or less pictures
#For loop to do it 'NUMBER_OF_PICTURES' times
for i in range(NUMBER_OF_PICTURES):
#Printing calculated color
print('Picture: ',i+1,'- Circle color: ', r, g, b)
#Add for loop here for printing 4 times.
#Note: There will be no difference in the output because they then are on top of each other
draw.ellipse(((50, 50), (450, 450)), fill=(r, g, b), outline=(0, 0, 0))
#Showing the image
img.show()
#Changing the colors with a calculation so to not exceed the '255' limit for the 'fill=(r, g, b)' argument of the circle drawing
r = r + int((255/NUMBER_OF_PICTURES))
g = g - int((255/NUMBER_OF_PICTURES))
b = b + int((255/NUMBER_OF_PICTURES))
您首先需要创建一个图像来绘制。在这种情况下,它是黑色背景。接下来是制作一个“绘图”对象,以便您可以在图像上绘图。
您提供的绘图功能正确,但我将符号从 (x,y,x,y) 更改为 ((x,y),(x,y)) 以使其更清晰。第一个(x,y) 是起始坐标,第二个(x,y) 是“停止”坐标。
还添加了一些变量来改变圆圈的颜色。在 for 循环结束时,由于 fill 参数有 255 个数字限制,这涉及到一个小计算。
我没有添加 4 次绘制圆圈的 for 循环,但在代码中它说明了它需要在哪里。也许你可以自己试试这个作为一个小练习。我也没有完全理解你想要的 4 个圆圈。
如果您需要 4 张具有相同圆圈颜色的图片,则在 img.show() 周围添加一个 for 循环,如下所示:
for j in range(4):
img.show()
如果您有更多问题,请询问:)
第二个解决方案,因为对问题的理解不同
注意:添加了更多库,用于随机和数学。还涉及一些计算,可以做得更好,您需要尝试一下您希望一切看起来如何。
#Import the libraries
from PIL import Image, ImageDraw
import math
import random
#Creating new Image object for background. The color 'scheme' is 'RGB' and the size IMG_WIDTH x IMG_HEIGHT pixels
IMG_WIDTH = 400
IMG_HEIGHT = 250
img = Image.new("RGB", (IMG_WIDTH, IMG_HEIGHT))
#Creating object from img to draw on.
draw = ImageDraw.Draw(img)
#First drawing a rectangle from (x,y) to (x,y). With color 'fill=(..)' and border 'outline(..)'
#https://www.geeksforgeeks.org/python-pil-imagedraw-draw-rectangle/
REC_START_X = 50 #Start (x and y) needs to be the same
REC_START_Y = 50
REC_STOP_X = 350 #Stop (x and y) needs to be the same
REC_STOP_Y = 200
draw.rectangle((REC_START_X, REC_START_Y, REC_STOP_X, REC_STOP_Y), fill=(0, 192, 192), outline=(255, 255, 255))
#It depends on the number of cicles in one row and the number of pixels between
NUMBER_OF_CIRCLES_IN_ONE_ROW = 16
NUMBER_OF_CIRCLE_WITH_SAME_COLOR = 4
#Calculates the number of circles in one column and calculates the diameter. REC_START_Y and REC_STOP_Y determines how 'good' the cicles fit
#Cast to int because range(..) cannot handle float
circleDiameter = (REC_STOP_X - REC_START_X)/NUMBER_OF_CIRCLES_IN_ONE_ROW
circlesInOneColumn = int((REC_STOP_Y-REC_START_Y)/circleDiameter)
print("circlesInOneRow: ", NUMBER_OF_CIRCLES_IN_ONE_ROW, " circlesInOneColumn: ", circlesInOneColumn, " circleDiameter: ", circleDiameter)
#Calculates number of colors and generates 'random' rgb numbers between 0 and 255
#https://www.pythonforbeginners.com/random/how-to-use-the-random-module-in-python
numberOfCircles = NUMBER_OF_CIRCLES_IN_ONE_ROW * circlesInOneColumn
numberOfColors = numberOfCircles/NUMBER_OF_CIRCLE_WITH_SAME_COLOR
r = random.randint(-1, 255)
g = random.randint(-1, 255)
b = random.randint(-1, 255)
#Counter for the colors
colorCounter = 0
#Draw the circles from left to right and then starting on the next row
for i in range(circlesInOneColumn):
for j in range(NUMBER_OF_CIRCLES_IN_ONE_ROW):
draw.ellipse(((REC_START_X+(circleDiameter*j), REC_START_Y+(circleDiameter*i)), (REC_START_X+(circleDiameter*(j+1)), REC_START_Y+(circleDiameter*(i+1)))), fill=(r, g, b), outline=(0, 0, 0))
colorCounter = colorCounter +1
if((colorCounter % NUMBER_OF_CIRCLE_WITH_SAME_COLOR) == 0):
r = random.randint(-1, 255)
g = random.randint(-1, 255)
b = random.randint(-1, 255)
#Showing the image
img.show()
绘制“小”方块的第三种解决方案
它不如其他代码好,有一些错误,但它会让你朝着正确的方向前进。
#Import the libraries
from PIL import Image, ImageDraw
import math
import random
#Creating new Image object for background. The color 'scheme' is 'RGB' and the size IMG_WIDTH x IMG_HEIGHT pixels
IMG_WIDTH = 400
IMG_HEIGHT = 400
img = Image.new("RGB", (IMG_WIDTH, IMG_HEIGHT))
#Creating object from img to draw on.
draw = ImageDraw.Draw(img)
#First drawing a rectangle from (x,y) to (x,y). With color 'fill=(..)' and border 'outline(..)'
#https://www.geeksforgeeks.org/python-pil-imagedraw-draw-rectangle/
REC_START_X = 50 #Start (x and y) needs to be the same
REC_START_Y = 50
REC_STOP_X = 350 #Stop (x and y) needs to be the same
REC_STOP_Y = 350
draw.rectangle((REC_START_X, REC_START_Y, REC_STOP_X, REC_STOP_Y), fill=(0, 192, 192), outline=(255, 255, 255))
#Number of small squares. Needs to be ..^2
NUMBER_OF_SMALL_SQUARES = 4
#It depends on the number of cicles in one row and the number of pixels between
#Note: NUMBER_OF_CIRCLES_IN_ONE_ROW > NUMBER_OF_SMALL_SQUARES
NUMBER_OF_CIRCLES_IN_ONE_ROW = 8
NUMBER_OF_CIRCLE_WITH_SAME_COLOR = 16
#Calculates the number of circles in one column and calculates the diameter. REC_START_Y and REC_STOP_Y determines how 'good' the cicles fit
#Cast to int because range(..) cannot handle float
circleDiameter = (REC_STOP_X - REC_START_X)/NUMBER_OF_CIRCLES_IN_ONE_ROW
circlesInOneColumn = int((REC_STOP_Y-REC_START_Y)/circleDiameter)
print("circlesInOneRow: ", NUMBER_OF_CIRCLES_IN_ONE_ROW, " circlesInOneColumn: ", circlesInOneColumn, " circleDiameter: ", circleDiameter)
#Calculates number of colors and generates 'random' rgb numbers between 0 and 255
#https://www.pythonforbeginners.com/random/how-to-use-the-random-module-in-python
numberOfCircles = NUMBER_OF_CIRCLES_IN_ONE_ROW * circlesInOneColumn
numberOfColors = numberOfCircles/NUMBER_OF_CIRCLE_WITH_SAME_COLOR
r = random.randint(-1, 255)
g = random.randint(-1, 255)
b = random.randint(-1, 255)
#Counter for the colors, rows and columns
colorCounter = 0
#Draw the circles from left to right and then starting on the next row
#Do this NUMBER_OF_SMALL_SQUARES times
xMovement = 0
yMovement = 0
rowNumber = 0
columnNumber = 0
for k in range(NUMBER_OF_SMALL_SQUARES):
for i in range(int(circlesInOneColumn/math.sqrt(NUMBER_OF_SMALL_SQUARES))):
for j in range(int(NUMBER_OF_CIRCLES_IN_ONE_ROW/math.sqrt(NUMBER_OF_SMALL_SQUARES))):
startX = REC_START_X+(circleDiameter*j) + xMovement
startY = REC_START_Y+(circleDiameter*i) + yMovement
stopX = REC_START_X+(circleDiameter*(j+1)) + xMovement
stopY = REC_START_Y+(circleDiameter*(i+1)) + yMovement
draw.ellipse((startX, startY , stopX, stopY), fill=(r, g, b), outline=(0, 0, 0))
colorCounter = colorCounter +1
if((colorCounter % NUMBER_OF_CIRCLE_WITH_SAME_COLOR) == 0):
r = random.randint(-1, 255)
g = random.randint(-1, 255)
b = random.randint(-1, 255)
rowNumber = rowNumber + 1
columnNumber = columnNumber + 1
xMovement = xMovement + circleDiameter*(NUMBER_OF_CIRCLES_IN_ONE_ROW/math.sqrt(NUMBER_OF_SMALL_SQUARES))
if(xMovement == circleDiameter*NUMBER_OF_CIRCLES_IN_ONE_ROW):
xMovement = 0
yMovement = yMovement + circleDiameter*(NUMBER_OF_CIRCLES_IN_ONE_ROW/math.sqrt(NUMBER_OF_SMALL_SQUARES))
#Showing the image
img.show()