I made a function to crop the image of python openCV, so please use it.

When cropping an image with python's openCV, you have to specify the image index as below:


import cv2

img = cv2.imread("hoge.jpg ")

trimmedImg = img[0 : 50, 0: 50]

With this, you can't tell where you are trimming just by looking at it. (I was) So I made a trimming function like cv2.rectangle (). Please use it.

trim.py


import cv2

def trim(img, pt1, pt2):
    return img[pt1[1]:pt2[1], pt1[0]:pt2[0]]

Enter pt1 and pt2 like this. pts.jpg

Made it easier to use

trim.py


import cv2

def trim(img, position, width, height):
    x, y = position
    return img[y:y+height, x:x+width]

Or

trim.py


import cv2

def trim(img, x, y, width, height):
    return img[y:y+height, x:x+width]

The input looks like this. xy2.jpg

Recommended Posts

I made a function to crop the image of python openCV, so please use it.
I made a function to see the movement of a two-dimensional array (Python)
I want to crop the image along the contour instead of the rectangle [python OpenCV]
I made a function to check the model of DCGAN
How to crop the lower right part of the image with Python OpenCV
I made a program to check the size of a file in Python
Python: I want to measure the processing time of a function neatly
I made a script to record the active window using win32gui of Python
I wanted to use the find module of Ansible2, but it took some time, so make a note
A Python beginner made a chat bot, so I tried to summarize how to make it.
Add a function to tell the weather of today to slack bot (made by python)
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I made a dot picture of the image of Irasutoya. (part1)
I made a dot picture of the image of Irasutoya. (part2)
[Python] I made an image viewer with a simple sorting function.
[Python3] Define a decorator to measure the execution time of a function
[Super basics of Python] I learned the basics of the basics, so I summarized it briefly.
I made a segment tree with python, so I will introduce it
[Python] A simple function to find the center coordinates of a circle
I tried to make an image similarity function with Python + OpenCV
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
[Python] I made a decorator that doesn't seem to have any use.
[Python] Explains how to use the range function with a concrete example
I made a GAN with Keras, so I made a video of the learning process.
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
The story of making a tool to load an image with Python ⇒ save it as another name
I tried "differentiating" the image with Python + OpenCV
How to crop an image with Python + OpenCV
I tried to correct the keystone of the image
I tried "binarizing" the image with Python + OpenCV
I tried using the image filter of OpenCV
I want to use the activation function Mish
[Python + OpenCV] Whiten the transparent part of the image
[You have to know it! ] I tried to set up a Python environment profitably by making full use of the privileges of university students.
I made a tool to estimate the execution time of cron (+ PyPI debut)
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
I want to use Python in the environment of pyenv + pipenv on Windows 10
I made a tool to notify Slack of Connpass events and made it Terraform
Use Pillow to make the image transparent and overlay only part of it
I failed to install django with pip, so a reminder of the solution
I made an appdo command to execute a command in the context of the app
I made a tool to automatically back up the metadata of the Salesforce organization
[Python] I tried to get the type name as a string from the type function
I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
The tree.plot_tree of scikit-learn was very easy and convenient, so I tried to summarize how to use it easily.
How to save the feature point information of an image in a file and use it for matching
I wanted to know the number of lines in multiple files, so I tried to get it with a command
[OpenCV / Python] I tried image analysis of cells with OpenCV
I made a VM that runs OpenCV for Python
I made a Python module to translate comment outs
I wanted to use the Python library from MATLAB
I made a command to markdown the table clipboard
I made a python library to do rolling rank
I want to use the R dataset in python
Python OpenCV tried to display the image in text.
Python Note: The mystery of assigning a variable to a variable
I tried to summarize the string operations of Python
I use python but I don't know the class well, so I will do a tutorial
[Python] I made an app to practice the subtle voice distinction of English words.