{"id":12964,"date":"2023-05-09T10:00:00","date_gmt":"2023-05-09T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=12964"},"modified":"2023-05-08T20:51:35","modified_gmt":"2023-05-08T11:51:35","slug":"rotating-and-cropping-the-images-in-python-using-opencv","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/12964","title":{"rendered":"Rotating and cropping the images in Python using opencv"},"content":{"rendered":"\n<p>Today, I would like to share how to rotate and crop the images in Python using opencv.<\/p>\n\n\n\n<p>Rotating and cropping are common image processing techniques used to manipulate digital images. Python OpenCV is a powerful library for image processing that provides numerous tools for image manipulation. In this tutorial, we will learn how to rotate and crop images using Python OpenCV.<\/p>\n\n\n\n<p>1. Installing OpenCV<\/p>\n\n\n\n<p>First, we need to install OpenCV in our system. If you haven&#8217;t installed OpenCV yet, you can install it using pip:<\/p>\n\n\n\n<p><code>pip install opencv-python<\/code><\/p>\n\n\n\n<p>2. Loading an Image<\/p>\n\n\n\n<p>After installing OpenCV, we can load an image using the imread() function. The imread() function takes the image file path as an argument and returns an array representing the image.<\/p>\n\n\n\n<p><code>import cv2<\/code><\/p>\n\n\n\n<p><code># Load image<\/code><\/p>\n\n\n\n<p><code>image = cv2.imread('path\/to\/image.jpg')<\/code><\/p>\n\n\n\n<p>3. Rotating an Image<\/p>\n\n\n\n<p>Rotating an image involves changing the orientation of the image. We can use the cv2.rotate() function to rotate an image. The cv2.rotate() function takes three arguments: the image, the rotation type, and the angle of rotation.<\/p>\n\n\n\n<p><code># Rotate image<\/code><\/p>\n\n\n\n<p><code>rotated_image = cv2.rotate(image, cv2.cv2.ROTATE_90_CLOCKWISE)<\/code><\/p>\n\n\n\n<p>In the above example, we have rotated the image 90 degrees clockwise.<\/p>\n\n\n\n<p>4. Cropping an Image<\/p>\n\n\n\n<p>Cropping an image involves selecting a portion of the image and discarding the rest. We can use array slicing to crop an image. The array slicing notation is [start_row:end_row, start_column:end_column].<\/p>\n\n\n\n<p># Crop image<\/p>\n\n\n\n<p><code>cropped_image = image[start_row:end_row, start_column:end_column]<\/code><\/p>\n\n\n\n<p>In the above example, we have cropped the image from start_row to end_row and start_column to end_column.<\/p>\n\n\n\n<p>5. Displaying Images<\/p>\n\n\n\n<p>After rotating or cropping an image, we can display the images using the cv2.imshow() function. The cv2.imshow() function takes two arguments: the name of the window and the image.<\/p>\n\n\n\n<p># Display images<\/p>\n\n\n\n<p><code>cv2.imshow('Original Image', image)<\/code><\/p>\n\n\n\n<p><code>cv2.imshow('Rotated Image', rotated_image)<\/code><\/p>\n\n\n\n<p><code>cv2.imshow('Cropped Image', cropped_image)<\/code><\/p>\n\n\n\n<p><code># Wait for a key press and close all windows<\/code><\/p>\n\n\n\n<p><code>cv2.waitKey(0)<\/code><\/p>\n\n\n\n<p><code>cv2.destroyAllWindows()<\/code><\/p>\n\n\n\n<p>6. Saving an Image<\/p>\n\n\n\n<p>We can save the rotated or cropped image using the cv2.imwrite() function. The cv2.imwrite() function takes two arguments: the name of the file and the image.<\/p>\n\n\n\n<p># Save image<\/p>\n\n\n\n<p><code>cv2.imwrite('path\/to\/saved\/image.jpg', rotated_image)<\/code><\/p>\n\n\n\n<p>In the above example, we have saved the rotated image to a file named &#8216;saved_image.jpg&#8217; in the specified path.<\/p>\n\n\n\n<p>Conclusion<\/p>\n\n\n\n<p>In this tutorial, we have learned how to rotate and crop images using Python OpenCV. We have also learned how to display and save images. These are just a few of the many image processing techniques that can be performed using OpenCV. With OpenCV, we can perform a wide range of image processing tasks, from simple operations like cropping and rotating to more complex operations like edge detection and object recognition.<\/p>\n\n\n\n<p>This is all for now. Hope you enjoy that.<\/p>\n\n\n\n<p>By Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12964\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/12964\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Rotating and cropping the images in Python using opencv\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12964\" data-text=\"Rotating and cropping the images in Python using opencv\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12964\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12964\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today, I would like to share how to rotate and crop the images in Python using opencv. Rotating and cropping a [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12964"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=12964"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12964\/revisions"}],"predecessor-version":[{"id":12966,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12964\/revisions\/12966"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=12964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=12964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=12964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}