[英]Resize image before upload django without saving
我正在嘗試在上傳圖像之前裁剪和調整大小,這是我的代碼的旋轉網-
x,y,w,h = self.x, self.y, self.w, self.h
image = Image.open(self.cleaned_data.get('profile_image'))
try:
for orientation in ExifTags.TAGS.keys() :
if ExifTags.TAGS[orientation]=='Orientation' : break
exif=dict(image._getexif().items())
if exif[orientation] == 3 :
image=image.rotate(180, expand=True)
elif exif[orientation] == 6 :
image=image.rotate(270, expand=True)
elif exif[orientation] == 8 :
image=image.rotate(90, expand=True)
except:
pass
cropped_image = image.crop((x, y, w+x, h+y))
resized_image = cropped_image.resize((160, 160), Image.ANTIALIAS)
filename = 'image'
new_image = InMemoryUploadedFile(resized_image,'ImageField',\
"%s.jpg" % filename , 'image/jpeg', resized_image.__sizeof__(), None)
self.cleaned_data['profile_image'] = resized_image
return super(UpdateUserProfileForm, self).save()
這不起作用,不保存調整大小的圖像,而是保存原始圖像。 我必須將它保存在InMemoryUploadedFile
中,因為我使用 AWS S3 存儲桶存儲媒體文件,並且它不支持保存圖像的絕對路徑。
以前在開發中我正在使用此代碼-
x,y,w,h = self.x, self.y, self.w, self.h
try:
image = Image.open(update_form.profile_image)
for orientation in ExifTags.TAGS.keys() :
if ExifTags.TAGS[orientation]=='Orientation' : break
exif=dict(image._getexif().items())
if exif[orientation] == 3 :
image=image.rotate(180, expand=True)
elif exif[orientation] == 6 :
image=image.rotate(270, expand=True)
elif exif[orientation] == 8 :
image=image.rotate(90, expand=True)
except:
pass
cropped_image = image.crop((x, y, w+x, h+y))
resized_image = cropped_image.resize((160, 160), Image.ANTIALIAS)
resized_image.save(update_form.profile_image.path)
這工作正常,但我需要更改代碼,因為resized_image.save(update_form.profile_image.path)
給出后端不支持絕對路徑的錯誤。
要更改 clean_data 使用方法clean_<attibute>
像 -
def clean_profile_image(self):
if 'profile_image' in self.changed_data:
p_image = self.cleaned_data.get('profile_image')
print('self.cleaned_data',p_image)
x,y,w,h = self.x, self.y, self.w, self.h
image = Image.open(p_image)
try:
for orientation in ExifTags.TAGS.keys() :
if ExifTags.TAGS[orientation]=='Orientation' : break
exif=dict(image._getexif().items())
if exif[orientation] == 3 :
image=image.rotate(180, expand=True)
elif exif[orientation] == 6 :
image=image.rotate(270, expand=True)
elif exif[orientation] == 8 :
image=image.rotate(90, expand=True)
except:
pass
cropped_image = image.crop((x, y, w+x, h+y))
resized_image = cropped_image.resize((160, 160), Image.ANTIALIAS)
filename = 'image'
output = StringIO()
resized_image.save(output, format='JPEG', quality=95)
output.seek(0)
new_image = InMemoryUploadedFile(output,'ImageField',\
"%s.jpg" % filename , 'image/jpeg', resized_image.__sizeof__(), None)
print('new_image',new_image)
return new_image
還要在 init 中初始化 x,y,w,h -
def __init__(self,*args,**kwargs):
print(args)
print(kwargs)
if kwargs.get('data'):
self.x = float(kwargs.get('data').get('x'))
self.y = float(kwargs.get('data').get('y'))
self.w = float(kwargs.get('data').get('width'))
self.h = float(kwargs.get('data').get('height'))
print(self.x,self.y,self.w,self.h)
return super(UpdateUserProfileForm,self).__init__(*args,**kwargs)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.