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$

    1 Answer 1

    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
      CommentedMar 8, 2021 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
      CommentedMar 8, 2021 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
      CommentedMar 9, 2021 at 10:10
    • $\begingroup$I have updated my answer to loop over all records to create and save an image.$\endgroup$
      – Oxbowerce
      CommentedMar 9, 2021 at 10:53
    • $\begingroup$Accepted. Good answer, and very helpful. Thanks for it!$\endgroup$
      – Jack
      CommentedMar 9, 2021 at 16:00

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.