rotate image python

Solutions on MaxInterview for rotate image python by the best coders in the world

showing results for - "rotate image python"
Jana
18 Jan 2018
1def rotate(image,a): 
2   #image : your image
3   #a : angle to rotate your image
4    angle = - ( a )            
5    angle=math.radians(angle)                               
6    cosine=math.cos(angle)
7    sine=math.sin(angle)
8    height=image.shape[0]    
9    width=image.shape[1]                                  
10    # height and width of the new image 
11    new_height  = round(abs(image.shape[0]*cosine)+abs(image.shape[1]*sine))+1
12    new_width  = round(abs(image.shape[1]*cosine)+abs(image.shape[0]*sine))+1
13    #image variable of dimensions of new_height and new _column filled with zeros
14    output=np.zeros((new_height,new_width,image.shape[2]))
15    image_copy=output.copy()
16    #Find the centre of the image about which we have to rotate the image
17    original_centre_height   = round(((image.shape[0]+1)/2)-1)    
18    original_centre_width    = round(((image.shape[1]+1)/2)-1)   
19    # Find the centre of the new image that will be obtained
20    new_centre_height= round(((new_height+1)/2)-1)        
21    new_centre_width= round(((new_width+1)/2)-1)          
22
23    for i in range(height):
24        for j in range(width):
25            #co-ordinates of pixel with respect to the centre of original image
26            y=image.shape[0]-1-i-original_centre_height                   
27            x=image.shape[1]-1-j-original_centre_width 
28            '''
29            #co-ordinate of pixel with respect to the rotated image
30        
31            new_y=round(-x*sine+y*cosine)
32            new_x=round(x*cosine+y*sine)
33            '''
34            new_y,new_x=shear(angle,x,y)
35            new_y=new_centre_height-new_y
36            new_x=new_centre_width-new_x
37        
38            if 0 <= new_x < new_width and 0 <= new_y < new_height and new_x>=0 and new_y>=0:
39
40                output[new_y,new_x,:]=image[i,j,:] 
41    pil_img=Image.fromarray((output).astype(np.uint8))      
42
43    pil_img.save("rotated_image.png")    
44    img=cv.imread("rotated_image.png")
45    img_2=cv.imread("img.jpg")
46    cv.imshow("Befor Rotate",img_2)
47    cv.imshow("After Rotate",img)
48    cv.waitKey(0)
49          
50def shear(angle,x,y):
51    # shear 1
52    tangent=math.tan(angle/2)
53    new_x=round(x-y*tangent)
54    new_y=y
55    #shear 2
56    new_y=round(new_x*math.sin(angle)+new_y)      #since there is no change in new_x according to the shear matrix
57    #shear 3
58    new_x=round(new_x-new_y*tangent)              #since there is no change in new_y according to the shear matrix
59    return new_y,new_x
60
61            
Claudia
08 Apr 2019
1import skimage
2import skimage.transform
3rotated_img=skimage.transform.rotate(img,-60, resize=True)
Giacomo
26 Jun 2020
1def rotate_image(img, degrees, output_scale="crop"):
2
3    assert output_scale in ["crop", "full"], "output_scale should be either 'crop' or 'full'"
4    #  convert rotation amount to radian
5    rot_rad = degrees * np.pi / 180.0
6    rotate_m = np.array([[np.cos(rot_rad), np.sin(rot_rad)],
7                         [- np.sin(rot_rad), np.cos(rot_rad)]])
8
9    # If output_scale = "full", the image must be inserted into a bigger frame, so the coordinates would be translated
10    # appropriately.
11    gray_scale = False
12    if len(img.shape) < 3:
13        img = img.reshape(*img.shape, 1)
14        gray_scale = True
15
16    h, w, c = img.shape
17    if output_scale == "full":
18        diagonal = int(np.sqrt(h * h + w * w))   # Pytagoras theorm - the diagonal is the longest line in the rectangle
19        im_padded = np.zeros((diagonal, diagonal, c))
20        center_h = int((diagonal - h) // 2)
21        center_w = int((diagonal - w) // 2)
22        im_padded[center_h:-center_h, center_w:-center_w, :] = img
23        img = im_padded
24        rotated_image = np.zeros((diagonal, diagonal, c))
25        h, w, c = img.shape
26    else:
27        rotated_image = np.zeros((h, w, c))
28
29    # Rotate and shift the indices, from PICTURE to SOURCE (and NOT the intuitive way)
30    indices_org = np.array(np.meshgrid(np.arange(h), np.arange(w))).reshape(2, -1)
31    indices_new = indices_org.copy()
32    indices_new = np.dot(rotate_m, indices_new).astype(int)   # Apply the affineWrap
33    mu1 = np.mean(indices_new, axis=1).astype(int).reshape((-1, 1))
34    mu2 = np.mean(indices_org, axis=1).astype(int).reshape((-1, 1))
35    indices_new += (mu2-mu1)   # Shift the image back to the center
36
37    # Remove the pixels in the rotated image, that are now out of the bounds of the result image
38    # (Note that the result image is a rectangle of shape (h,w,c) that the rotated image is inserted into, so in the
39    # case of a "full" output_scale, these are just black pixels from the padded image...).
40    t0, t1 = indices_new
41    t0 = (0 <= t0) & (t0 < h)
42    t1 = (0 <= t1) & (t1 < w)
43    valid = t0 & t1
44    indices_new = indices_new.T[valid].T
45    indices_org = indices_org.T[valid].T
46
47    #
48    xind, yind = indices_new
49    xi, yi = indices_org
50    rotated_image[xi, yi, :] = img[xind, yind, :]
51
52    if gray_scale:
53        rotated_image = rotated_image.reshape((h, w))
54
55    return rotated_image.astype(np.uint8)
56  
57img = cv2.imread(image_path)
58rotated = rotate_image(img, 45)
59cv2.imshow("Rotated_Image", rotated)
60cv2.waitkey(0)
queries leading to this page
how to rotate image using pilrotate image continuously pythonto rotate an image in pythonrotate imu pythonhow turn images pythonhow to rotate image in pyhtonpython pil imagedraw rotate image rotate vector pythonhow to rotate the image using pil in pythonutils rotate pythonpython resize image rotatepython image rotateimage rotate pythoned rotation of image in pythonhow to save the rotate image in pythonrotate images matrix pythonpython rotate image without libraryrotate image program in pythonimage manipulation python shear rotaterotateimage 28a 29 pythonrotate an image with pythonpython rotatehow to rotate a image in python imutils python image rotaterotate image pillowrotate image with pil pythonbest way to rotate image in pythonrotateimage python rotation image pythonrotate an image pythonmatplotlib rotate 90 degrees imagesrotate pictures pythonrotate image function in pythonimshow python rotate imghow to rotate image manualy pythonpython code to rotate imagerotate and insert image pythonpython resize image rotate manuallyopencv rotationpython rotate image py xrotate image degrees opencvpillow python rotate imagerotate image pilrotate images in pythonrotate image x degrees cv2how to rotate an image pythonrotate png by 90 degrees pythonrotate image ythonpython rotate imahehow to rotate image in pythonhow to rotate a picture smoothly in pythonpython rotate a imagepython rotate picturerotate image by angle python using pillowrotate image opencv pythonrotaate picture with pythonrotate image python youtubecv2 rotate imageimage rotatein pythonrotate image in python matplotlibhow to rotate an image in pythonpython code for rotating an imageimage rotation python opencvfunction to rotate an image in pythonpython rotate vectorrotate image pypython rotate imgaepil rotate image pythonimage rotate in pythonrotate 90 degrees image wqith pilrotate the image by 180 in pythonhow to rotate image python manuallyimage rotation python python rotate image and cropopencv rotate 1 degreerotating an image in pygaempython image rotate manuallycan you rotate an image directly in pythonhow to rotate images in pythonpython def rotate image 28a 29 3apython get rotation of imagerotate image in pythonrotate image and points pythonpython rotate the image in placeimg rotate pythonrotate an image 2b pythonpython restore rotate imagepython rotate image manualypython rotate imagehow to rotate a pic in pythonrotate array pythonrotate image pyhtonhow rotate images pythonrotation of image in pythonrotate an image using pythonpython image roation manual poythonopencv rotate image degreeread image using opencv and rotaterotate images usingmatrices in pythonhow to rotate an img pythonpython rotate image until orientedrotate picture using pythonpython code to rotate an imagerotate image pil pythonpythonocc rotate imagerotate image python pilrotate image using opencv pythonrotate image in python image libraryhow to rotate mri image in pythonhow to rotate image in ptthonrotate the image in pythonhow to rotate at pic in pythonpython rotate imagesauto rotate image pythonrotate image python matplotlibimage rotate pythnrotate image info pythonrotate an image using pyrhonimplement image rotation pythonhow to rotate the image in pythonrotate image in python without libraryrotate image 270 pythonpython rotation imagehow to rotate an image in imagetk pythonmontage image rotate pythonrotate 3d image pythonrotate pythonrotate images using pilhow turn image pythonhow to rotate the image using pythonrotate image pythonpython pil rotate imagerotate images pythonrotate imagem python gameimage rotate pythonratating image pythonrototion value of image pythoncv2 rotate image by anglerotate image python