title: OCR every .png file in a folder
body: <p>I want to iterate through every .png file in a folder and print every text contained in the Images. The first iteration works fine but the second gives an Error.</p>
  
  <p>Code:</p>
  
  <pre><code>import pytesseract
  from PIL import Image
  import os
  
  directory = (r'C:\folder...')
  
  for filename in os.listdir(directory):
      if filename.endswith('.png'):
          Image = Image.open(filename)
          im = pytesseract.image_to_string(Image)
          print(im)
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):   File
    "C:\Users\Artur\Desktop\Pytesseract_test.py", line 9, in 
        Image = Image.open(filename) AttributeError: 'PngImageFile' object has no attribute 'open'</p>
  </blockquote>
  
  <p>What does it mean 'PngImageFile' object has no attribute 'open'? Doesn't <code>Image = Image.open(filename)</code> does exactly that?</p>
  
  <p>Thanks in advance</p>
  
  <p>Edit:</p>
  
  <p>The initial PngError is solved but now another Error with PIL library occured:</p>
  
  <pre><code>import pytesseract
  from PIL import Image
  import os
  
  directory = (r'C:\folder...')
  
  for filename in os.listdir(directory):
      if filename.endswith('.png'):
          img = Image.open(filename)
          im = pytesseract.image_to_string(img)
          print(im)
  </code></pre>
  
  <p>Output: (ocr of 'frame_0000.png' is correct and then)</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Artur\Desktop\Pytesseract_test.py", line 9, in &lt;module&gt;
      img = Image.open(filename)
    File "C:\Users\Artur\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 2580, in open
      fp = builtins.open(filename, "rb")
  FileNotFoundError: [Errno 2] No such file or directory: 'frame_0001.png'
  </code></pre>
  
  <p>Edit2:</p>
  
  <p>This is very strange. When I do this:</p>
  
  <pre><code>for filename in os.listdir(r'folderpath...'):
      print(filename)
  </code></pre>
  
  <p>it works perfectly fine, iterating through every file, printing every filename.</p>
  
  <p>But when I do this:</p>
  
  <pre><code>for filename in os.listdir(r'folderpath...'):
      print(filename)
      print(pytesseract.image_to_string(Image.open(filename)))
  </code></pre>
  
  <p>an Error is given:</p>
  
  <pre><code>Bewegung_UHF_Plots.m
  Traceback (most recent call last):
    File "C:\Users\Artur\Desktop\Pytesseract_test.py", line 19, in &lt;module&gt;
      print(pytesseract.image_to_string(Image.open(filename)))
    File "C:\Users\Artur\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 2580, in open
      fp = builtins.open(filename, "rb")
  FileNotFoundError: [Errno 2] No such file or directory: 'Bewegung_UHF_Plots.m'
  </code></pre>