Unable to open multiple images through openCV
up vote
0
down vote
favorite
I am learning image processing for my project, I want to open multiple images from a folder, but the problem is the image files are loaded but when I tried to display it through matplot lib only one image is shown.Code is
img_dir=r"D:UCPMachine Learning A-ZfacialExpimages"
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
#specify your vald extensions here
valid_image_extensions = [item.lower() for item in
valid_image_extensions]
image_path_list=
for file in os.listdir(img_dir):
extension = os.path.splitext(file)[1]
if extension.lower() not in valid_image_extensions:
continue
image_path_list.append(os.path.join(img_dir, file))
for image_file in image_path_list:
image=cv2.imread(image_file)
if image is not None:
plt.imshow(image, cmap='gray')
elif image is None:
print ("Error loading: " + image_file)
#end this loop iteration and move on to next image
continue
In first loop the dirrectories of all images are saved in image_path_list, but when I want to plot in second loop only one is plotted.
kindly suggest I am missing or doing something wrong..
python opencv matplotlib
add a comment |
up vote
0
down vote
favorite
I am learning image processing for my project, I want to open multiple images from a folder, but the problem is the image files are loaded but when I tried to display it through matplot lib only one image is shown.Code is
img_dir=r"D:UCPMachine Learning A-ZfacialExpimages"
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
#specify your vald extensions here
valid_image_extensions = [item.lower() for item in
valid_image_extensions]
image_path_list=
for file in os.listdir(img_dir):
extension = os.path.splitext(file)[1]
if extension.lower() not in valid_image_extensions:
continue
image_path_list.append(os.path.join(img_dir, file))
for image_file in image_path_list:
image=cv2.imread(image_file)
if image is not None:
plt.imshow(image, cmap='gray')
elif image is None:
print ("Error loading: " + image_file)
#end this loop iteration and move on to next image
continue
In first loop the dirrectories of all images are saved in image_path_list, but when I want to plot in second loop only one is plotted.
kindly suggest I am missing or doing something wrong..
python opencv matplotlib
Is this all nested in a for loop? I ask because of the continue at the bottom. also it looks like you can iterating over the 'image' variable instead of making a list or simply plotting in the for loop. Did you mean to have the last 5 lines indented to the for loop?
– SHK
Nov 9 at 18:55
the statement after for loop are the part of for loop,
– Nabeel Ayub
Nov 9 at 19:03
if image is not None: .... elif image is None: ...
-- There's a third option? If not, then the second test is redundant.
– Dan Mašek
Nov 9 at 19:03
Yes I know i just added it while checking the problem, I have removed the second part still only image is plotting...
– Nabeel Ayub
Nov 9 at 19:07
I believe it is actually showing 2 images, however the 1 is overlaying the other because you are on the same plot. try making a figure for each plot `fig = plt.figure()' before the plt.imshow()
– SHK
Nov 9 at 21:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am learning image processing for my project, I want to open multiple images from a folder, but the problem is the image files are loaded but when I tried to display it through matplot lib only one image is shown.Code is
img_dir=r"D:UCPMachine Learning A-ZfacialExpimages"
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
#specify your vald extensions here
valid_image_extensions = [item.lower() for item in
valid_image_extensions]
image_path_list=
for file in os.listdir(img_dir):
extension = os.path.splitext(file)[1]
if extension.lower() not in valid_image_extensions:
continue
image_path_list.append(os.path.join(img_dir, file))
for image_file in image_path_list:
image=cv2.imread(image_file)
if image is not None:
plt.imshow(image, cmap='gray')
elif image is None:
print ("Error loading: " + image_file)
#end this loop iteration and move on to next image
continue
In first loop the dirrectories of all images are saved in image_path_list, but when I want to plot in second loop only one is plotted.
kindly suggest I am missing or doing something wrong..
python opencv matplotlib
I am learning image processing for my project, I want to open multiple images from a folder, but the problem is the image files are loaded but when I tried to display it through matplot lib only one image is shown.Code is
img_dir=r"D:UCPMachine Learning A-ZfacialExpimages"
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"]
#specify your vald extensions here
valid_image_extensions = [item.lower() for item in
valid_image_extensions]
image_path_list=
for file in os.listdir(img_dir):
extension = os.path.splitext(file)[1]
if extension.lower() not in valid_image_extensions:
continue
image_path_list.append(os.path.join(img_dir, file))
for image_file in image_path_list:
image=cv2.imread(image_file)
if image is not None:
plt.imshow(image, cmap='gray')
elif image is None:
print ("Error loading: " + image_file)
#end this loop iteration and move on to next image
continue
In first loop the dirrectories of all images are saved in image_path_list, but when I want to plot in second loop only one is plotted.
kindly suggest I am missing or doing something wrong..
python opencv matplotlib
python opencv matplotlib
edited Nov 9 at 18:55
asked Nov 9 at 18:50
Nabeel Ayub
114
114
Is this all nested in a for loop? I ask because of the continue at the bottom. also it looks like you can iterating over the 'image' variable instead of making a list or simply plotting in the for loop. Did you mean to have the last 5 lines indented to the for loop?
– SHK
Nov 9 at 18:55
the statement after for loop are the part of for loop,
– Nabeel Ayub
Nov 9 at 19:03
if image is not None: .... elif image is None: ...
-- There's a third option? If not, then the second test is redundant.
– Dan Mašek
Nov 9 at 19:03
Yes I know i just added it while checking the problem, I have removed the second part still only image is plotting...
– Nabeel Ayub
Nov 9 at 19:07
I believe it is actually showing 2 images, however the 1 is overlaying the other because you are on the same plot. try making a figure for each plot `fig = plt.figure()' before the plt.imshow()
– SHK
Nov 9 at 21:29
add a comment |
Is this all nested in a for loop? I ask because of the continue at the bottom. also it looks like you can iterating over the 'image' variable instead of making a list or simply plotting in the for loop. Did you mean to have the last 5 lines indented to the for loop?
– SHK
Nov 9 at 18:55
the statement after for loop are the part of for loop,
– Nabeel Ayub
Nov 9 at 19:03
if image is not None: .... elif image is None: ...
-- There's a third option? If not, then the second test is redundant.
– Dan Mašek
Nov 9 at 19:03
Yes I know i just added it while checking the problem, I have removed the second part still only image is plotting...
– Nabeel Ayub
Nov 9 at 19:07
I believe it is actually showing 2 images, however the 1 is overlaying the other because you are on the same plot. try making a figure for each plot `fig = plt.figure()' before the plt.imshow()
– SHK
Nov 9 at 21:29
Is this all nested in a for loop? I ask because of the continue at the bottom. also it looks like you can iterating over the 'image' variable instead of making a list or simply plotting in the for loop. Did you mean to have the last 5 lines indented to the for loop?
– SHK
Nov 9 at 18:55
Is this all nested in a for loop? I ask because of the continue at the bottom. also it looks like you can iterating over the 'image' variable instead of making a list or simply plotting in the for loop. Did you mean to have the last 5 lines indented to the for loop?
– SHK
Nov 9 at 18:55
the statement after for loop are the part of for loop,
– Nabeel Ayub
Nov 9 at 19:03
the statement after for loop are the part of for loop,
– Nabeel Ayub
Nov 9 at 19:03
if image is not None: .... elif image is None: ...
-- There's a third option? If not, then the second test is redundant.– Dan Mašek
Nov 9 at 19:03
if image is not None: .... elif image is None: ...
-- There's a third option? If not, then the second test is redundant.– Dan Mašek
Nov 9 at 19:03
Yes I know i just added it while checking the problem, I have removed the second part still only image is plotting...
– Nabeel Ayub
Nov 9 at 19:07
Yes I know i just added it while checking the problem, I have removed the second part still only image is plotting...
– Nabeel Ayub
Nov 9 at 19:07
I believe it is actually showing 2 images, however the 1 is overlaying the other because you are on the same plot. try making a figure for each plot `fig = plt.figure()' before the plt.imshow()
– SHK
Nov 9 at 21:29
I believe it is actually showing 2 images, however the 1 is overlaying the other because you are on the same plot. try making a figure for each plot `fig = plt.figure()' before the plt.imshow()
– SHK
Nov 9 at 21:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You are loading the image
variable multiple times, because you are in a for
loop. So you are basically loading an image on top of the other, and only the last image will show.
For your code to work you will need to plot everytime you load the image.
Your code probably is just wrongly indented, if you indent one tab the if statement for the plotting, it should work
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You are loading the image
variable multiple times, because you are in a for
loop. So you are basically loading an image on top of the other, and only the last image will show.
For your code to work you will need to plot everytime you load the image.
Your code probably is just wrongly indented, if you indent one tab the if statement for the plotting, it should work
add a comment |
up vote
0
down vote
You are loading the image
variable multiple times, because you are in a for
loop. So you are basically loading an image on top of the other, and only the last image will show.
For your code to work you will need to plot everytime you load the image.
Your code probably is just wrongly indented, if you indent one tab the if statement for the plotting, it should work
add a comment |
up vote
0
down vote
up vote
0
down vote
You are loading the image
variable multiple times, because you are in a for
loop. So you are basically loading an image on top of the other, and only the last image will show.
For your code to work you will need to plot everytime you load the image.
Your code probably is just wrongly indented, if you indent one tab the if statement for the plotting, it should work
You are loading the image
variable multiple times, because you are in a for
loop. So you are basically loading an image on top of the other, and only the last image will show.
For your code to work you will need to plot everytime you load the image.
Your code probably is just wrongly indented, if you indent one tab the if statement for the plotting, it should work
answered Nov 9 at 18:53
Rodolfo Donã Hosp
478211
478211
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231705%2funable-to-open-multiple-images-through-opencv%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is this all nested in a for loop? I ask because of the continue at the bottom. also it looks like you can iterating over the 'image' variable instead of making a list or simply plotting in the for loop. Did you mean to have the last 5 lines indented to the for loop?
– SHK
Nov 9 at 18:55
the statement after for loop are the part of for loop,
– Nabeel Ayub
Nov 9 at 19:03
if image is not None: .... elif image is None: ...
-- There's a third option? If not, then the second test is redundant.– Dan Mašek
Nov 9 at 19:03
Yes I know i just added it while checking the problem, I have removed the second part still only image is plotting...
– Nabeel Ayub
Nov 9 at 19:07
I believe it is actually showing 2 images, however the 1 is overlaying the other because you are on the same plot. try making a figure for each plot `fig = plt.figure()' before the plt.imshow()
– SHK
Nov 9 at 21:29