简体   繁体   English

从Django HTML视图获取反转图像

[英]Getting Inverted image from Django html view

I am trying to make web-based snake&ladder game on Django , for which I am using matplotlib and mpld3 to create and display my game data on web page. 我正在尝试在Django上制作基于网络的蛇梯游戏,为此我正在使用matplotlibmpld3在网页上创建和显示我的游戏数据。

I am able to get my image on the web page but it is inverted. 我可以在网页上获取我的图片,但是它是倒置的。

Where am I going wrong in this code? 这段代码在哪里出错?

my views.py file: 我的views.py文件:

import matplotlib.image as mpimg
import mpld3 as mpld3
import numpy as np
import matplotlib.pyplot as plt

def home(request):
    image =  mpimg.imread('platform3.png')
    figure = plt.figure(dpi=100)
    subplot = figure.add_subplot(111)
    subplot.set_xlim(0, 10)
    subplot.set_ylim(0, 10)
    subplot.imshow(image, extent=[0, 10, 0, 10])
    canvas = FigureCanvas(figure)
    response = django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    html_gen = 'graph.html'
    with open(html_gen, 'w') as f:
        f.write(mpld3.fig_to_html(figure))
        f.write("Hello")

    return render(request, html_gen)

should I provide the generated html_gen.html file too? 我也应该提供生成的html_gen.html文件吗?

Just place the [0, 0] in the lower left corner of the axes with: 只需将[0, 0]放在轴的左下角即可:

subplot.imshow(image, extent=[0, 10, 0, 10], origin='lower')

Please have a look at the imshow() docs 请看看imshow()文档

Update: How to check and set a default image.origin value 更新:如何检查和设置默认的image.origin

  1. Check: 校验:

     >>> import matplotlib as mpl >>> print(mpl.rcParams['image.origin']) upper 
  2. Set lower as a default value ( docs ): 设置lower的默认值( docs ):

     $ echo "image.origin : lower" >> .config/matplotlib/matplotlibrc 
  3. Check again: 再检查一遍:

     >>> import matplotlib as mpl >>> print(mpl.rcParams['image.origin']) upper 

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

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