我需要画一个矩形,我想在其中放置许多大小相同但颜色不同的圆。
4个圆圈后圆圈概率的颜色。
使用python
from PIL import Image, ImageDraw
draw.ellipse((100, 100, 150, 200), fill=(255, 0, 0), outline=(0, 0, 0))
draw.rectangle((200, 100, 300, 200), fill=(0, 192, 192), outline=(255, 255, 255))ps-我是编程新手
发布于 2020-05-19 17:44:07
也许这能帮助你更进一步..
#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()绘制“小”方块的第三个解决方案
它没有其他代码好,有一些but,但它会把你带到正确的方向。
#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()https://stackoverflow.com/questions/61886436
复制相似问题