Pil.image.save() To An Ftp Server
Right now, I have the following code: pilimg = PILImage.open(img_file_tmp) # img_file_tmp just contains the image to read pilimg.thumbnail((200,200), PILImage.ANTIALIAS) pilimg.sav
Solution 1:
Python's ftplib
library can initiate an FTP transfer, but PIL cannot write directly to an FTP server.
What you can do is write the result to a file and then upload it to the FTP server using the FTP library. There are complete examples of how to connect in the ftplib
manual so I'll focus just on the sending part:
# (assumes you already created an instance of FTP# as "ftp", and already logged in)
f = open(fn, 'r')
ftp.storbinary("STOR remote_filename.png", f)
If you have enough memory for the compressed image data, you can avoid the intermediate file by having PIL write to a StringIO
, and then passing that object into the FTP library:
import StringIO
f = StringIO()
image.save(f, 'PNG')
f.seek(0) # return the StringIO's file pointer to the beginning of the file# again this assumes you already connected and logged in
ftp.storbinary("STOR remote_filename.png", f)
Post a Comment for "Pil.image.save() To An Ftp Server"