title: Extract PNG from a PDF file using PyPDF2 and Pillow: not enough image data
body: <p>Using the code I found in <a href="https://stackoverflow.com/a/37055040/9640238">this post</a>, which corrects <a href="https://github.com/mstamy2/PyPDF2/blob/master/Scripts/pdf-image-extractor.py" rel="nofollow noreferrer">this example code</a>, I'm trying to extract images on all pages of a PDF file. Now I'm getting an error for PNG images (works for JPG) at the second line of this piece (<code>Image.frombytes</code>):</p>
  <pre class="lang-py prettyprint-override"><code>if xObject[obj]['/Filter'] == '/FlateDecode':
      img = Image.frombytes(mode, size, data)
      img.save(imagename + &quot;.png&quot;)
      number += 1
  </code></pre>
  <p>This yields <code>ValueError: not enough image data</code>, which seems to occur because <code>data</code> cannot be correctly decoded.</p>