user13758379
user13758379

Reputation:

python image show

I want to fill different color after 6 circles in row and 6 in column

from PIL import Image, ImageDraw
import math
IMG_HEIGHT pixels
IMG_WIDTH = 1440
IMG_HEIGHT = 1280
img = Image.new("RGB", (IMG_WIDTH, IMG_HEIGHT)) 
draw = ImageDraw.Draw(img) 

REC_START_X = 20            #Start (x and y) needs to be the same
REC_START_Y = 20
REC_STOP_X = 1020            #Stop (x and y) needs to be the same
REC_STOP_Y = 710
   
  
#Calculates the number of circles in one column and calculates the diameter.
circleDiameter = (REC_STOP_X - REC_START_X)/NUMBER_OF_CIRCLES_IN_ONE_ROW
circlesInOneColumn = int((REC_STOP_Y-REC_START_Y))
print("circlesInOneRow: ", NUMBER_OF_CIRCLES_IN_ONE_ROW, " circlesInOneColumn: ", circlesInOneColumn, " circleDiameter: ", circleDiameter)
    #Draw the circles from left to right and then starting on the next row

       
img.show()

the code working but I am unable to put different color after 6 circles both row and column color.

Upvotes: 1

Views: 63

Answers (1)

iGian
iGian

Reputation: 11183

This solution is made with opencv, maybe it can provide an hint.

First define a helper for random color.

from random import randrange

def rand_color():
    return (randrange(256), randrange(256), randrange(256))

Then the presets:

radius = 10
num_circles_per_side = 6
num_cols = 4
num_rows = 3

Useful variables and image initialization:

side = num_circles_per_side * radius * 2
w = num_cols * side
h = num_rows * side
patch_side = num_circles_per_side * radius * 2

image = np.zeros([h, w, 3], np.uint8)
patch = np.zeros([patch_side, patch_side], np.uint8)

Loops:

# draws white circles on the patch
for y in range(num_circles_per_side):
    for x in range(num_circles_per_side):
        cv2.circle(patch, (x * radius * 2 + radius, y * radius * 2 + radius), radius, 255, cv2.FILLED)

# uses the patch as mask for the square slices
for y in range(num_rows):
    for x in range(num_cols):
        slice = np.s_[y * side:y * side + side, x * side:x * side + side]
        image[slice][patch == 255] = rand_color()

Output image:

enter image description here

Circles misses the border but it can easily added it modifying the patch.

Upvotes: 2

Related Questions