Batch crop a picture python

"" "With Pythonp Batch crop a picture" ""

from PIL import Image
import matplotlib.pyplot as plt
import os

# Define the path to be batch cropped image address
IMAGE_INPUT_PATH = 'D: / 2_Class'
#define cropped image storage address
IMAGE_OUTPUT_PATH = 'D: / 2_Class [0]'
# Custom Crop image left, upper right, the pixel coordinates of the
BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN = 130, 180, 600, 400

each_image in the os.listdir for (IMAGE_INPUT_PATH):
# full path to each image
image_input_fullname = IMAGE_INPUT_PATH + '/' + each_image
# libraries open the PIL each image
IMG = Image.open (image_input_fullname)
plt.figure ( "image_input_fullname")
PLT .subplot (. 1, 2,. 1)
plt.imshow (IMG)
plt.axis ( 'OFF')
Print (img.format, img.size, img.mode)
# returns a rectangular area from the original image, a region 4 tuple defines the upper left coordinates of the lower right pixel
Box = (BOX_LEFT, BOX_UP, BOX_RIGHT + BOX_LEFT, BOX_DOWN BOX_UP +)
# roi performed crop
roi_area = img.crop (Box)
plt.subplot (. 1, 2, 2)

plt.imshow (roi_area)
plt.axis ( 'OFF')
Print (roi_area.format, roi_area.size, roi_area.mode)
plt.show ()
# of each path name + cropped image after
image_output_fullname = IMAGE_OUTPUT_PATH + "/" each_image +
# storage cropped image obtained
roi_area.save (image_output_fullname)
Print ( '{0} DONE crop Allows you.'. the format (each_image))

Guess you like

Origin www.cnblogs.com/elitphil/p/11722427.html