Home How to crop the Image using Python PIL or CV?
Reply: 2

How to crop the Image using Python PIL or CV?

user1464878
1#
user1464878 Published in 2018-02-14 08:18:58Z

My all images have Region of interest which is bounded by doted white lines. I want to crop only those portion.

I got one idea that find minimum(x,y) which is top left corner and find maximum(x,y) which is bottom right corner and crop the area?

We can get Pixel value from PIL library

SparRow
2#
SparRow Reply to 2018-02-14 08:43:08Z

It's easy to crop images using opencv2. Try this link

http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html

ball = img[280:340, 330:390]

since Image is a 2D array you can crop with above line.

CodeCupboard
3#
CodeCupboard Reply to 2018-02-14 08:45:36Z

To read a pixel value in PIL

from PIL import Image
im = Image.open(imagename)
pix = im.load()
print (pix[0,0])

This will return the first pixel in the first row.

If you wanted to read all pixels you could do something like this.

x, y = im.size

for i in x:
    for j in y:
        print (pix[i,j])
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331867 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO