1

I want to make a program in python to manipulate an image. The problem is I want to get the image by user input on file dialog but when I use a variable string named file_path it does not read on cv2.imread() function.

I suppose the problem is with the two slash problem on python. When we try to read an image path with one slash like c:\XXX\X.png, we get an error. The solution is c:\\XXX\\X.png, but with a string variable I cannot get solution?

Is there a solution for this problem?

import cv2

import tkinter as tk

from tkinter import filedialog

import numpy as np


root = tk.Tk()
root.withdraw()

#where i get path of image
file_path = filedialog.askopenfilenames(title = 'Select File', filetypes = [('image files', '.png;.jpg'), ('image files!', '*.png;*.jpg')])

# i get bug here below. It does even not show error text
image = cv2.imread(file_path)


# ... REST OF CODE
user4020527
  • 1
  • 8
  • 20
  • 3
    Use `filedialog.askopenfilename()`, instead (note lack of 's' at the end). You're getting back a tuple or list of filenames, even if only one file was selected, which of course isn't something you can pass to `imread()`. – jasonharper Apr 06 '19 at 19:45
  • Thank you. It solved the problem :). –  Apr 06 '19 at 19:47

0 Answers0