'Doing some processing locally before uploading

I am new to Django and doing a small project where the user logs into a Django powered website and can then upload some images. These images will need to have some preprocessing on them before it can be used by the remote services. What I would like to do is perform the preprocessing on the client side and I have some python code that does it. However, I am not sure how I can setup Django so that the user can grant it permissions to run some code locally (and generate some files in the temporary directory).

So, for example, one of my view is as follows:

class UploadImageView(APIView):
    permission_classes = (IsAuthenticated, )
    authentication_classes = (JSONWebTokenAuthentication, )

    # Create your views here.
    def post(self, request):
        image = request.FILES['image']
        study = int(request.data.get('study').strip())            

        if image is None:
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # Check study ID and Image type exists
        s = StudyModel.objects.filter(pk=study).first()            
        if s is None:
            return Response(status=status.HTTP_412_PRECONDITION_FAILED)
        try:
            _ = ImageModel.objects.create(path=image, study=s)
        except:
            return Response(status=status.HTTP_417_EXPECTATION_FAILED)

        return Response(status=status.HTTP_200_OK)

Now currently, the user needs to run some code locally manually to generate this transformed image and then call the ReST API to upload it. What would be nice is the user selects the raw image, some code transforms it locally and then uploads the transformed image.



Solution 1:[1]

This sort of thing was quite possible in the past with the help of Java applets. In fact, I built one of the first applets to do so. It could resize images on the fly or compress any file before update but I digress. Nowadays applets are not widely supported in browsers.

You do have the option of using one of several javascript file uploaders that allow loading an image into a canvas. See this excellent tutorial on MDN on how to use the canvas. This QA explains how to load an image into the canvas.

Solution 2:[2]

That's not a thing you can do. Django code runs on the server. The only code you can run locally is Javascript in the browser.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel
Solution 2 Daniel Roseman