Python uses opencv to cut images in batches,

Source: Internet
Author: User

Python uses opencv to cut images in batches,

In the previous article, we introducedImplementation of image processing and Feature Extraction in pythonHere, let's take a look at Python's batch cropping of images through OpenCV, as detailed below.

For image processing, you need to modify the image size in large batches for training samples. Therefore, this program uses opencv to cut images in large batches.

Import cv2import osdef cutimage (dir, suffix): for root, dirs, files in OS. walk (dir): for file in files: filepath = OS. path. join (root, file) filesuffix = OS. path. splitext (filepath) [1] [1:] if filesuffix in suffix: # Find the file name with the specified suffix, such as ["jpg", png], image = cv2.imread (file) # opencv cut image # cv2.imshow (file, image) dim = (242,200) # specify the size w * h resized = cv2.resize (image, dim, interpolation = cv2.INTER _ AREA) # The interpolation method used here is INTER_LINEAR # cv2.imshow ("resize: % s" % file, resized) cv2.imwrite (".. /cv/% s "% file, resized) # Save the file cv2.waitKey (0) # exit suffix = [" jpg "] dir = '. 'cutimage (dir, suffix)

Some values need to be changed by yourself, such as the Save path and save name.

Summary

The preceding section describes how to use python to cut images in batches using opencv. It is helpful to you. Interested friends can continue to refer to this site:

Python image operations

Several simple games for basic python exercises

Python-based facial recognition code

If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.