简体   繁体   中英

how to draw using python

I need to draw a rectangle in which I want to place numbers of circles with the same size but different colors.

color of circle chance after 4 circles.

using 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- I am new to programing

maybe this could help you a little bit further..

#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))

You first need to create an image to draw on. In this case it's a black background. The next thing is to make a 'draw' object so you can draw on the image.

The drawing function you gave works correct, but I changed the notation from (x,y,x,y) to ((x,y),(x,y)) to make it more clear. The first (x,y) are the starting coordinates, and the second (x,y) the 'stop' coordinates.

Also added some variables to change the color of the circles. At the end of the for loop this involves a small calculation because of the 255 number limit the fill argument has.

The for loop for drawing the circle 4 times I didn't added, but in the code it says where it needs to be. Maybe you can try this for yourself as a little excercise.. I also didn't fully understand what you wanted with the 4 circles.

If you need 4 pictures with the same circle color, then add a for loop around img.show() like so:

for j in range(4):
    img.show()

Please ask if you have more questions:)


Second solution because question was understood differently

Note: Added more libraries, for random and math. There also are a few calculations involved, which could be done better, you need to expiriment a bit of how you want everything to look like.

#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()

Thirth solution for drawing 'little' squares

It's not as good as the other codes, has a few bugs, but it will get you in the right direction.

#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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM