Search code examples
pythonpython-3.ximageimage-processingtiff

How to putpixel for multiple pixels


Currently, i'm visualizing the current pixel intensity value on very specific locations of 50 different frames of a tiff image. To do so, im printing out the same coordinate values for all 50 frames, everything works perfectly. Though, to make sure that i'm looking at the right pixels, i decided to turn them into black colors, but i get the following error described in the title.

TypeError: Invalid shape (50, 128, 160) for image data

at Line

imgplot = plt.imshow(imageList)

The images they are tiff format, but are split into 50 frames, for instance

What i am doing is:

from os import listdir
from PIL import Image as PImage

def loadImages(path):

    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        img = PImage.open(path + image)
        loadedImages.append(img)


    return loadedImages



imgs = loadImages('C:/Dataset/Frames/')

for img in imgs:

    imgplot = plt.imshow(img)
    img.putpixel((45, 100), (0))
    img.putpixel((45, 80), (0))
    img.putpixel((50, 65), (0))
    img.putpixel((50, 110), (0))
    img.putpixel((40, 110), (0))
    img.putpixel((35, 90), (0))
    img.putpixel((25, 90), (0))
    img.putpixel((25, 110), (0))
    img.putpixel((64, 89), (0))
    img.putpixel((25, 100), (0))
    img.putpixel((40, 65), (0))
    img.putpixel((65, 60), (0))
    img.putpixel((65, 120), (0))
    img.putpixel((82, 75), (0))
    img.putpixel((82, 105), (0))
    img.putpixel((78, 88), (0))
    img.putpixel((110, 90), (0))
    img.putpixel((90, 89), (0))
    img.putpixel((100, 65), (0))
    img.putpixel((100, 110), (0))
    plt.show()

What i want to do basically, is just change the values in any way possible for the values in these constant coordinates for every image inside a folder.


Solution

  • I'm not sure I entirely understand your question, but it looks like you are trying to check, change, and confirm pixel values at x/y coordinates.

    I would recommend converting your image to a numpy array. You can do so with:

    import numpy as np
    arr = np.array(img)
    

    Now you can access pixels as if you were indexing into an array (or using numpy's fancy indexing).

    # check initial value
    print(arr[0,0])
    # prints (213, 147, 69) or whatever
    
    # set new value
    arr[0,0] = (42, 42, 42)
    
    # confirm new value worked
    print(arr[0,0])
    # prints (42, 42, 42)
    

    NOTE: numpy sets up your array in height/width order so you would access your array via y/x instead of x/y. You can confirm the shape with arr.shape

    You can also take slices of the array. For example, if you wanted the top/left 50 pixels, you could:

    sub_arr = arr[0:50, 0:50]
    

    EDIT: looks like you just want to pull pixel values and put them in a vector. You could do something like:

    # list of coordinates to extract IN Y/X FORMAT 
    coord_list = [[100, 45], [80, 45], ... ]
    
    # not really sure what your feature vector looks like, just using a list
    feat_vec = []
    
    for img in imgs:
        # list for per-image features
        img_feats = []
        for coords in coord_list:
            img_feats.append(img[coords[0], coords[1]])
        feat_vec.append(img_feats)