如何为多个像素putpixel
How to putpixel for multiple pixels
目前,我正在可视化 tiff 图像的 50 个不同帧的非常特定位置上的当前像素强度值。为此,我为所有 50 帧打印出相同的坐标值,一切正常。虽然,为了确保我看到的是正确的像素,我决定将它们变成黑色,但我得到了标题中描述的以下错误。
TypeError: Invalid shape (50, 128, 160) for image data
在第
行
imgplot = plt.imshow(imageList)
图片是tiff格式,但是分割成50帧,例如
我正在做的是:
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()
基本上我想做的就是以任何可能的方式更改文件夹中每个图像的这些常量坐标中的值。
我不确定我是否完全理解您的问题,但看起来您正在尝试检查、更改和确认 x/y 坐标处的像素值。
我建议将您的图像转换为 numpy 数组。您可以这样做:
import numpy as np
arr = np.array(img)
现在您可以像索引数组一样访问像素(或使用 numpy 的高级索引)。
# 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)
注意:numpy 按 height/width 顺序设置数组,因此您可以通过 y/x
而不是 x/y
访问数组。您可以使用 arr.shape
确认形状
您也可以获取数组的切片。例如,如果您想要 top/left 50 像素,您可以:
sub_arr = arr[0:50, 0:50]
编辑:看起来您只想提取像素值并将它们放入向量中。你可以这样做:
# 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)
目前,我正在可视化 tiff 图像的 50 个不同帧的非常特定位置上的当前像素强度值。为此,我为所有 50 帧打印出相同的坐标值,一切正常。虽然,为了确保我看到的是正确的像素,我决定将它们变成黑色,但我得到了标题中描述的以下错误。
TypeError: Invalid shape (50, 128, 160) for image data
在第
行imgplot = plt.imshow(imageList)
图片是tiff格式,但是分割成50帧,例如
我正在做的是:
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()
基本上我想做的就是以任何可能的方式更改文件夹中每个图像的这些常量坐标中的值。
我不确定我是否完全理解您的问题,但看起来您正在尝试检查、更改和确认 x/y 坐标处的像素值。
我建议将您的图像转换为 numpy 数组。您可以这样做:
import numpy as np
arr = np.array(img)
现在您可以像索引数组一样访问像素(或使用 numpy 的高级索引)。
# 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)
注意:numpy 按 height/width 顺序设置数组,因此您可以通过 y/x
而不是 x/y
访问数组。您可以使用 arr.shape
您也可以获取数组的切片。例如,如果您想要 top/left 50 像素,您可以:
sub_arr = arr[0:50, 0:50]
编辑:看起来您只想提取像素值并将它们放入向量中。你可以这样做:
# 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)