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!