0

Does anybody know how to upload an image (using filedialog.askopenfile) and then storing the uploaded image to an existing folder on my computer?! All the examples available on the internet require image paths, and i get an error whenever i provide the filepath for the uploaded image, am i doing something wrong?

import cv2
import os
from tkinter.filedialog import askopenfile
filename = askopenfile(title ='open', filetypes=(("PNGs", "*.png"),("JPGs", "*.jpg"), ("GIFs", "*.gif")))

img = cv2.imread(filename)
path = "/Users/mac/desktop/test" # => Folder path
cv2.imwrite(os.path.join(path, img)
Sam
  • 11
  • 2
  • 1
    What error do you get? Can you please post the full traceback so that we can debug it – TheLizzard Mar 08 '21 at 20:14
  • 1
    Are you aware of the difference between `askopenfile` and `askopenfilename`? – Bryan Oakley Mar 08 '21 at 20:16
  • @BryanOakley I did a quick search and it seems like "askopenfilename" returns the path to the selected file, which is what i'm looking for. Should i use it in this case? – Sam Mar 08 '21 at 20:28
  • @TheLizzard The error i'm getting is: SystemError: returned NULL without setting an error – Sam Mar 08 '21 at 20:29
  • If you want the file path, then yes, the function that returns the path is the one you should use. – Bryan Oakley Mar 08 '21 at 20:31
  • Use `filedialog.askopenfilename` not `askopenfile` it returns and `io.TextIOWrapper`. – Delrius Euphoria Mar 08 '21 at 20:46
  • @BryanOakley Thanks a lot but I'm still getting an error that says: `join() argument must be str or bytes, not 'ndarray'` am i storing the image incorrectly? – Sam Mar 08 '21 at 21:00
  • `img` is not a string, so you can't join it to a string. That's what the error is telling you. If you're trying to create a fully qualified path, you need to join the folder path and the filename, not the folder path and the image data. – Bryan Oakley Mar 08 '21 at 21:09
  • @BryanOakley I changed it to `cv2.imwrite(os.path.join(path, filename))` and now it gives me the error `TypeError: imwrite() missing required argument 'img' (pos 2)` – Sam Mar 08 '21 at 21:21
  • I suggest you read the documentation for the `imwrite` method. – Bryan Oakley Mar 08 '21 at 21:37
  • Yes i figured out the problem from reading the documentation, thank you! – Sam Mar 09 '21 at 07:07

0 Answers0