The comment seems to can't have contain formated code, so I have to use answer to reply:
def crop_avatar(file_path,size):
avatar_format = get_image_format(os.path.splitext(file_path)[1])
file_format = avatar_format.split('/')[1]
avatar_filename = get_avatar_filename(file_path)
image_file = default_storage.open(file_path)
image = Image.open(image_file)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
width,height = image.size
crop_ratio = float(200)/(size[2]-size[0])
ratio = float(480)/width * crop_ratio
width = int(width * ratio)
height = int(height * ratio)
left = int(size[0] * crop_ratio)
top = int(size[1] * crop_ratio)
crop_image = image.resize((width,height),Image.ANTIALIAS)
crop_image = crop_image.crop((left,top,left+200,top+200))
avatar_io = BytesIO()#this is for default_storage using.
crop_image.save(avatar_io, format=file_format)
avatar = InMemoryUploadedFile(
avatar_io,
None,
avatar_filename,
avatar_format,
len(avatar_io.getvalue()),
None)
avatar.seek(0)
saved_path = default_storage.save(avatar_filename, avatar)
image_file.close()
default_storage.delete(file_path)
return saved_path