超級網迷

超級網迷+ 電腦迷+ 音樂迷
個人資料
正文

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)
評論
目前還沒有任何評論
登錄後才可評論.