【发布时间】:2021-06-03 03:52:07
【问题描述】:
我需要使用 Python 从给定的图像文件中提取(x, y) 坐标。
我希望数据采用以下格式
pixel# x y
1 0 0
2 1 0
.
.
301 0 1
302 1 1
.
.
XX,000 299 199
对于我拥有的任何大小的图像文件。
目前我正在使用这个脚本:
from PIL import Image
import numpy as np
import sys
# load the original image
img_file = Image.open("inputimage_005004.jpg")
img_file.show()
# get original image parameters...
width, height = img_file.size
format = img_file.format
mode = img_file.mode
# Make image Greyscale
img_grey = img_file.convert('L')
img_grey.save('result.jpg')
img_grey.show()
value = np.asarray(img_grey.getdata(),dtype=img_grey.float64).reshape((img_grey.size[1],img_grey.size[0]))
np.savetxt("outputdata.csv", value, delimiter=',')
但是我在倒数第二行遇到错误
value = np.asarray(...)
错误是:
AttributeError: 'Image' object has no attribute 'float64`
另外,我希望输出类似于这个 StackOverflow 问题中的输出:
Extract x,y coordinates of each pixel from an image in Python.
如何将我当前的脚本与上述链接结合并修复我当前的错误?
编辑:
错误已修复,但我没有正确获取图像文件的(x, y) 坐标。它给了我一个很长的数字,像这样:
2.270000000000000000e+02,2.370000000000000000e+02,2.270000000000000000e+02,2.320000000000000000e+02,2.330000000000000000e+02,...
但是,我希望它采用如上所示的格式。我怎样才能做到这一点?
【问题讨论】:
-
在倒数第二行尝试
dtype=np.float64而不是dtype=img_grey.float64 -
我做到了,它修复了错误,谢谢!但是输出到 outputdata.csv 文件中的数据不是我想要的。我怎样才能格式化它,以便我可以获得与我发布的链接类似的输出? @AnuragDabas
标签: python image numpy save python-imaging-library