Compress PNG image in Python using PIL
<p>I have a python script recorded with Selenium Builder that takes the full browser screenshot of a webpage using the following:</p>

<pre><code>fileName = "Screenshot1.png"
webDriverInstance.save_screenshot(fileName)
</code></pre>

<p>The file size is about 3.5 MB since it is a long, scrollable page and I need the full browser screenshot. I need a way to compress the saved screenshots, or save them as smaller file size PNG images so that I can attach and send several such screenshots in the same email using another Python script (with smtplib).</p>

<p>I tried this:</p>

<pre><code>fileName = "Screenshot1.png"
foo = Image.open(fileName)
fileName2 = "C:\FullPath\CompressedImage.png"
foo.save(fileName2, "PNG", optimize = True)
</code></pre>

<p>However, this doesn't seem to be working. Both files, Screenshot1.png and CompressedImage.png are of the same size (about 3.5 MB). </p>

<p>I tried several options with the save method but none of them seem to work. I do not get any errors when I run the script, but there is no decrease in the file size either.</p>

<pre><code>foo.save(fileName2, "PNG", optimize = True, compress_level = 9)
foo.save(fileName2, "PNG", optimize = True, quality = 20)
</code></pre>

<p>I am using Python 2.7. Any suggestions?</p>