超级网迷

超级网迷+ 电脑迷+ 音乐迷
个人资料
正文

How to write StringIO object to an image file in python

(2014-06-22 18:33:11) 下一个

########## 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

[ 打印 ]
阅读 ()评论 (0)
评论
目前还没有任何评论
登录后才可评论.