It seems that there is misunderstanding in what you are going to do with image files. The following shows the two possible cases based on you question.
To read a JPG
file into TXT
file without analyzing image data i.e., no decompressing etc. Use this (what would be the use for this, we are not sure!, BTW).
import os
from scipy.misc import imread
import numpy as np
imagePath = 'c:/your jpgs/'
savepath = imagePath
#save as text no decompressing
for filename in os.listdir(imagePath):
if filename!='.DS_Store' and filename[-3:]=='jpg':
with open(filename,'rb') as fin:
b = fin.read()
fin.close()
out = ','.join(b)+'\n'
with open(savepath+'trainMatrix1.txt','a') as fut:
fut.write(out)
fut.close()
output is as:
ÿ,Ø,ÿ,à, ,,J,F,I,F, ,,,, ,d, ,d, , ,ÿ,á,
To read a JPG
file into TXT
file with analyzing image data i.e., decompressing etc. Use this which utilises imread
to decompress image data. You will need remember JPG
is a heavily compressed image format, so after decompressing, it will be huge text file. You are appending all, so the output will be huge!
#save as text decompressed image into bytes
for filename in os.listdir(imagePath):
if filename!='.DS_Store' and filename[-3:]=='jpg':
b = imread(filename,flatten=0).flatten()
print b.shape
out = ','.join('%d'%i for i in b)+'\n'
print len(out)
with open(savepath+'trainMatrix2.txt','a') as fut:
fut.write(out)
fut.close()
output is as (color data):
255,255,255,245,245,245,125,125,125,72,72,72,17,17,17,2,2,2,15