[PYTHON] Crop Numpy.ndarray and save it as an image

Image processing libraries such as OpenCV and Caffe often convert image images to Ndarray and handle them, but I was at a loss for a moment to cut this out as ndarray and save it as an image, so be careful.

image_crop.py


import numpy as np
import scipy as sp
import Image

#Image coordinates are stored in variables as shown below
# x:x-axis start coordinate
# y:y-axis start coordinate
# w:Number of pixes you want to crop from the x-axis
# h:Number of pixes you want to crop from the y-axis

#When using PIL
pic = Image.open(input_file_path)
imgar = np.asarray(pic.crop((x, y, x+w, y+h)))
sp.misc.imsave('test.jpg', imgar)

#ndarray If you want to handle it as it is
#PIL and x,Note that y is reversed
imgar = nd[y: y+h, x: x+w]
sp.misc.imsave('test.jpg', imgar)

Recommended Posts

Crop Numpy.ndarray and save it as an image
Grayscale image and save as csv
Save screenshot of [Python] [Windows] screen as an image
Get an image from a web page and resize it
Save the input video from the capture board as an image
Take an image with Pepper and display it on your tablet
A python program that resizes a video and turns it into an image
Start the webcam to take a still image and save it locally
The story of making a tool to load an image with Python ⇒ save it as another name
[python] Send the image captured from the webcam to the server and save it
How to save the feature point information of an image in a file and use it for matching
I made an image classification model and tried to move it on mobile
How to crop an image with Python + OpenCV
Sort Fashion-MNIST data and save as PNG file
Build an image classification model explosively with Azure Custom Vision and implement it with Flask
Save the graph drawn by pyqtgraph to an image
POST the image with json and receive it with flask