I have some png-files in a given directory (C:/.../*.png). These images are of different size (e.g. 343 x 109 and 189 x 130 pixels). I would like to open this files with Python to work with this files (to show the images and to develop a neural network).
import os
def open_images(filename):
with os.scandir(filename) as it:
for entry in it:
if entry.name.endswith(".png") and entry.is_file():
print(entry.name, entry.path)
X_train = open_images("C:/...")
Obviously, this code returns only the file path. Could anyone help me to open the png-files? I think, one should also focus on the different sizes of the images. How do I handle this issue?
Thank you very much in advance!
When you need to open files that match a pattern it's a job for the glob
module. Using that module you don't need to check each files which one are good or not. You only get those that you want.
import os.path
import glob
def open_images(images_directory):
pattern_to_match = os.path.join(images_directory, "*.png")
png_files = (x for x in glob.iglob(pattern_to_match)
if os.path.isfile(x))
for current_png_filename in png_files:
print("Opening file", current_png_filename)
with open(current_png_filename) as current_png_file:
# Do something with current_png_file
pass
directory_to_search = r"C:\…"
open_images(directory_to_search)
Here's what I did
os.path.join()
to set where and what kind of files we are looking forglob.iglob()
to get a iterator of names of the files that match our patternopen()
to open each one of them.But I didn't know what you wanted to do with them so I've used the pass
statement to do nothing.
For example, let's say that you wanted to size (length x height) for these images. You could use the Pillow
module to get this information. But doing so, you don't need to open the file yourself. Here's how it would look like:
import os.path
import glob
import PIL.Image # Used to do something with the images
def open_images(images_directory):
pattern_to_match = os.path.join(images_directory, "*.png")
png_files = (x for x in glob.iglob(pattern_to_match)
if os.path.isfile(x))
for current_png_filename in png_files:
current_image = PIL.Image.open(current_png_filename)
print("Image", current_png_filename, "size:", current_image.size)
directory_to_search = r"C:\…"
open_images(directory_to_search)
But we can simplify how we are processing the file by using the pathlib
module which give us a higher level way to processing files. That would give us:
import pathlib
import PIL.Image # Used to do something with the images
def open_images(images_directory):
png_files = (x for x in images_directory.glob('*.png')
if x.is_file())
for current_png_filename in png_files:
current_image = PIL.Image.open(current_png_filename)
print("Image", current_png_filename, "size:", current_image.size)
directory_to_search = pathlib.Path(r"C:\…")
open_images(directory_to_search)
So does that give you some indications of what you goal is?