1

Below is my Code. It is a server program that receives stream of bitmaps from the client and I want to display the bitmap in real time. However, the "frame.set_data(im)" is the bottle neck of my code and I only get 5 FPS. Disabling that line, I get aroung 15fps for receiving images. (the display is disabled ofcourse without set_data()).

I looked for other answers, and I know I have to perform blitting to fast things up with MatPlotLib. However, I have no idea how to perform blitting with bitmaps. Could someone help me fast things up?

import matplotlib
matplotlib.use('TKAgg')
import matplotlib.pyplot as plt

while 1:
    # Decode and Save Image 
    imgdata = base64.b64decode(data)
    stream = io.BytesIO(imgdata)

    # Display realtime gameplay
    im = plt.imread(stream,"bmp")
    if frame is None:
        print "Start Rendering.."
        frame = plt.imshow(im)
        plt.show()
    else:
        frame.set_data(im)
    plt.pause(0.00000001)
  • Someone might prove me wrong, but I'd say matplotlib is not the right tool for this task. It is more geared towards quality than performance. There are certainly other libraries out there that can show images much faster (e.g. [pygame](http://stackoverflow.com/q/8873219/3005167)). – MB-F Feb 20 '17 at 07:58
  • 1
    The final speed will surely depend on how large the images are. Providing the data to imshow using `set_data` does take some time, so it is not surprising that framerate goes down. Blitting may help. Now I don't know if your application would allow for using `matplotlib.animation.FuncAnimation`? If so, `blit = True` can be directly used. If not, have a look at [this question](http://stackoverflow.com/questions/40126176/fast-live-plotting-in-matplotlib-pyplot) on how to use blitting without the build-in animation class. – ImportanceOfBeingErnest Feb 20 '17 at 08:32

1 Answers1

1

Thanks @kazemakase I was able to achieve desired speed with pygame. Below is my code.

import pygame

pygame.init()
screen = pygame.display.set_mode(size) 

while 1:
    # Decode and Save Image 
    imgdata = base64.b64decode(data)
    stream = io.BytesIO(imgdata)

    pygame.event.get()
    img=pygame.image.load(stream,'bmp') 
    screen.blit(img,(0,0))
    pygame.display.flip() # update the display