【问题标题】:How to pass image from python to laravel如何将图像从python传递到laravel
【发布时间】:2019-03-23 10:01:28
【问题描述】:

我有一个使用枕头库生成图像的 python 脚本,我想用作我的后端并让 laravel 管理它, 我如何将 python 生成的图像传递回 laravel 请求集?

【问题讨论】:

    标签: php python laravel shell python-imaging-library


    【解决方案1】:

    解决方案是在python端将文件编码为base64并将其打印到shell, 并在 laravel 端从 base64 解码它。 以下是关键代码:

    Python3:

    import base64
    from io import BytesIO
    
    from PIL import Image, ImageDraw
    background = Image.new('RGBA', (500, 500))
    d = ImageDraw.Draw(background)
    d.text((10, 10), "Hello World", fill=(255, 255, 0))
    imgByteArr = BytesIO()
    background.save(imgByteArr, format='PNG')
    imgByteArr = base64.b64encode(imgByteArr.getvalue())
    print(imgByteArr)
    

    Laravel:

    public function newImage()
    {
        $process = new Process('python3 /path-to-script/shell.py');
        $process->run();
        if ( ! $process->isSuccessful())
        {
            return abort(404);
        }
    
        return response(base64_decode(str_replace_first('b', '', $process->getOutput())))->header('Content-Type', 'image/png');
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2021-12-23
      • 1970-01-01
      相关资源
      最近更新 更多