2

I am new in programming, and I would like to make a picture resizer with gui. I have problems with making the gui. I grabbed the problematic part of the code:

from tkinter import *
from tkinter import filedialog
import os, backend, glob2, cv2

loaded_pics=[]
picture_read=[]
window = Tk()

browsed_dir = StringVar()

browsed_dir.set(filedialog.askdirectory(initialdir="/",title='Please select a directory'))
file_path = browsed_dir.get()#+"/"

for filename in os.listdir(file_path):
    if filename.endswith(('.jpg', '.jpeg', '.gif', '.png')):
        loaded_pics.append(filename)
print(loaded_pics)

try:
    for images in loaded_pics:
        imgs = cv2.imread(images, 1)
        print(imgs)
except:
    print("ERROR")


window.destroy()
window.mainloop()

So, I have got a list of .png/.jpg/.bmp files, I can print the list, but I cannot read them with cv2.imread(), when I print(imgs), I got "None"-s.

(I have not managed to make this with glob2. It is work well with current directory, but I could not make it with filedialog.)

I hope someone can help.

Thanks in advance!

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
koger23
  • 322
  • 1
  • 4
  • 13
  • Well, maybe the problem is the filepath. – koger23 Feb 04 '17 at 16:20
  • I see two possibilities. Either the images cannot be read because their fileformat is not supported by opencv imread (that can happen if you save an emf file as png or so). Or you have a problem with your file- or folder name. It might happen that it contains non-ASCII characters which will not be properly interpreted. – ImportanceOfBeingErnest Feb 04 '17 at 16:51
  • there are a few responses here that could help: http://stackoverflow.com/questions/26392336/importing-images-from-a-directory-python/26392492 – user1269942 Feb 04 '17 at 18:51
  • Thank you @user1269942. The solution was [http://stackoverflow.com/a/26392490/7507462] without 'Image.open' – koger23 Feb 05 '17 at 13:32

1 Answers1

2

You are building a list of filenames in loaded_pics. But this doesn't include the name of the directory, file_path. So when you call imread, your image is actually located at file_path/filename, but you are only passing filename. So cv2 cannot find your file, and returns None.

yhenon
  • 4,111
  • 1
  • 18
  • 34