简体   繁体   English

Matplot 图像(从 PHP 执行 python)到 png 或 sys.stdout

[英]Matplot image (execute python from PHP) to png or sys.stdout

I'm trying to save matplot image by executing my_task.py from php-script at localhost.我试图通过在本地主机的 php-script 中执行 my_task.py 来保存 matplot 图像。

my_task.py my_task.py

    # do this before importing pylab or pyplot
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot([1,2,3])
    #fig.savefig('/var/www/html/test.png') # or relative path ('test.png') - Here I have an error - filename_or_obj = open(filename_or_obj, 'wb') IOError: [Errno 13] Permission denied: '/var/www/html/test.png'
    print "Hello<br>"
    import sys
    fig.savefig(sys.stdout, type='png')

If I'm trying fig.savefig(sys.stdout, type='png') - I'm getting ieroglighs, unreadable symbols, like:如果我正在尝试 fig.savefig(sys.stdout, type='png') - 我得到了 ieroglighs,不可读的符号,例如:

    ‰PNG  IHDR Xљv‚psBIT|d€ pHYsaaЁ?§i IDATxњмЭ}lЦхЅяс'([О)xЊДrм™r°fБШкђўмЂШ ўђўЁґCV2n„QПшi9•№33ШЩЌ=ћeвNpЭI=‰їљ%PІЪiЎwм¤МЙ офtМЦ#ЗZЫ^ї?>gQn ґЧчєy>’&хZ/|5!qЇлуюѕ?#b±XI’$IЉѓ‘Q
    ђ$I’”>, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, 
    ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, ’$I’вЖ"I’$)n, зрпяюп<рАЊ?ћїьЛїdмШ±ЬuЧ]јьтЛѓzясгЗyфСG№цЪkЙИИа®»овхЧ_жФ’$IRb»2к‰кдЙ“ЬpГ ,]є”їщ›

But If I try fig.savefig('/var/www/html/test.png') or relative path ('test.png') I have an error:但是如果我尝试 fig.savefig('/var/www/html/test.png') 或相对路径 ('test.png') 我有一个错误:

    filename_or_obj = open(filename_or_obj, 'wb')
    IOError: [Errno 13] Permission denied: '/var/www/html/test.png'

Please, help me with formatting to png or with rights to overcome permissions!请帮助我格式化为 png 或有权克服权限! How to do it - right formatting to png or giving rights to overcome permissions?怎么做 - 正确格式化 png 或授予克服权限的权利?

Here is php-code:这是php代码:

  <?php
          $pytask = 'python py/my_task.py';
          echo "<br>" .$pytask,"<br>";
          $command = escapeshellcmd($pytask);
          $output = shell_exec($command);
          echo "<p>",$output,"</p>";
          echo '<p><img src="/PyPlots/test.png" class="img-responsive img-thumbnail" alt="Куыгде"/></p>';

  ?>

Addition: when I tried in php this:另外:当我在 php 中尝试时:

          header("Content-type: image/png");

I received another error: The image " http://localhost/test.php " cannot be displayed because it contains errors我收到另一个错误:图像“ http://localhost/test.php ”无法显示,因为它包含错误

This is a late reply, but hopefully it might still help someone.这是一个迟到的回复,但希望它仍然可以帮助某人。

See your code.查看您的代码。 You have 'commas' and not 'dots'你有“逗号”而不是“点”

Change: echo "<br>" .$pytask,"<br>";更改: echo "<br>" .$pytask,"<br>"; with: echo "<br>" .$pytask."<br>";与: echo "<br>" .$pytask."<br>";

Change: echo "<p>",$output,"</p>";更改: echo "<p>",$output,"</p>"; with: echo "<p>".$output."</p>";与: echo "<p>".$output."</p>";

If you have access to your system, see errors (on apache) /var/log/apache2/error.log .如果您有权访问您的系统,请查看错误(在 apache 上) /var/log/apache2/error.log It can help you.它可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM