在上传 django 之前调整图像大小而不保存



我正在尝试在上传图像之前裁剪和调整大小,这是我的代码的旋转网 -

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)因为给出后端不支持绝对路径的错误。

要更改cleaned_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

还要在初始化中初始化 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)

相关内容

  • 没有找到相关文章

最新更新