Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cutting one image into multiple images using the Python Image Library

I need to cut this image into three parts using PIL and pick the middle part. How do I do it?

http://thedilbertstore.com/images/periodic_content/dilbert/dt110507dhct.jpg

like image 307
un33k Avatar asked May 19 '11 13:05

un33k


People also ask

How do I split an image into multiple images in Python?

Image. split() method is used to split the image into individual bands. This method returns a tuple of individual image bands from an image.

How do I crop multiple images at once in Python?

You can resize multiple images in Python with the awesome PIL library and a small help of the os (operating system) library. By using os. listdir() function you can read all the file names in a directory. After that, all you have to do is to create a for loop to open, resize and save each image in the directory.


1 Answers

If the boxes are not known on before hand I would run a simple edge finding filter over the image (both x and y directions) to find the boundaries of the box.

A simple approach would be:

  1. Run horizontal edge filter over image. You now have an image where each pixel describes the changes in intensity left and right of that pixel. I.e. it will "find" vertical lines.
  2. For each column in the horizontal-edge-image get the average absolute magnitude of its rows. In the resulting 1 x WIDTH sized array you will find the vertical lines at the positions of highest value. Since the lines are more than one pixel wide yo might have to be a bit clever here.
  3. Do the same for the other axis to find the horizontal lines.

You could do some pre processing by first extracting only pixels that are black (or near black) if you believe that the borders of the boxes will always be black. But I doubt it'd be necessary since the above method should be very stable.

like image 62
Hannes Ovrén Avatar answered Oct 01 '22 07:10

Hannes Ovrén