방금 장고와 파이썬을 사용하기 시작했고 사진 앱을 만들려고합니다. 이 스크립트는 미리보기 이미지를 생성하고 있으며 직접 작성하고 싶습니다. 불행히도 StringIO()
님은 무엇을하고 있는지 이해가되지 않습니다. 이 경우 Python Docs는별로 도움이되지 않습니다.이 스크립트에서 사용 된 StringIO()는 무엇입니까?
이 특별한 경우에 누군가 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()
앞으로는 질문에 코드를 게시하십시오. StackOverflow를 사용하면 쉽게 읽을 수 있습니다. – cheeken
'StringIO'는 (보통 파일 시스템을 사용하는'open'로 생성 된 보통 파일과는 대조적으로) 메모리 내 파일을 생성하므로 기본적으로 메모리에서 파일 작업을 수행합니다. – birryree