Reputation: 71
Hopefully this'll be relatively easy to answer as I'm still new to Python.
I need to for-iterate through images of name [x]img[y] where x and y are identifiers that determine how the image is processed. x determines process and is valued 1 or 2 and y determines one of the values to be used in processing.
So, in pseudo-code:
directory = 'my/path/to/images'
for image in directory:
if x = 1:
do process a with y
elif x = 2:
do process b with y
else: print "ERROR"
I'm not familiar enough yet with the code and extracting values from strings to piece together the various different examples I've seen around. I get the feeling I should be able to turn the string into an array and read the 1st and 5th values as integers, right?
Your help is very much appreciated and if you'd like to get a better understanding of what I'm trying to accomplish with this project I've made a GitHub repository here.
Many thanks :)
EDIT
Thanks to Ely for getting me this far however that introduced another error. Here's the test code I'm using to open the directory, which contains files 1img2.png, 1img4.png and 2img1.png.
import os
import re
DIRECTORY = '/home/pi/Desktop/ScannerDev/TestPhotos/'
for img_filename in os.listdir(DIRECTORY):
x, y = os.path.splitext(img_filename)[0].split('img')
print 'Camera is ', x
print 'Image is ', y
"""
for img_filename in os.listdir(DIRECTORY):
a = re.search('^(.*)img(.*)\.png$', img_filename)
if a is None:
break
else:
x = a.group(1)
y = a.group(2)
print 'Camera is ', x
print 'Image is ', y
"""
Which returns:
Camera is 2
Image is 1
Traceback (most recent call last):
File "iteratorTEST.py", line 10, in <module>
x, y = os.path.splitext(img_filename)[0].split('img')
ValueError: need more than 1 value to unpack
Upvotes: 2
Views: 626
Reputation: 71
Thanks to ely for the help with this and to the other's who's answers helped and show alternative methods. I am writing this as a separate answer to give the final solution we came to in its own, clear area.
# To extract operators x and y from file named ximgy.png
# where y may be multiple characters long.
import os
DIRECTORY = '/home/pi/Desktop/ScannerDev/TestPhotos/'
for img_filename in os.listdir(DIRECTORY):
# This ensure hidden files or others don't cause issues
if img_filename.endswith(".png"):
x, y = os.path.splitext(img_filename)[0].split('img')
print 'Camera is ', x
print 'Image is ', y
# DO STUFF HERE
Upvotes: 1
Reputation: 77424
Use os.listdir
for iterating over the image files in the directory:
for img_filename in os.listdir('my/path/to/images'):
# in case you don't need to worry about anything but 'img'.
x, y = img_filename.split('img')
# in case you want to remove .png as in the comments.
x, y = os.path.splitext(img_filename)[0].split('img')
# do stuff here
If you want to enforce your assumption, say of a .png file name, when iterating the directory, you could do this:
raw_files = os.listdir(DIRECTORY)
for img_filename in filter(lambda x: x.endswith('.png'), raw_files):
...
or
for img_filename in [x for x in os.listdir(DIRECTORY) if x.endswith('.png')]:
...
or various other variations of the idea.
Note that you may also want to further post-process the result of x
and y
above, like converting them to int
, or removing any file extension (if present in the file name), and there are various other helper functions in the os
module that will help with it.
This solution makes a hard assumption that the files follow your given format exactly, and that the special string img
only appears once in the file name, and acts as a perfect separator between the X
portion and the Y
portion you need. You'll have to do some additional processing if this assumption is not true.
Upvotes: 3