首页 > 解决方案 > 如何将使用 php 上传的图像发送到 python 脚本进行处理?

问题描述

我想使用 php 上传图像并在 python 脚本中使用上传的图像,这样 python 脚本应该能够自动获取这个存储的图像。我应该在 python 脚本中写什么?

这是上传图片的php部分:

<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>

这将创建图像的本地存储:

<?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!";
    }
}

我应该在 python 脚本中写什么来自动输入这个图像?

标签: pythonphp

解决方案


推荐阅读