How to crop the lower right part of the image with Python OpenCV

Introduction

This is the first post. Leave it for your notes.

Target person

--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

What i did

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.

Before trimming

lena.jpg

After trimming

trim_photo1.jpg

Source code

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

Trimming area specified part

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.

Finally

It's nice to be able to implement Python easily.

Recommended Posts

How to crop the lower right part of the image with Python OpenCV
How to crop an image with Python + OpenCV
[Python + OpenCV] Whiten the transparent part of the image
I want to crop the image along the contour instead of the rectangle [python OpenCV]
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I made a function to crop the image of python openCV, so please use it.
I tried "smoothing" the image with Python + OpenCV
Crop the image to rounded corners with pythonista
I tried "differentiating" the image with Python + OpenCV
Try to image the elevation data of the Geographical Survey Institute with Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Image recognition] How to read the result of automatic annotation with VoTT
How to specify attributes with Mock of python
I tried "binarizing" the image with Python + OpenCV
The easiest way to use OpenCV with python
How to measure execution time with Python Part 1
[Python] How to handle Japanese characters with openCV
How to measure execution time with Python Part 2
Note: How to get the last day of the month with python (added the first day of the month)
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Extract the table of image files with OneDrive & Python
How to get the number of digits in Python
How to scrape image data from flickr with python
[Introduction to Python] How to iterate with the range function?
How to upload with Heroku, Flask, Python, Git (Part 3)
[OpenCV / Python] I tried image analysis of cells with OpenCV
[Python] How to specify the download location with youtube-dl
Convert the image in .zip to PDF with Python
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
[Python] Summary of how to specify the color of the figure
[Python] How to rewrite the table style with python-pptx [python-pptx]
Python OpenCV tried to display the image in text.
Image processing with Python (Part 2)
Image editing with python OpenCV
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Try to measure the position of the object on the desk (real coordinate system) from the camera image with Python + OpenCV
How to identify the element with the smallest number of characters in a Python list?
[Note] How to write QR code and description in the same image with python
How to count the number of occurrences of each element in the list in Python with weight
The 15th offline real-time I tried to solve the problem of how to write with python
I tried to simulate how the infection spreads with Python
How to shuffle a part of a Python list (at random.shuffle)
[Ev3dev] How to display bmp image on LCD with python
[Part.2] Crawling with Python! Click the web page to move!
Try to automate the operation of network devices with Python
How to get into the python development environment with Vagrant
Estimate the attitude of AR markers with Python + OpenCV + drone
Ruby vs Python Performance. How to pick the right technology?
[Introduction to Python] How to get data with the listdir function
[Python] Easy reading of serial number image files with OpenCV
Get the source of the page to load infinitely with python.
Python: How to use async with
How to write offline real time I tried to solve the problem of F02 with Python
[Python] Using OpenCV with Python (Image transformation)
How to get the Python version
How to get started with Python
How to scrape stock prices of individual stocks from the Nikkei newspaper website with Python