How do I load every image in a file and convert them to 2D array then export it to a new folder?

SN Zn

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))
Burhan Khalid

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do you scan a text file and convert it to a 2D char array?

How do I create a new sheet in excel for every csv file I have in my folder

How do I load a file from resource folder?

How do I convert a String to an 2D Array of ints?

How do I open a binary matrix and convert it into a 2D array or a dataframe?

How to load and convert .mat file into numpy 2D array?

I have Tableau twbx files. How do I convert them to xls or csv or export the data?

How do you convert a text file into a 2D character array?

How to write every Nth file to new folder

How do I log to a new file every day in rails?

How do I sort array objects and manipulate them to return a new 2D array?

How Can I Convert My new Byte Array To an Image

How do i convert my values in an array/object to measure them?

How do I check that every file in folder A exists in folder B?

How do I batch copy existing file names and use them as new folder names?

How can I copy every 4th file in a Folder into a New Folder?

How do you convert a CSV file into a 2d array in a .js file?

How do i convert a Set into 2D array in java?

How do I convert my 2D numpy array to a pandas dataframe with given categories?

How do I load a dataset file that has folder name and image name but does not contain an id in python using panda?

How do i get every json elemet in a json file and put them in an array

How do I update a 2d array to convert grades to their GPA values?

How do I extract a list of numbers separated by a space from a text file and convert them to an integer array. - Python

How do I move a file from the downloads folder to a folder on my Desktop (I need this to work on every computer)

how can i extract arrays from one array & convert them in object and put them in a new array?

How do I add a new field for every object in the array?

How can I read a file integer by integer and put them into a 2D array?

How do I load an image from my project folder inside a JavaScript file?

How do I subtract from every second[i][1] element in a 2d array?