How to crop an image vertically into two equal image sizes
So I have an 800 x 600 image that I want to slice vertically into two equally sized images using OpenCV 3.1.0. This means that at the end of the cut, I have to have two images of 400 x 600 each and be stored in their own PIL variables.
Here's an illustration:
Thank.
EDIT: I want the most efficient solution, so if this solution uses numpy splicing or something, go for it.
+3
source to share
1 answer
You can try the following code, which will create two numpy.ndarray
that you can easily display or write to new files.
from scipy import misc
# Read the image
img = misc.imread("face.png")
height, width, _ = img.shape
# Cut the image in half
width_cutoff = width // 2
s1 = img[:, :width_cutoff, :]
s2 = img[:, width_cutoff:, :]
# Save each half
misc.imsave("face1.png", s1)
misc.imsave("face2.png", s2)
The file face.png
is an example and should be replaced with your own image file.
+4
source to share