#python #ellipse
#python #эллипс
Вопрос:
Я пытаюсь сгенерировать изображение со случайно размещенным и повернутым эллипсом. Все остальное работает так, как должно, однако эллипс не вращается.
from PIL import ImageDraw, Image, ImageFilter
import math
import numpy as np
import random
#define build area
x_bound = int(input("X Resolution? (Pixel) "))
y_bound = int(input("Y resolution? (Pixel) "))
#define number of outputs
repetitions = int(input("How many output images would you like? "))
count = 0
#choose amount of bubbles
distortion = int(input("how many distorting circles would you like "))
while count != repetitions:
img = Image.new("RGB", (x_bound, y_bound), color="White")
draw = ImageDraw.Draw(img)
#initialize randomness based on clock
random.seed()
# Generate a basic random colour, random RGB values 10-245
R, G, B = random.randint(10, 245), random.randint(10, 245), random.randint(10, 245),
for _ in range(0, distortion):
# Choose RGB values for this circle
r = R random.randint(-10, 10)
g = G random.randint(-10, 10)
b = B random.randint(-10, 10)
diam = random.randint(30, 90)
x, y = random.randint(0, x_bound), random.randint(0, y_bound)
draw.ellipse([x, y, x diam, y diam], fill=(r, g, b))
# Blur the background a bit
res = img.filter(ImageFilter.BoxBlur(5))
#img.show(res)
# define shape bounds.
# I don't want to have a 0 value for size, always moving in the positive direction
x1 = random.randint(0, x_bound) y1 = random.randint(0, y_bound)
x2 = random.randint(x1, x_bound)
y2 = random.randint(y1, y_bound)
#commented line allows you to check coordinates, point of error check
print(x1, y1, x2, y2)
shape = [(x1, y1), (x2, y2)]
# create ellipse image
img1 = ImageDraw.Draw(img)
img1.ellipse(shape, fill=None, outline="black")
# rotate the ellipse image
rotation_value = random.randint(0, 180)
rotate = img.rotate(rotation_value)
#img.show(rotate)
# finding bounds of rotated figure
angle = rotation_value * math.pi / 180
# rotated coordinates of each of the four bounded corners
x_prime_1 = -y1 * math.sin(angle) x1 * math.cos(angle)
y_prime_1 = y1 * math.cos(angle) x1 * math.sin(angle)
x_prime_2 = -y2 * math.sin(angle) x2 * math.cos(angle)
y_prime_2 = y2 * math.cos(angle) x2 * math.sin(angle)
# commented code line is to check coordinates of rotated shape for validity
print(x_prime_1, y_prime_1, x_prime_2, y_prime_2)
# start a counter, ensure that x coordinates fall within defined bounds
x_count = 0
if x_prime_1 < x_bound:
x_count = 1
if x_prime_1 > 0:
x_count = 1
if x_prime_2 < x_bound:
x_count = 1
if x_prime_2 > 0:
x_count = 1
# start a counter, ensure that y coordinates fall within defined bounds
y_count = 0
if y_prime_1 < y_bound:
y_count = 1
if y_prime_1 > 0:
y_count = 1
if y_prime_2 < y_bound:
y_count = 1
if y_prime_2 > 0:
y_count = 1
print(x_count), print(y_count)
# ensure that both x and y coordinates fall within the defined area. Only then will it produce an
image.
if y_count == 4:
if x_count == 4:
img.show()
count = 1