简体   繁体   English

如何在没有pyglet或类似工具的情况下使用python显示图像?

[英]How to display an Image with python, without pyglet or similar?

So, what I am trying to do in detail: I have a device that acts as a display, although is technically not one (that's not really important, it can be handled like a normal display) and I want to display a series of images on it. 因此,我想做的很详细:我有一台可以充当显示器的设备,尽管从技术上讲不是一个设备(这并不重要,可以像普通显示器一样处理),但我想显示一系列图像在上面。 When each image is displayed i call a function that takes readings from another device. 当显示每个图像时,我会调用一个从另一个设备获取读数的函数。 So far I have used pyglet for that and I can get it to display the images in the correct order and where I want them, but the readings taken seem not to correspond to the right image. 到目前为止,我已经使用pyglet了,我可以让它以正确的顺序显示图像并按需要显示它们,但是所获取的读数似乎与正确的图像不符。

The simplest way to explain what I want to do is: 解释我要做什么的最简单方法是:

for a in range(10):
    display('image%d'%a)
    values(a) = measure()

Imagine values being initiated in the correct form. 想象一下以正确的形式初始化值。

What I tried so far was: 到目前为止,我尝试过的是:

import pyglet
import numpy as np
size = 64
no = 512/size
a,b,c = (0,0,0)
values = np.zeros((no,no,3))
display = pyglet.window.get_platform().get_default_display()
screens = []
for screen in display.get_screens():
    screens.append(screen)
window = pyglet.window.Window(screen = screens[1], fullscreen=1)
#here i choose the mentioned device, since it is connected to my computer via display port
@window.event

def update(dt):
    global a,b,c
    if a == no/2. and b == no/2.:
        values[a,b,c] = 0
        window.clear()
    else:
        image = pyglet.image.load('folder_%d/grating_%d_%d_%d.bmp' % (size,a,b,c))
        window.clear()
        print (a,b,c)
        image.blit(0,0)
        values[a,b,c] = measure()
    c+=1
    if c == 3:
        b += 1
        c = 0
        if b == no:
            a += 1
            b = 0
            if a == no:
                pyglet.app.exit()
pyglet.clock.schedule_interval(update, .2)
pyglet.app.run()

where measure() is my own function. 其中measure()是我自己的函数。 "no" is an index for my images, they range from (0,0,0),(0,0,1),(0,0,2),(0,1,0)... to (no,no,2) and are called to be called one by one. “ no”是我的图像的索引,范围从(0,0,0),(0,0,1),(0,0,2),(0,1,0)...到(no ,no,2)并被一一调用。 Also the case of a = b = no/2 is a special case that is of no special importance to my problem, I think. 我认为a = b = no / 2的情况也是特例,对我的问题并不特别重要。

first of all: I am pretty sure this is neither elegant nor efficient but I am not creating software that anyone else is ever likely to use. 首先:我很确定这既不优雅也不高效,但是我并不是在创造任何其他人都可能使用的软件。 I am also aware that using global variables is bad practice but their use is due to me not knowing how to use eventloops correctly. 我也知道使用全局变量是一种不好的做法,但是使用它们是由于我不知道如何正确使用事件循环。

I am not happy with this solution, because the readings i take always seem to correspond to the previous image. 我对这种解决方案不满意,因为我所读取的读数似乎总是与上一张图像相对应。 I guess I misuse the eventloop badly, but the pyglet documentation does not really help me here. 我想我严重滥用了eventloop,但是pyglet文档在这里并没有真正帮助我。 Also I feel like I am building a whole truck just to carry a bag across the street... 我也觉得自己正在建造一辆整辆卡车,只是为了在街上提着一个袋子。

I have already looked into pyplot as an alternative, since the imshow() function works in the way I want, but the plotting libraries seem to display images in random sizes, which I cannot figure out how to control properly. 我已经研究了pyplot作为替代方案,因为imshow()函数以我想要的方式工作,但是绘图库似乎以随机大小显示图像,我无法弄清楚如何正确控制。

I appreciate any help on using pyglet correctly as well as alternative libraries that can help. 感谢您正确使用pyglet所提供的帮助以及可以提供帮助的替代库。

Thank you already, 已经谢谢你了

Mopsi Mopsi

An extra-long comment, that requires formatted code 超长注释,需要格式化代码

From your example, you don't need a,b,c outside of the update function and all the global stuff is about having values that stay alive across invocations. 从您的示例中,您不需要在update功能之外使用a,b,c ,并且所有global内容都涉及使值在调用之间保持有效。 If I'm correct this is better suited by a closure, like 如果我正确的话,这更适合闭包,例如

...
def make_update(no, window):
    from itertools import product
    abcs = product(range(no),range(no),range(3))
    @window.event
    def _update(...):
        try:
            a, b, c = next(abcs)
        except StopIteration:
            ... wind up ...
        ...
    return _update

update = make_update(no, window)
...

Alright, I did not actually solve the problem but found a workaround: I just flatten my image nomenclature, eg 好吧,我实际上并没有解决问题,但是找到了一种解决方法:我只是弄平了图像命名法,例如

0_0_0 -> 000
0_0_1 -> 001
0_0_2 -> 002
0_1_0 -> 003
etc.

With values now being an array with dimensions [no*no*3,1] 现在值是尺寸为[no * no * 3,1]的数组

And since for the n-th iteration and therefore n-th measurement I see the (n-1)th image, I simply add a dummy image and assign the n-th measured value to values[n-1]. 并且由于第n次迭代以及第n次测量,我看到了第(n-1)张图像,因此我仅添加了一个虚拟图像并将第n个测量值分配给values [n-1]。 The first measurement being useless is no problem here, since values[-1] is the last element, which gets overwritten with a meaningful measurement in the end. 第一个无用的度量在这里没有问题,因为values [-1]是最后一个元素,最后被有意义的度量覆盖。

Since the number of images is known and predetermined I can reshape my array of values afterwards. 由于图像的数量是已知的并且是预先确定的,因此我可以在以后重新调整我的值数组的形状。

If anyone cares to explain why no the displayed image is shifted by one iteration (with no image at all being displayed at the first iteration) please go ahead. 如果有人想解释为什么显示的图像没有移动一个迭代(在第一次迭代中根本没有显示图像),请继续。

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

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