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,610 points
2,395 views
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 = []

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('D:/Gen5/Memory_audit/Memory_audit_automation')

for file in myFileList:

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)

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
990 views
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,120 points
258 views
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,270 points
266 views
+1 vote
1 answer

Error loading package list:pypi.python.org

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

answered Jan 9, 2019 in Python by ana1504.k
• 7,900 points
5,700 views
+1 vote
4 answers

How to install tkinter in pycharm?

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

answered Apr 3, 2019 in Python by Jishan
45,252 views
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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,033 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,050 points
2,218 views
0 votes
2 answers

How do I access command line arguments in Python?

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

answered Jun 25, 2020 in Python by Karan
180 views