I have been struggling with this for a couple of days. I am trying to create an html page in which the user imports an image and resizes it ( all of this without saving anything to a model ). I want to do 3 main things:
- After the user selects his image, to have the image displayed even if the form has not been submittedand to display the current width and height of the image.
- To be able to access and modify those width and height values and to change the values.
- After "submit" has been sent, to have on the same page the transformed image and to be able to download.
Also, I am a beginner, this being my first project. I would much appreciate if you can share some of your knowledge and leave me some tips :)
forms.py
class ImageForm(forms.Form):
image = forms.ImageField()
views.py
def image_resize(request):
form = forms.ImageForm()
if request.method == "POST":
form = forms.ImageForm(request.POST or None, request.FILES or None)
if form.is_valid():
image_object = form.cleaned_data['image']
w, h = image_object.image.width, image_object.image.width
print(w, h)
else:
form = forms.ImageForm(request.POST or None, request.FILES or None)
context = {'form':form}
return render(request, 'images/image-grayscale.html', context)
Let me know if something else is needed to make your life easier with this
I'm a beginner too, but I've been able to make the first two steps somewhat (except the "image displayed even if the form has not been submitted") with cobbled together code from stackoverflow. Just wanted to share so far and in case I don't continue working on this, maybe you can make something of it.
My thoughts on the missing steps: "image displayed, even if the form has not been submitted" I'm thinking using ajax there to submit the form onchange(js), so it only looks like it hasn't been submitted, or, if possible, sending the img data to an iframe and changing the values outside the iframe and again submitting with ajax.
The project: I thought about temporary storage or cron/celery stuff, but then decided to try with data uri I think it's called. I'm not changing the images height/width, but using style="" to showcase what it would look like at those dimensions. (fun thought to try: proportional height/width editing). I'm using PIL to get the dimensions of the image and the changed dimensions of the image.
//--- html ---//
//--- views---//
//---forms---//
Hope it's helpful. Ask if you have questions. Good luck! I'll share more, if I continue.