Django と Python を使い始めたばかりで、写真アプリを作成しようとしています。このスクリプトはサムネイルを生成しています。私はそれを自分でやりたいと思っています。残念ながら、私は何をしているのか理解していませんStringIO()
。その場合、Python Docs はあまり役に立ちません。
StringIO()
誰かがこの特定のケースで何をするのか説明してもらえますか?
http://djangosnippets.org/snippets/1172/から:
def save(self):
from PIL import Image
#Original photo
imgFile = Image.open(self.image.path)
#Convert to RGB
if imgFile.mode not in ('L', 'RGB'):
imgFile = imgFile.convert('RGB')
#Save a thumbnail for each of the given dimensions
#The IMAGE_SIZES looks like:
#IMAGE_SIZES = { 'image_web' : (300, 348),
# 'image_large' : (600, 450),
# 'image_thumb' : (200, 200) }
#each of which corresponds to an ImageField of the same name
for field_name, size in self.IMAGE_SIZES.iteritems():
field = getattr(self, field_name)
working = imgFile.copy()
working.thumbnail(size, Image.ANTIALIAS)
fp = StringIO()
working.save(fp, "JPEG", quality=95)
cf = ContentFile(fp.getvalue())
field.save(name=self.image.name, content=cf, save=False);
#Save instance of Photo
super(Photo, self).save()