how to rotate image python manually

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

showing results for - "how to rotate image python manually"
Louanne
15 Aug 2020
1import skimage
2import skimage.transform
3rotated_img=skimage.transform.rotate(img,-60, resize=True)
Ilaria
06 Nov 2019
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
rototion value of image pythonhow to rotate a image in python auto rotate image pythonrotate png by 90 degrees pythonhow to rotate image in ptthonpython rotate picturepython rotate image and croprotate the image by 180 in pythonhow to rotate the image in pythonread image using opencv and rotaterotate image ythonrotate image x degrees cv2matplotlib rotate 90 degrees imagesrotate vector pythoncv2 rotate image by anglerotate image by angle python using pillowpython get rotation of imagerotateimage python rotate an image with pythonpython code to rotate an imagepython rotate image py xrotate and insert image pythonrotation image pythonhow to rotate image python manuallyhow rotate images pythonhow to save the rotate image in pythonrotate image in python without libraryrotate an image using pyrhonrotate the image in pythonpythonocc rotate imagemontage image rotate pythonpython rotate imageshow to rotate at pic in pythonrotate an image using pythonrotate images matrix pythonrotateimage 28a 29 pythonpython rotate imagepython restore rotate imagerotate image function in pythonrotate image continuously pythonhow to rotate the image using pil in pythonhow to rotate image manualy pythonpython rotate a imagepillow python rotate imagerotate images using pilpython rotate image manualyutils rotate pythoncan you rotate an image directly in pythonto rotate an image in pythonrotate image info pythonrotate pythonimg rotate pythonrotate image pyrotate image in python matplotlibrotate an image 2b pythonimage manipulation python shear rotatehow turn image pythonrotate pictures pythonrotate image using opencv pythonhow to rotate mri image in pythonhow to rotate image in pyhtonimage rotate pythonimutils python image rotaterotate image 270 pythonhow to rotate an image pythonhow to rotate images in pythonrotate image python matplotlibpython image rotatepython rotate imgaehow to rotate the image using pythonpython rotate the image in placerotate image pythonrotate array pythonrotate 90 degrees image wqith pilrotate image with pil pythonimage rotation python opencvimshow python rotate imgimage rotation python how turn images pythonfunction to rotate an image in pythonrotate image pillowpython rotation imagepython code to rotate imagerotate imu pythonrotate image opencv pythonrotate image pilopencv rotate image degreerotate images pythonpython rotaterotate image pil pythonbest way to rotate image in pythonrotate image and points pythonpil rotate image pythoncv2 rotate imagehow to rotate an image in pythonrotate image python youtubehow to rotate a pic in pythonpython code for rotating an imagehow to rotate a picture smoothly in pythonratating image pythonrotate imagem python gamehow to rotate an image in imagetk pythonpython image roation manual poythonhow to rotate image using pilrotating an image in pygaemrotate image in pythonpython pil rotate imagehow to rotate image in pythonpython def rotate image 28a 29 3apython rotate image until orientedpython rotate vectorpython pil imagedraw rotate image rotate image python pilpython rotate image without libraryrotate picture using pythonrotaate picture with pythonopencv rotationrotate image degrees opencvrotate image program in pythonrotate images usingmatrices in pythonimage rotate pythonrotate an image pythonopencv rotate 1 degreehow to rotate image python manually