简体   繁体   中英

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.

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. "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. Also the case of a = b = no/2 is a special case that is of no special importance to my problem, I think.

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

I appreciate any help on using pyglet correctly as well as alternative libraries that can help.

Thank you already,

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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