How can I send image uploaded using php to a python script for processing?

  php, python

I want to upload an image using php and use that uploaded image in a python script such that the python script should be able to automatically take this stored image. What should I write in the python script for the same?

This is the part of php for uploading image:

<form action="upload.php" method="POST" enctype="multipart/form-data">
  <label for="file">Select image:</label>
  <input type="file" id="file" name="file" required><br><br>
  <button type="submit" name="submit"> Upload Image </button>
</form>

This creates a local storage of the image:

<?php
if(isset($_POST['submit']))
{
    $file = $_FILES['file'];
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];

    $fileExt = explode('.',$fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg','jpeg','png');

    if(in_array($fileActualExt, $allowed))
    {
        if($fileError === 0)
        {
            if($fileSize<1000000)
            {
                $fileNameNew = 'uploaded_image'.".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName,$fileDestination);

                $output = shell_exec("python3 /var/www/html/py_script.py $fileNameNew");

                echo "uploaded successfully";
            }
            else
            {
                echo "File size exceeded!";
            }
        }
        else
        {
            echo "There was an error uploading your file!";
        }
    }
    else
    {
        echo "You cannot upload files of this type!";
    }
}

What should I write in the python script to input this image automatically?

Source: Ask PHP

LEAVE A COMMENT