Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python example to show an image in full screen by opencv
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 22 16:44:27 2017
@author: sakurai
"""
import numpy as np
import cv2
import screeninfo
if __name__ == '__main__':
screen_id = 2
is_color = False
# get the size of the screen
screen = screeninfo.get_monitors()[screen_id]
width, height = screen.width, screen.height
# create image
if is_color:
image = np.ones((height, width, 3), dtype=np.float32)
image[:10, :10] = 0 # black at top-left corner
image[height - 10:, :10] = [1, 0, 0] # blue at bottom-left
image[:10, width - 10:] = [0, 1, 0] # green at top-right
image[height - 10:, width - 10:] = [0, 0, 1] # red at bottom-right
else:
image = np.ones((height, width), dtype=np.float32)
image[0, 0] = 0 # top-left corner
image[height - 2, 0] = 0 # bottom-left
image[0, width - 2] = 0 # top-right
image[height - 2, width - 2] = 0 # bottom-right
window_name = 'projector'
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.moveWindow(window_name, screen.x - 1, screen.y - 1)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
cv2.imshow(window_name, image)
cv2.waitKey()
cv2.destroyAllWindows()
@HJ959
Copy link

HJ959 commented Apr 12, 2019

Thanks!

@aecanales
Copy link

aecanales commented Jun 14, 2019

Worked for me, thanks for writing this!

@muzaffar-hussain
Copy link

muzaffar-hussain commented Feb 17, 2020

The screen freezes everytime when I use it.

@RexBarker
Copy link

RexBarker commented May 16, 2020

pip install screeninfo doesn't work for macOS by default. It requires a gcc compiler

@Farrukhraz
Copy link

Farrukhraz commented Sep 8, 2020

Thanx

@MrChaos1234
Copy link

MrChaos1234 commented Apr 25, 2021

Thank you! Working good for me!
thats exactly what i needed and i couldnt find it anywhere!

@mrjones-plip
Copy link

mrjones-plip commented Jan 13, 2022

Found this and am really happy now! Thanks for sharing.

@Adithya2018
Copy link

Adithya2018 commented Apr 1, 2022

Thanks so much been looking around for this

@jackjaquejack
Copy link

jackjaquejack commented Apr 8, 2022

I had to make a small change to work for me on my RPi

`
from screeninfo import get_monitors
import numpy as np
import cv2

screen = get_monitors()[0]
print(screen)
width, height = screen.width, screen.height

is_color = True

if is_color:
image = np.ones((height, width, 3), dtype=np.float32)
image[:10, :10] = 0 # black at top-left corner
image[height - 10:, :10] = [1, 0, 0] # blue at bottom-left
image[:10, width - 10:] = [0, 1, 0] # green at top-right
image[height - 10:, width - 10:] = [0, 0, 1] # red at bottom-right
else:
image = np.ones((height, width), dtype=np.float32)
image[0, 0] = 0 # top-left corner
image[height - 2, 0] = 0 # bottom-left
image[0, width - 2] = 0 # top-right
image[height - 2, width - 2] = 0 # bottom-right

window_name = 'projector'
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.moveWindow(window_name, screen.x - 1, screen.y - 1)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
cv2.imshow(window_name, image)
cv2.waitKey()
cv2.destroyAllWindows()`

@obedrios
Copy link

obedrios commented May 2, 2022

Thanks a lot, works for me and I can project images with Raspberry Pi. Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment