title: FileNotFoundError - Trouble opening an image from WindowsPath object
body: <p>Iterating through this list and reading/opening the images gives no error,you can see that each item (index 0) in the tuple is a WindowsPath object of pathlib module. So, this works:
  <a href="https://i.stack.imgur.com/5i1Yd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5i1Yd.png" alt="Working" /></a></p>
  <p>Iterating through this list does not work and the image is unable to be found, and you can see that each item(index 0) in the list is a WindowsPath that points to the WindowsPath object of pathlib module. I'd love to get this list to be the same as the list in the above image:
  <a href="https://i.stack.imgur.com/xvSJ5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xvSJ5.png" alt="Not Working" /></a></p>
  <p>To me, <strong>it looks like it's trying to open a non-existent file called &quot;[WindowsPath('dir1/img1.png')]&quot;  instead of the existing file called &quot;dir1/img1.png&quot;</strong>. Which seems to stem from calling function random_list() and then appending the output of that function to a list.</p>
  <p>See below code:</p>
  <pre><code>def random_list():
      #set empty list
      combo = []
      #mapping images to probabilities
      for d, p in zip(layers, probabilities):
          #random choice of image given the probability
          ch = choices(d, p)
          #add image path to list
          combo.append(ch)
      #return list in order to add more images, total of 7 loops
      return combo
             
  '''
  Main method
  '''
  #makes a list of all .png images in the given directories
  layers = [list(Path(directory).glob(&quot;*.png&quot;)) for directory in (&quot;dir1/&quot;, &quot;dir2/&quot;, &quot;dir3/&quot;, &quot;dir4/&quot;, &quot;dir5/&quot;, &quot;dir6/&quot;, &quot;dir7/&quot;)]
  
  #list of probabilities for each image
  probabilities =
                  [ [0.17, 0.11, 0.15, 0.085, 0.05, 0.235, 0.2],
                   [0.075, 0.6, 0.3, 0.025],
                   [0.075, 0.3, 0.6, 0.025],
                   [0.3, 0.075, 0.6, 0.025],
                   [0.55, 0.35, 0.1],
                   [0.1, 0.55, 0.35],
                   [0.1, 0.35, 0.55] ]
  
  #setting empty list
  combinations = []
   
  #while combinations is less than 1,111 items:
  while len(combinations) &lt; 1111:
      #get random list of traits
      a = random_list()
      #add to list if unique
      if a not in combinations:
          combinations.append(a)
  
  
  #calling iterative function which layers images from directories and saves them
  generate(layers)
  </code></pre>
  <p>ERROR:</p>
  <pre><code> File &quot;E:\myDir\subDir\imageGeneratorTest.py&quot;, line 22, in generate
          layer = Image.open(str(path), &quot;r&quot;)
      
        File &quot;C:\Program Files\pkgs\PIL\Image.py&quot;, line 2968, in open
          fp = builtins.open(filename, &quot;rb&quot;)
      
      FileNotFoundError: [Errno 2] No such file or directory: &quot;[WindowsPath('dir1/img1.png')]&quot;
  </code></pre>