0
$\begingroup$

I have created some code that reads my CSV file and converts the dataset to a grayscale image. I want to know if there is any possible way to read through each row in the dataset and save each of the images created from the rows?

So far, I have got this code that reads the CSV files and creates an image using .imshow

import pandas as pd 
import numpy as np 
from sklearn.datasets import load_digits
from keras.preprocessing.image import array_to_img
import matplotlib.pyplot as plt

data_path = "dataset_malwares.csv"

data = pd.read_csv(data_path);

label = data.Malware.values

data = data.drop("Malware", axis=1)
data = data.drop("Name", axis=1)
data = data.values
data = data.reshape(data.shape[0], data.shape[1], 1)
data = np.tile(data, (1, data.shape[1]))

plt.imshow(data[1], cmap="gray")

plt.title("label: {0:}".format(label[1]))
plt.show()

print(data[0].shape)

I want to go through the dataset and save each image but not too sure where to start. Any suggestions would be great. Thanks :)

Rows/format of the data - I've provided a shared version of the dataset: https://1drv.ms/x/s!AqFNg8FC48SSgtZSObDmmGHs3utWog

$\endgroup$
0
$\begingroup$

Are you looking to simply save the file produced by plt.imshow? If yes, then you should be able to use plt.savefig as follows:

plt.imshow(data[1], cmap="gray")
plt.title("label: {0:}".format(label[1]))
plt.savefig("output_image.png")
plt.show()

You can either remove or keep the plt.show() call depending on whether you want the image to still be shown or just save the image.

EDIT: If you want to do this for all rows you can just loop through the numpy array as follows:

for i in range(data.shape[0])):
    plt.imshow(data[i], cmap="gray")
    plt.title("label: {0:}".format(label[1]))
    plt.savefig(f"output_image_{i}.png")
    plt.close()
$\endgroup$
5
  • $\begingroup$ Ah, excellent. I was using the wrong thing. I would like to go through the file also and save each row in the CSV document as an image without manually changing the data value one by one if that makes sense? $\endgroup$
    – Jack
    Mar 8 at 15:38
  • $\begingroup$ That should be relatively simply as well. If you could provide a few rows/format of your data I could update my code go through all rows. $\endgroup$
    – Oxbowerce
    Mar 8 at 16:28
  • $\begingroup$ Hello. Thanks for your reply. If you view my original question, I have provided a OneDrive link to the dataset. This is a copy of the one I am using so the filename is slightly different but you can access that to see all the appropriate rows and columns. $\endgroup$
    – Jack
    Mar 9 at 10:10
  • $\begingroup$ I have updated my answer to loop over all records to create and save an image. $\endgroup$
    – Oxbowerce
    Mar 9 at 10:53
  • $\begingroup$ Accepted. Good answer, and very helpful. Thanks for it! $\endgroup$
    – Jack
    Mar 9 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.