Python Forum
grayscale image processing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: grayscale image processing (/thread-5849.html)

Pages: 1 2


grayscale image processing - zyb1003 - Oct-24-2017

Hello guys,

I am rookie to python, and have trouble in image processing.

Now I have an image showing a beam spot, only the beam spot is white and the rest part is black. The beam shape is oval. I changed the image to grayscale already. Now I want to know which row has the longest beam length and how many pixels on white spot in that row.

How should I do this using PIL?

Thanks


RE: grayscale image processing - Larz60+ - Oct-24-2017

You can do a lot with pillow, see: https://auth0.com/blog/image-processing-in-python-with-pillow/
but I don't believe there is any code there for image processing

for that you want something like scikit-image see: http://scikit-image.org/


RE: grayscale image processing - Mekire - Oct-24-2017

OpenCV is a really strong choice for image processing:
http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html


RE: grayscale image processing - zyb1003 - Oct-24-2017

(Oct-24-2017, 07:43 PM)Larz60+ Wrote: You can do a lot with pillow, see: https://auth0.com/blog/image-processing-in-python-with-pillow/
but I don't believe there is any code there for image processing

for that you want something like scikit-image see: http://scikit-image.org/

Hello, thanks for the reply. I know that the Scikit might be a more powerful tool for image processing, but I still cannot find a proper way to achieve my goal. Could you please solve it more specifically?

Thanks

(Oct-24-2017, 07:44 PM)Mekire Wrote: OpenCV is a really strong choice for image processing:
http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html
Hello,

Thanks for reply, I know OpenCV is a more powerful tool for image processing, and of course it's more complicated than pillow, but I think pillow is also a powerful tool and I think it's enough for me. However, now what confused me is how can I achieve my goal. Thanks


RE: grayscale image processing - Mekire - Oct-24-2017

We'll need to see what code you have so far.  As long as you can access the images as arrays it shouldn't be too hard.  Not sure if PIL uses numpy or not but if it does it will make things easier.


RE: grayscale image processing - zyb1003 - Oct-24-2017

(Oct-24-2017, 09:19 PM)Mekire Wrote: We'll need to see what code you have so far.  As long as you can access the images as arrays it shouldn't be too hard.  Not sure if PIL uses numpy or not but if it does it will make things easier.

from PIL import Image
from pylab import *
im=array(Image.open('0.1v.jpg').convert('L'))
print(im)
So far, this is the code I have. Now I can read every pixel's grayscale value.


RE: grayscale image processing - Mekire - Oct-24-2017

If I understand what you are asking for,

1. import numpy
2. use numpy to find sums of each row in your array
3. return the max of these.

If your image is binary (just black and white) you should load as black and white rather than grayscale too.


RE: grayscale image processing - heiner55 - Oct-25-2017

from PIL import Image

img  = Image.open('grey.jpg')
width, height = img.size

for x in range(width):
    for y in range(height):
        pixel = img.getpixel((x,y))
        red   = pixel[0]
        green = pixel[1]
        blue  = pixel[2]
        grey  = (red + green + blue) // 3
        img.putpixel((x,y), (grey,grey,grey))

img.save("grey2.jpg", "jpeg")



RE: grayscale image processing - zyb1003 - Oct-25-2017

(Oct-25-2017, 06:25 AM)heiner55 Wrote:
from PIL import Image

img  = Image.open('grey.jpg')
width, height = img.size

for x in range(width):
    for y in range(height):
        pixel = img.getpixel((x,y))
        red   = pixel[0]
        green = pixel[1]
        blue  = pixel[2]
        grey  = (red + green + blue) // 3
        img.putpixel((x,y), (grey,grey,grey))

img.save("grey2.jpg", "jpeg")

hello, Thanks for reply. However, I don't understand your code that much. Why do you make grey=red+green+blue/3? What is this grey used for? And also, my aim is to find how many bright(greyscale value>200) pixels in a certain row which has most bright pixels.


RE: grayscale image processing - Mekire - Oct-25-2017

That other reply has nothing to do with what you asked and has the additional added benefit of being extremely slow.

As I said previously, convert your image to a binary image (not grayscale).

Then find the sums of all rows (a single numpy operation).
Then take the max of these sums (also a single numpy operation).