Rotate Image | OpenCV vs Pillow | Python
In this tutorial, you will learn how to rotate an image in OpenCV (cv2) and Pillow (PIL).
- To rotate the image without cropping, please read Rotate Image Without Cropping.
- To specify the background color, please read Rotate/Translate Image with Background Color.
1. Rotate by 90, 180, or 270 Degrees
For OpenCV, the function cv2.rotate
lets you rotate the image only by 90, 180, or 270 degrees.
# rotate 90 degrees clockwise
rotated_cv2_img = cv2.rotate(cv2_img, cv2.ROTATE_90_CLOCKWISE)
# rotate 180 degrees
rotated_cv2_img = cv2.rotate(cv2_img, cv2.ROTATE_180)
# rotate 270 degrees clockwise (90 degrees counterclockwise)
rotated_cv2_img = cv2.rotate(cv2_img, cv2.ROTATE_90_COUNTERCLOCKWISE)
2. Rotate by Any Angle
OpenCV
# rotate 45 degrees counterclockwise
h, w = cv2_img.shape[:2]
M = cv2.getRotationMatrix2D(center=(w/2, h/2), angle=45, scale=1.0)
rotated_cv2_img = cv2.warpAffine(cv2_img, M, (w, h))
Pillow
# rotate 45 degrees counterclockwise
rotated_pil_img = pil_img.rotate(45)
Full Example
OpenCV
import cv2
# read image
cv2_img = cv2.imread("test_images/test1.jpg")
# rotate 45 degrees counterclockwise
h, w = cv2_img.shape[:2]
M = cv2.getRotationMatrix2D(center=(w/2, h/2), angle=45, scale=1.0)
rotated_cv2_img = cv2.warpAffine(cv2_img, M, (w, h))
# show the rotated image
cv2.imshow("cv2 rotated image", rotated_cv2_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Pillow
from PIL import Image
# read image
pil_img = Image.open("test_images/test1.jpg")
# rotate 45 degrees counterclockwise
rotated_pil_img = pil_img.rotate(45)
# show the rotated image
rotated_pil_img.show("pil rotated image")
Syntax
OpenCV
cv2.rotate(src, rotateCode[, dst])
Parameters:
src
: input array.dst
: output array of the same type as src. The size is the same withROTATE_180
, and the rows and cols are switched forROTATE_90_CLOCKWISE
andROTATE_90_COUNTERCLOCKWISE
.rotateCode
: an enum to specify how to rotate the array; see the enum RotateFlags.
Returns:
- An image (Numpy array).
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
Parameters:
src
: input image.dst
: output image that has the size dsize and the same type assrc
.M
: 2×3 transformation matrix.dsize
: the size of the output image.flags
: a combination of interpolation methods (see InterpolationFlags) and the optional flagWARP_INVERSE_MAP
that means thatM
is the inverse transformation (dst
→src
).borderMode
: pixel extrapolation method (see BorderTypes); whenborderMode
=BORDER_TRANSPARENT
, it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function.borderValue
: value used in case of a constant border; by default, it is 0.
Pillow
Image.rotate(angle, resample=Resampling.NEAREST, expand=0, center=None, translate=None, fillcolor=None)
Parameters:
angle
: In degrees counter-clockwise.resample
: An optional resampling filter. This can be one ofResampling.NEAREST
(use nearest-neighbour),Resampling.BILINEAR
(linear interpolation in a 2×2 environment), orResampling.BICUBIC
(cubic spline interpolation in a 4×4 environment). If omitted, or if the image has mode “1” or “P”, it is set toResampling.NEAREST
. See Filters.expand
: Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.center
: Optional center of rotation (a 2-tuple). Origin is the upper left corner. Default is the center of the image.translate
: An optional post-rotate translation (a 2-tuple).fillcolor
: An optional color for areas outside the rotated image.
RETURNS:
- An
Image
object.
References
- https://docs.opencv.org/4.x/d2/de8/group__core__array.html
- https://docs.opencv.org/4.x/da/d54/group__imgproc__transform.html
- https://pillow.readthedocs.io/en/stable/reference/Image.html
- https://note.nkmk.me/en/python-pillow-rotate/
- https://pyimagesearch.com/2017/01/02/rotate-images-correctly-with-opencv-and-python/