########## My example python script ################################
## Purpose: To show how to write StringIO object to an image file
## Date: 2014-06-22
###############################################################
from PIL import Image
from StringIO import StringIO
# Read source image
im = Image.open("n:\\desktop\\s.png")
# Initiate a buffer object (empty)
fil = StringIO()
# Save the image to buffer
# 'PNG' is the source image format
im.save(fil, 'PNG')
# Our new image is called s_new.png :)
# using binary mode to write
with open('n:\\desktop\\s_new.png', 'wb') as f:
f.write(fil.getvalue())
# Close the buffer object
fil.close()
# EOS (End of script)
# Result:
# Souce file: 5534 bytes; generated file: 5530 bytes
# Other than the above difference, they look pretty the same