Skip to content

Instantly share code, notes, and snippets.

@SamsadSajid
Last active November 26, 2017 12:22
Show Gist options
  • Save SamsadSajid/f6b74fe6c82ec17fc9ec37c98ebc1827 to your computer and use it in GitHub Desktop.
Save SamsadSajid/f6b74fe6c82ec17fc9ec37c98ebc1827 to your computer and use it in GitHub Desktop.
Picture will be reshaped using PIL from PILLOW
@login_required(login_url='/login/')
@group_required('client_group')
def picture(request):
user = request.user
profile_pictures = django_settings.MEDIA_ROOT + '/profile_pictures/'
if not os.path.exists(profile_pictures):
os.makedirs(profile_pictures)
if request.method == 'POST':
_picture = request.FILES['picture']
filename = profile_pictures + request.user.username + '_' + str(request.user.id) + '.jpg'
with open(filename, 'wb+') as destination:
for chunk in _picture.chunks():
destination.write(chunk)
im = Image.open(filename)
width, height = im.size
if width > 400:
new_width = 400
new_height = 300 # (height * 400) / width
new_size = new_width, new_height
im.thumbnail(new_size, Image.ANTIALIAS)
im.save(filename)
user.profile.profile_picture = filename
user.save()
return render(request, 'client/client_picture.html')
return render(request, 'client/client_picture.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment