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..










share|improve this question























  • 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














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..










share|improve this question























  • 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












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..










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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






    share|improve this answer
























      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






      share|improve this answer






















        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 18:53









        Rodolfo Donã Hosp

        478211




        478211



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo