user22785665
user22785665

Reputation:

How to rotate a section of an image by iteration

Goal

I'm trying to rotate a masked section of an image. For instance if I have a mask that is circular with inner radius of 30 and outer radius of 50 then my aim is to rotate the masked image by some degrees and then add it to the original image. But I want to do this with a circular mask that is decreasing constantly while varying the degree of rotation. Imagine I want to twist the center of an image then all I have to do is rotate circular section of the image with increasing degree of rotate for every decreasing radius.

My code

from PIL import Image, ImageDraw, ImageFilter
import numpy
im1 = Image.open('lena.jpg')
im2 = Image.open('lena.jpg').resize(im1.size)
# Giving The Original image Directory 
# Specified 
Original_Image = Image.open('lena.jpg') 


# This Will Rotate Image By 60 Degree 


for i in numpy.arange(0,10,0.5):
    mask = Image.new("L", im1.size, 0)
    draw = ImageDraw.Draw(mask)
    rotated_image3 = Original_Image.rotate(18*i) 
    draw.ellipse((144+i-5, 54+i-5, 256-i+5, 166-i+5), fill=255)
    draw.ellipse((140+i, 50+i, 260-i, 170-i), fill=0)

    im = Image.composite(rotated_image3, im2, mask)
im.show()

here is the original image

enter image description here

How can I add all rotated images into one image? When I run the code it shows me only one rotated region in the image. how can I show an image where all the regions selected by the for loop appear rotated and not just one rotated region? I know that my code has issues but I don't know how to achieve my goal.

Upvotes: 0

Views: 56

Answers (1)

user22785665
user22785665

Reputation:

I have found a method

from PIL import Image, ImageDraw, ImageFilter
import numpy
im1 = Image.open('gala.png')

# Giving The Original image Directory 
# Specified 
Original_Image = Image.open('gala.png') 


# This Will Rotate Image By 60 Degree 


for i in numpy.arange(0,90,1):
im2=Image.open(f'/Users/beadpile/Desktop/galaxy/gala{i}.png').resize(im1.size)
    mask = Image.new("L", im1.size, 0)
    draw = ImageDraw.Draw(mask)
    rotated_image3 = Original_Image.rotate(i*8) 
    draw.ellipse((144+i-5, 54+i-5, 256-i+5, 166-i+5), fill=255)
    draw.ellipse((140+i, 50+i, 260-i, 170-i), fill=0)
    im = Image.composite(rotated_image3, im2, mask)
    im.save(f'/Users/beadpile/Desktop/galaxy/gala{i+1}.png')
im.show()

Upvotes: 0

Related Questions