在python中将图像转换为csv文件

Neb*_*ula 1 python csv image

我已将我的图像转换为csv文件,它就像一个矩阵,但我希望它是一行.如何将数据集中的所有图像转换为csv文件(每个图像为一行).

这是我用过的代码:

from PIL import Image
import numpy as np
import os, os.path, time

format='.jpg'
myDir = "Lotus1"
def createFileList(myDir, format='.jpg'):
    fileList = []
    print(myDir)
    for root, dirs, files in os.walk(myDir, topdown=False):
            for name in files:
               if name.endswith(format):
                  fullName = os.path.join(root, name)
                  fileList.append(fullName)
                  return fileList

fileList = createFileList(myDir)
fileFormat='.jpg'
for fileFormat in fileList:
 format = '.jpg'
 # get original image parameters...
 width, height = fileList.size
 format = fileList.format
 mode = fileList.mode
 # Make image Greyscale
 img_grey = fileList.convert('L')
 # Save Greyscale values
 value = np.asarray(fileList.getdata(),dtype=np.float64).reshape((fileList.size[1],fileList.size[0]))
 np.savetxt("img_pixels.csv", value, delimiter=',')
Run Code Online (Sandbox Code Playgroud)

输入:http: //uupload.ir/files/pto0_lotus1_1.jpg

输出:http://uupload.ir/files/huwh_output.png

Pam*_*Pam 9

从你的问题来看,我想你想知道numpy.flatten().你想要添加

value = value.flatten()
Run Code Online (Sandbox Code Playgroud)

就在你的np.savetxt电话之前.它会将数组展平为只有一个维度,然后它应该打印成一行.

你的问题的其余部分不清楚它意味着你有一个充满jpeg图像的目录,你想要一种方法来读取所有.首先,获取文件列表:

def createFileList(myDir, format='.jpg'):
fileList = []
print(myDir)
for root, dirs, files in os.walk(myDir, topdown=False):
    for name in files:
        if name.endswith(format):
            fullName = os.path.join(root, name)
            fileList.append(fullName)
return fileList
Run Code Online (Sandbox Code Playgroud)

用你的代码环绕 for fileName in fileList:

编辑添加完整的示例 请注意,我已经使用了csv编写器并将你的float64更改为整数(这应该没问题,因为像素数据是0-255

from PIL import Image
import numpy as np
import sys
import os
import csv

#Useful function
def createFileList(myDir, format='.jpg'):
fileList = []
print(myDir)
for root, dirs, files in os.walk(myDir, topdown=False):
    for name in files:
        if name.endswith(format):
            fullName = os.path.join(root, name)
            fileList.append(fullName)
return fileList

# load the original image
myFileList = createFileList('path/to/directory/')

for file in fileList:
    print(file)
    img_file = Image.open(file)
    # 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.png')
    #img_grey.show()

    # Save Greyscale values
    value = np.asarray(img_grey.getdata(), dtype=np.int).reshape((img_grey.size[1], img_grey.size[0]))
    value = value.flatten()
    print(value)
    with open("img_pixels.csv", 'a') as f:
        writer = csv.writer(f)
        writer.writerow(value)
Run Code Online (Sandbox Code Playgroud)


3ya*_*bos 5

您如何将图像转换为 2D numpy 数组,然后将它们写为.csv扩展名和,作为分隔符的txt 文件?

也许您可以使用如下代码:

np.savetxt('np.csv', image, delimiter=',')
Run Code Online (Sandbox Code Playgroud)


小智 5

import numpy as np
import cv2
import os

IMG_DIR = '/home/kushal/Documents/opencv_tutorials/image_reading/dataset'

for img in os.listdir(IMG_DIR):
        img_array = cv2.imread(os.path.join(IMG_DIR,img), cv2.IMREAD_GRAYSCALE)

        img_array = (img_array.flatten())

        img_array  = img_array.reshape(-1, 1).T

        print(img_array)

        with open('output.csv', 'ab') as f:

            np.savetxt(f, img_array, delimiter=",")
Run Code Online (Sandbox Code Playgroud)