I can only load and convert an image one by one but this is time consuming to do it one image at a time.
from __future__ import with_statement
from PIL import Image
im = Image.open("\path\.jpg")
pix = im.load()
width, height, = im.size
new_width = 100
new_height = 100
im = im.resize((new_width, new_height), Image.ANTIALIAS)
im.save('.png')
with open('output_file2.csv', 'w+') as f:
f.write('R,G,B\n')
for x in range(width):
for y in range(height):
r = pix[x,y][0]
g = pix[x,y][1]
b = pix[x,y][2]
f.write('{0},{1},{2}\n'.format(r,g,b))
You can simply loop through all images in your code, by using glob
:
from __future__ import with_statement
import csv
import glob
from PIL import Image
with open('output_file2.csv', 'w') as f:
w = csv.writer(f)
w.writerow(['R','G','B'])
for img in glob.iglob('/path/to/file/*.jpg'):
im = Image.open(img)
pix = im.load()
width, height = im.size
im = im.resize((100, 100), Image.ANTIALIAS)
im.save('.png')
for x,y in zip(range(width),range(height)):
w.writerow([pix[x,y][0], pix[x,y][1], pix[x,y][2]])
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments