python - 如何为多个像素放置像素
问题描述
目前,我正在可视化 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 按高度/宽度顺序设置您的数组,因此您可以通过y/x
而不是x/y
. 您可以确认形状arr.shape
您还可以获取数组的切片。例如,如果您想要顶部/左侧 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)
推荐阅读
- reactjs - 为什么我用我的对象传播覆盖我所有的状态值?
- javascript - 当我们需要返回一个值时,为什么我们需要在递归中“返回”?
- react-native - 有什么方法可以检测 android 电视 HDMI 电缆是否连接或未在世博会反应原生?
- gcc - `jz` 到 RIP 相对内存位置会产生操作数不匹配 (GCC)
- powershell - 如何仅从 PowerCLI 获取 currentVersion?
- javascript - Chrome DevTools 中的“暂停脚本执行”未按预期工作
- azure - Azure Blockchain Workbench REST API 返回 204 无内容
- ruby-on-rails - 未定义的方法“simple_form_for”
- php - ERR_TOO_MANY_REDIRECTS laravel 管理/登录页面
- amazon-web-services - EC2 实例状态