How to import an image into a csv file using python

0 votes
Hey there!

Any one know  how to import an image into a csv file using python...I tried a lot using multiple option but doesn't work...plz give me ur suggestions!
Jun 25, 2020 in Python by anonymous
• 19,620 points
Are you looking at reading an image file or want to write an image into CSV. I am not sure if storing image is CSV is advisable
Hey @Bhanu, I want to write an image into CSV.
#Useful function

def createFileList(myDir, format='.jpg'):

fileList = []


for root, dirs, files in os.walk(myDir, topdown=False):

for name in files:

if name.endswith(format):

fullName = os.path.join(root, name)


return fileList

# load the original image

myFileList = createFileList('D:/Gen5/Memory_audit/Memory_audit_automation')

for file in myFileList:


img_file =

    # 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')'result.png')

    # Save Greyscale values

value = np.asarray(img_grey.getdata(),[1], img_grey.size[0]))

value = value.flatten()


with open("img_pixels.csv", 'a') as f:

writer = csv.writer(f)


1 answer to this question.

0 votes
I am sure you are aware CSV is plain text.

There's no way to include graphic data unless both the generator and the reader agree on a format, such as base64-encoded PNG.

This still may not be accurate and hence not suggested.
answered Jun 25, 2020 by Bhanu Kumar
I completely agree! Unless, you want to transform your image into a csv file as in a numpy array.

Related Questions In Python

0 votes
0 answers

How to save the import csv file to mongodb using pyspark (or python)?

I have this code, and I want ...READ MORE

Oct 9, 2019 in Python by Ahmed
• 310 points
0 votes
1 answer
0 votes
1 answer

I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.

You don't have a template matching tabularQueryResult in your ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 58,140 points
0 votes
1 answer

How to download a file over HTTP using Python?

In Python 2, use urllib2 which comes ...READ MORE

answered Oct 18, 2018 in Python by SDeb
• 13,300 points
+1 vote
1 answer

Error loading package

You can try doing the following : go ...READ MORE

answered Jan 9, 2019 in Python by ana1504.k
• 7,910 points
+1 vote
4 answers

How to install tkinter in pycharm?

Ya, this is a problem with installing ...READ MORE

answered Apr 4, 2019 in Python by Jishan
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,421 views
0 votes
2 answers

Is it possible to launch python idle from a virtual environment?

Try running the following code. #!/usr/bin/env python """Simple script ...READ MORE

answered Jun 25, 2020 in Python by Sirajul
• 59,090 points
0 votes
2 answers

How do I access command line arguments in Python?

import sys print(sys.argv) More specifically, if you run python ...READ MORE

answered Jun 25, 2020 in Python by Karan