Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Cut an Image Vertically into Two Equal Sized Images

So I have an 800 x 600 image that I want to cut vertically into two equally sized pictures using OpenCV 3.1.0. This means that at the end of the cut, I should have two images that are 400 x 600 each and are stored in their own PIL variables.

Here's an illustration:

Paper being cut into halves

Thank you.

EDIT: I want the most efficient solution so if that solution is using numpy splicing or something like that then go for it.

like image 768
Halp Avatar asked Jul 29 '17 03:07

Halp


People also ask

How do I crop a picture in exactly in half?

To split images in half in Photoshop, select the marquee tool by pressing M, then click and drag over half of your image to create a rectangular selection. With the selection active, right-click and select New Layer Via Cut. This will cut the image in half and place the selected half on a new layer.


1 Answers

You can try the following code which will create two numpy.ndarray instances which 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 face.png file is an example and needs to be replaced with your own image file.

like image 129
fsimkovic Avatar answered Oct 04 '22 05:10

fsimkovic