This is the first post. Leave it for your notes.
--Python beginner --People who want to crop a photo but don't know how to do it --For those who want to crop multiple photos at once
Implementation of the source that cuts out and saves only the lower right part of multiple photos. It's hard to understand even if I write only words, so it's a cropped photo immediately.
trim_photo.py
#import
import os, glob
import cv2
#Photo folder to be cropped
importPath = r"C:\Users\User\Desktop\photo"
#File storage location after trimming
outputPath = r"C:\Users\User\Desktop\output"
#File name for saving
fileName = "trim_photo"
#Crop photos in the target folder with a loop
i = 1
for infile in glob.glob( os.path.join(importPath, '*.png') ): #Specify only png format
#File name generation
imgname= fileName + str(i)
#File reading
img = cv2.imread(infile)
#Specify the trimming area and cut out
img = img[0 : 200, 0 : 300]
cv2.imwrite(outputPath + imgname + '.png', img)
i = i +1
The trimming area is specified in the following places.
#Specify the trimming area and cut out
img = img[0 : 200, 0 : 300]
In the above case, it is specified to cut out 200 pixels from the bottom and 300 pixels from the right.
It's nice to be able to implement Python easily.
Recommended Posts