3

Can someone tell me how to rotate only part of an image like this:

this

How to find coordinate / center of this image:

image

i can rotate all pict using this

from PIL import Image

def rotate_image():
img = Image.open("nime1.png")

img.rotate(45).save("plus45.png")
img.rotate(-45).save("minus45.png")

img.rotate(90).save("90.png")
img.transpose(Image.ROTATE_90).save("90_trans.png")

img.rotate(180).save("180.png")


if __name__ == '__main__':
rotate_image()
4
  • Try to include the picture in your question without linking to it. Commented Aug 28, 2016 at 5:20
  • done sir, this image Commented Aug 28, 2016 at 5:26
  • Make a copy of the image; crop to the desired area you want to rotate, rotate it, and paste it on the original. Does that work? Commented Aug 28, 2016 at 5:27
  • Iam try using this answer but i got error :( Commented Aug 28, 2016 at 5:31

2 Answers 2

1

You can crop an area of the picture as a new variable. In this case, I cropped a 120x120 pixel box out of the original image. It is rotated by 90 and then pasted back on the original.

from PIL import Image

img = Image.open('./image.jpg')
sub_image = img.crop(box=(200,0,320,120)).rotate(90)
img.paste(sub_image, box=(200,0))

enter image description here

So I thought about this a bit more and crafted a function that applies a circular mask to the cropped image before rotations. This allows an arbitrary angle without weird effects.

def circle_rotate(image, x, y, radius, degree):
    img_arr = numpy.asarray(image)
    box = (x-radius, y-radius, x+radius+1, y+radius+1)
    crop = image.crop(box=box)
    crop_arr = numpy.asarray(crop)
    # build the cirle mask
    mask = numpy.zeros((2*radius+1, 2*radius+1))
    for i in range(crop_arr.shape[0]):
        for j in range(crop_arr.shape[1]):
            if (i-radius)**2 + (j-radius)**2 <= radius**2:
                mask[i,j] = 1
    # create the new circular image
    sub_img_arr = numpy.empty(crop_arr.shape ,dtype='uint8')
    sub_img_arr[:,:,:3] = crop_arr[:,:,:3]
    sub_img_arr[:,:,3] = mask*255
    sub_img = Image.fromarray(sub_img_arr, "RGBA").rotate(degree)
    i2 = image.copy()
    i2.paste(sub_img, box[:2], sub_img.convert('RGBA'))
    return i2

i2 = circle_rotate(img, 260, 60, 60, 45)
i2

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

yes sir, is successfull ,but i can't save a new pict :(
sir why iam not successfull , if i try using circle_rotate
You can save the picture using i2.save('./new_image.jpg')
sir iam use SC for rotate, but cant, error in i2 = circle_rotate(img, 260, 60, 60, 45) :( and if i will using SC where code from input image?
0

You can solve this problem as such. Say you have img = Image.open("nime1.png")

  1. Create a copy of the image using img2 = img.copy()
  2. Create a crop of img2 at the desired location using img2.crop(). You can read how to do this here
  3. Paste img2 back onto img at the appropriate location using img.paste()

Notes:

To find the center coordinate, you can divide the width and height by 2 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.