简体   繁体   中英

Python: Pixel values in image display?

Using any of the numpy, scikit-image libraries, I can easily load and display an image as an ndarray. However, I'd like some sort of display where I can move the cursor around the image, and see the indices of the current pixel, and its gray (or RGB) values, for example: (213,47: 178) or (213,47: 122,10,205) - these values of course constantly changing as the cursor moves.

Is there an easy way to do this? Oh: I'm using Linux, but my students will be using Windows, so I'm hoping for a cross-platform solution.

I am using tkinter for displaying and PIL for reading any image kind. It is a veeerryyy slow solution.

def pil2tkinter_image(img_path, master = None, *args, **kw):
    import tkinter, PIL.Image
    img = PIL.Image.open(img_path)
    x0, y0, width, height = img.getbbox()
    l = []
    for y in range(y0, height):
        l.append('{')
        for x in range(x0, width):
            l.append('#%02X%02X%02X' % img.getpixel((x, y))[:3])
        l.append('}')
    data = ' '.join(l) # slow part
    pi = tkinter.PhotoImage(master = master, *args, **kw)
    pi.put(data, (0,0))
    return pi

The function pil2tkinter_image takes a path to an image of any kind, png, bmp, jpg, gif and creates a tkinter PhotoImage out of it.

You can embed this Image in a tkinter Widget and use mouse events to read the pixel values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM