Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.5.1从像素访问对象到CSV数据集_Python_Image_Csv_Python Imaging Library_Pixels - Fatal编程技术网

Python 3.5.1从像素访问对象到CSV数据集

Python 3.5.1从像素访问对象到CSV数据集,python,image,csv,python-imaging-library,pixels,Python,Image,Csv,Python Imaging Library,Pixels,我正在用Python做一个项目,关于拍摄图像,然后用统计方法分析这些图像 我(非常)不熟悉Python,因此在处理数据时遇到了问题 我想将我的图像数据从Python导出为csv格式,以便在另一个程序中进行分析 因此,如果第一行中的第一个单元格具有RGB值[100,50200],那么我希望将其转换为[100050200] 我想我需要设置一个循环遍历所有单元格。但如何做到这一点,以及如何使R、G和B的每一个值都是3位数——我不知道 from PIL import Image im=Image.ope

我正在用Python做一个项目,关于拍摄图像,然后用统计方法分析这些图像

我(非常)不熟悉Python,因此在处理数据时遇到了问题

我想将我的图像数据从Python导出为csv格式,以便在另一个程序中进行分析

因此,如果第一行中的第一个单元格具有RGB值[100,50200],那么我希望将其转换为[100050200]

我想我需要设置一个循环遍历所有单元格。但如何做到这一点,以及如何使R、G和B的每一个值都是3位数——我不知道

from PIL import Image
im=Image.open('path.jpeg')
pixels=list(im.getdata())

然后我就迷路了。非常感谢您的帮助

以下是迈克尔·斯坦尼克的回答

好的,你的覆盆子会拍摄你想要转换成CSV文件的照片。其工作方式如下:

from PIL import Image
import numpy
img=Image.open(pathtoimage)
imgarray=numpy.array(img)
array([[[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],
       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],
       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]]])
使用PIL打开文件,它返回一个img对象,然后可以将其转换为numpy数组。 阵列将是三维的,因此它将如下所示:

from PIL import Image
import numpy
img=Image.open(pathtoimage)
imgarray=numpy.array(img)
array([[[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],
       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],
       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]]])
然后可以使用以下代码:

for row in imgarray:
    outputstring=""
    for column in row:
        valuestring=""
        for value in column:
            if value < 10:
                valuestring+="00"+str(value)
            elif value < 100:
                valuestring+="0"+str(value)
            else:
                valuestring+=str(value)
       outputstring+=valuestring+";"
    print(outputstring[:-1]) #excluding the last letter, because its a ;
对于imgarray中的行:
outputstring=“”
对于行中的列:
valuestring=“”
对于列中的值:
如果值<10:
valuestring+=“00”+str(值)
elif值<100:
valuestring+=“0”+str(值)
其他:
valuestring+=str(值)
outputstring+=valuestring+“;”
打印(outputstring[:-1])#不包括最后一个字母,因为它是a;
这将为您提供以下结果:

001002003;001002003;001002003 001002003;001002003;001002003 001002003;001002003;001002003