将具有像素值的csv文件转换为图像 [英] Convert csv file having pixel values into images

查看:31
本文介绍了将具有像素值的csv文件转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个


编辑:我检查了您的csv文件,像素不是整数值,而是浮点(正,负)值,因此您不能使用 uint8 .它必须是 float .

  pixels = np.array(pixels,dtype ='float') 

您可以将图像转换为 RGB 或灰度 L 进行保存

  image = image.convert('RGB')图片= image.convert('L') 

但是转换负值似乎有问题.

使用

  plt.imsave(文件名,像素) 

我得到了预期的结果


 将numpy导入为np导入csv从PIL导入图像导入matplotlib.pyplot作为plt计数器= dict()使用open('gen_image_wgan.csv')作为csv_file:csv_reader = csv.reader(csv_file)#跳过标题下一个(csv_reader)对于csv_reader中的行:像素=行[:-1]#不带标签像素= np.array(像素,dtype ='float')像素= pixel.reshape((28,28))标签=行[-1]如果标签不在柜台上:计数器[标签] = 0计数器[标签] + = 1filename ='{} {}.png'.format(label,counter [label])plt.imsave(文件名,像素)打印('保存:',文件名) 

I have a csv file, in which each row contains the pixel values for an image. Therefore, each column of a row contains pixel values, except for the last column, which contains the 'labels' for the image, which are words such as "coat", "dress", "sandal" etc. (Note: First row contains the column names and is not an image).

I wish to read these rows as images and save all of them (preferably sorted by label, such as 'dress1', 'dress2',....,'sandal1', 'sandal2',.., etc. for each of the four labels).

I tried the following approach for saving without label classification, but I am getting an error:

import numpy as np
import csv
import matplotlib.pyplot as plt
i=0
with open('myfile.csv', 'r') as csv_file:
      for data in csv.reader(csv_file):
      pixels = data[:]
      pixels = np.array(pixels, dtype='uint8')
      #Reshape the array into 28 x 28 array (As images are 28x28)
      pixels = pixels.reshape((28, 28)) 
      i +=1
      plt.savefig(str(i))

Looking for the most efficient way to go about this. Any inputs will be appreciated. Thanks!

解决方案

If you have name in last column then you have to convert without last element data[:-1]. And use last column in filenamename savefig( data[-1] + str(i) + '.jpg' ). Without extension it may not know what type of image to write.

You have to count i for every label separatelly - ie. using dict

i = dict() 
i[label] = 0 

# later 

i[label] += 1 
savefig( label + str(i[label]) + '.jpg' 

You can also use PIL/'pillowinstead ofmatplotlib` to write it.

from PIL import Image    

image = Image.fromarray(pixels)
image.save(filename)


import numpy as np
import csv
from PIL import Image    

counter = dict()

with open('myfile.csv') as csv_file:
    csv_reader = csv.reader(csv_file)

    # skip headers
    next(csv_reader)

    for row in csv.reader(csv_reader):

        pixels = row[:-1] # without label
        pixels = np.array(pixels, dtype='uint8')
        pixels = pixels.reshape((28, 28))
        image = Image.fromarray(pixels)

        label = row[-1]

        if label not in counter:
            counter[label] = 0
        counter[label] += 1

        filename = '{}{}.jpg'.format(label, counter[label])
        image.save(filename)

        print('saved:', filename)


EDIT: Example which show it with one row of data for those which whan to test it without downloading csv file.

import numpy as np
import csv
from PIL import Image

counter = dict()

row = [
        255,   0,   0,   0,   0,   0,  255,
          0, 255, 255, 255, 255, 255,    0,
          0, 255,   0, 255,   0, 255,    0,
          0, 255, 255, 255, 255, 255,    0,
          0, 255,   0,   0,   0, 255,    0,
          0, 255, 255, 255, 255, 255,    0,
        255,   0,   0,   0,   0,   0,  255,
        'face'
      ]

pixels = row[:-1]

pixels = np.array(pixels, dtype='uint8')
pixels = pixels.reshape((7, 7))
image = Image.fromarray(pixels)

label = row[-1]

if label not in counter:
    counter[label] = 0
counter[label] += 1

filename = '{}{}.png'.format(label, counter[label])

image.save(filename)

print('saved:', filename)

Result: face1.png:


EDIT: I checked your csv file and pixels are not integer values but float (positive, negative) values so you can't use uint8. It has to be float.

pixels = np.array(pixels, dtype='float')

You could convert image to RGB or grayscale L to save it

image = image.convert('RGB')
image = image.convert('L')

but it seems it has problem to convert negative values.

Using

plt.imsave(filename, pixels)

I get expected result


import numpy as np
import csv
from PIL import Image    
import matplotlib.pyplot as plt

counter = dict()

with open('gen_image_wgan.csv') as csv_file:
    csv_reader = csv.reader(csv_file)

    # skip headers
    next(csv_reader)

    for row in csv_reader:

        pixels = row[:-1] # without label
        pixels = np.array(pixels, dtype='float')
        pixels = pixels.reshape((28, 28))

        label = row[-1]

        if label not in counter:
            counter[label] = 0
        counter[label] += 1

        filename = '{}{}.png'.format(label, counter[label])
        plt.imsave(filename, pixels)

        print('saved:', filename)

这篇关于将具有像素值的csv文件转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆