pulford
Posts: 26
Joined: Thu Dec 13, 2012 12:37 am

picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 5:55 am

I've been able to put an overlay over the live camera preview which is cool. But problem is that transparency in png files doesn't seem to work. Is there any way to put an overlay over the video preview so that certain portions appear cut-out? If not, then seems like use is very limited to just rectangles.

Thanks

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 8:53 am

It is certainly possible, but there is no existing software that I know of. There is an api for the Raspberry Pi called DispmanX that allows you to add layers that are display over other layers. In fact as far as I know everything on the Raspberry Pi display is a DispmanX layer (including the framebuffer).

I wrote some code (started two years ago) to demonstrate the capabilities of the DispmanX API on the Raspberry Pi. It includes a simple PNG viewer that may satisfy your needs. The only thing that would need to be changed is that at the moment it is hard coded to display the PNG image on layer 1. I am not sure which layer the camera preview is on ... I don't have a camera board. I would be happy to add a parameter so that you can specify the layer number on the command line.

ethanol100
Posts: 589
Joined: Wed Oct 02, 2013 12:28 pm

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 9:16 am

AndyD wrote:I am not sure which layer the camera preview is on ... I don't have a camera board. I would be happy to add a parameter so that you can specify the layer number on the command line.
It is on layer 2. You can test it by playing a video by omxplayer on layer 2 (--layer 2) and overlay an image over it.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 9:45 am

OK I have made the changes and tested the code as ethanol100 has suggested. Using two SSH sessions running:-

Code: Select all

./pngview -b 0 -l 3 /usr/share/raspberrypi-artwork/raspberry-pi-logo-small.png
in one and

Code: Select all

omxplayer --layer 2 /opt/vc/src/hello_pi/hello_video/test.h264
in the other. The result is Big Buck Bunny running with a giant raspberry plastered over the top.

By default the pngview program adds a black background (on layer 0) to obscure the framebuffer. Using -b 0 makes this layer completely transparent. The new parameter -l specifies which DispmanX layer to display the image on. The code can handle transparency from the PNG file.
ethanol100 wrote:
AndyD wrote:I am not sure which layer the camera preview is on ... I don't have a camera board. I would be happy to add a parameter so that you can specify the layer number on the command line.
It is on layer 2. You can test it by playing a video by omxplayer on layer 2 (--layer 2) and overlay an image over it.
Thanks!

ethanol100
Posts: 589
Joined: Wed Oct 02, 2013 12:28 pm

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 10:04 am

Can confirm that it can be used with raspivid and raspistill preview now. Should work with python picamera, too.
Tested it with:

Code: Select all

./pngview -b 0 -l 3 Overlay.png &
raspivid -t 0
#killall pngview #to close the pngview afterwards...
Thanks AndyD, many people are asking for some simple overlay for aligning the camera. This can be really useful.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 10:13 am

ethanol100 wrote:... Thanks AndyD, many people are asking for some simple overlay for aligning the camera. This can be really useful.
You are welcome!
ethanol100 wrote:

Code: Select all

#killall pngview #to close the pngview afterwards...
Be careful with this, the DispmanX resources won't be cleaned up correctly (not to mention the terminal fudging I do to implement keyPressed()). I should put a signal handler in there to clean up.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8102
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 10:27 am

AndyD wrote:Be careful with this, the DispmanX resources won't be cleaned up correctly (not to mention the terminal fudging I do to implement keyPressed()). I should put a signal handler in there to clean up.
I haven't checked, but I'd anticipate that the GPU will automatically clean up all resources when the dispmanx client (your app) goes away.
A quick check for any differences on the output of "sudo vcdbg malloc" and sudo vcdbg reloc" before and after the app is run then killed would tell you for sure.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 10:38 am

6by9 wrote:I haven't checked, but I'd anticipate that the GPU will automatically clean up all resources when the dispmanx client (your app) goes away.
A quick check for any differences on the output of "sudo vcdbg malloc" and sudo vcdbg reloc" before and after the app is run then killed would tell you for sure.
OK thanks, I will do some testing.

pulford
Posts: 26
Joined: Thu Dec 13, 2012 12:37 am

Re: picamera overlay RGBA / transparent png?

Mon Feb 23, 2015 10:03 pm

Wow thanks for the help guys! Will try this soon and report back.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: picamera overlay RGBA / transparent png?

Fri Feb 27, 2015 1:20 am

AndyD wrote:...I should put a signal handler in there to clean up.
OK I have put a signal handler in to cleanup if the program receives SIGINT or SIGTERM.

pulford
Posts: 26
Joined: Thu Dec 13, 2012 12:37 am

Re: picamera overlay RGBA / transparent png?

Fri May 29, 2015 7:02 am

AndyD just wanted to say THANK YOU!! Sorry for the delay. I just got around to trying this myself and it works like a champ! This will be of use to many I'm sure. I feel it should be put into some official documentation. Good job. :D

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: picamera overlay RGBA / transparent png?

Fri May 29, 2015 7:15 am

pulford wrote:AndyD just wanted to say THANK YOU!! Sorry for the delay. I just got around to trying this myself and it works like a champ! This will be of use to many I'm sure. I feel it should be put into some official documentation. Good job. :D
Thank you. You are very welcome!

dineshb
Posts: 6
Joined: Wed Nov 19, 2014 8:58 am

Re: picamera overlay RGBA / transparent png?

Mon Oct 05, 2015 6:11 am

Hi AndyD,
I tried to use your latest raspidmx/pngview executable, But I find its not properly displaying the png image(alternate vertical line artifacts and no color).The same image is displayed properly in other image viewer application.
./pngview -b 0x0000 -d 0 -l 1 -x 0 -y 0 raspberry.png
Let me know if i am missing anything. thanks in advance.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: picamera overlay RGBA / transparent png?

Mon Oct 05, 2015 6:28 am

dineshb wrote:Hi AndyD,
I tried to use your latest raspidmx/pngview executable, But I find its not properly displaying the png image(alternate vertical line artifacts and no color).The same image is displayed properly in other image viewer application.
./pngview -b 0x0000 -d 0 -l 1 -x 0 -y 0 raspberry.png
Let me know if i am missing anything. thanks in advance.
I will have a look as soon as I can. Can you do a couple of tests for me. From the pngview directory try

Code: Select all

./pngview  -b 0x0000 -d 0 -l 1 -x 0 -y 0 ../game/texture.png
and let me know if that displays correctly.

Also could you post the output of the following:-

Code: Select all

file raspberry.png

barraba
Posts: 33
Joined: Wed Sep 28, 2016 12:17 pm

Re: picamera overlay RGBA / transparent png?

Tue Oct 11, 2016 2:00 pm

can i download .DEB package for pngview?
compiling is not an easy process to handle, allways missing something

barraba
Posts: 33
Joined: Wed Sep 28, 2016 12:17 pm

Re: picamera overlay RGBA / transparent png?

Tue Oct 11, 2016 2:34 pm

what i did:

Code: Select all

git clone https://github.com/AndrewFromMelbourne/raspidmx
cd /png/raspidmx/pngview

Code: Select all

[email protected]:~/png/raspidmx/pngview $ make
cc -Wall -g -O3 -I../common -I/usr/include/libpng12 -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -g -c pngview.c -o pngview.o -Wno-deprecated-declarations
cc -Wall -g -O3 -I../common -I/usr/include/libpng12 -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -g -c ../common/backgroundLayer.c -o ../common/backgroundLayer.o -Wno-deprecated-declarations
cc -Wall -g -O3 -I../common -I/usr/include/libpng12 -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -g -c ../common/imageLayer.c -o ../common/imageLayer.o -Wno-deprecated-declarations
cc -Wall -g -O3 -I../common -I/usr/include/libpng12 -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -g -c ../common/loadpng.c -o ../common/loadpng.o -Wno-deprecated-declarations
cc -Wall -g -O3 -I../common -I/usr/include/libpng12 -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -g -c ../common/image.c -o ../common/image.o -Wno-deprecated-declarations
cc -Wall -g -O3 -I../common -I/usr/include/libpng12 -I/opt/vc/include/ -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -g -c ../common/key.c -o ../common/key.o -Wno-deprecated-declarations
cc -o pngview -Wl,--whole-archive pngview.o ../common/backgroundLayer.o ../common/imageLayer.o ../common/loadpng.o ../common/image.o ../common/key.o -L/opt/vc/lib/ -lbcm_host -lm -lpng12 -Wl,--no-whole-archive -rdynamic
what should i do next?
make install
does't work

barraba
Posts: 33
Joined: Wed Sep 28, 2016 12:17 pm

Re: picamera overlay RGBA / transparent png?

Tue Oct 11, 2016 2:36 pm

ouhhh i got it

Code: Select all

[email protected]:~/png/raspidmx/pngview $ ./pngview 
Usage: pngview [-b <RGBA>] [-d <number>] [-l <layer>] [-x <offset>] [-y <offset>] <file.png>
    -b - set background colour 16 bit RGBA
         e.g. 0x000F is opaque black
    -d - Raspberry Pi display number
    -l - DispmanX layer number
    -x - offset (pixels from the left)
    -y - offset (pixels from the top)

Thank you !!!!!! Great Program !!! Exacly what i need !

gauthier.lerouzic
Posts: 3
Joined: Mon Jan 30, 2017 6:10 pm

Re: picamera overlay RGBA / transparent png?

Mon Jan 30, 2017 6:18 pm

Hi,
First, thanks all of you for your share !
I trying few weeks for put png on video stream and you resolve my problem.
But now I have a over problem.
When I run

Code: Select all

./pngview -b 0 -l 3 Overlay.png &
raspivid -t 0
I quit raspivid with Ctrl + C
But pngview doesn't want to close. Escape, Ctrl + C, Ctrl + D...
Can you please tell me how break pngview and raspivid in same time?

Because if i run pngview and raspivid with rc.local i will be blocked

Thank you all

ethanol100
Posts: 589
Joined: Wed Oct 02, 2013 12:28 pm

Re: picamera overlay RGBA / transparent png?

Mon Jan 30, 2017 11:17 pm

You can try add this last line:

Code: Select all

./pngview -b 0 -l 3 Overlay.png &
raspivid -t 0
killall pngview
If you exit raspivid via Ctrl+c, the next line of code in your rc.local will be executed. This will then kill the pngview program.
There are other more elegant ways, but this should be sufficient.

gauthier.lerouzic
Posts: 3
Joined: Mon Jan 30, 2017 6:10 pm

Re: picamera overlay RGBA / transparent png?

Tue Jan 31, 2017 9:34 am

Hi and thanks for you return @ethanol100.
If i put killall pngview in end, the pngview will be close in first, and you only see the raspivid.
your solution doesnt working. sorry

RedRambo
Posts: 1
Joined: Sat Jul 29, 2017 12:33 am

Re: picamera overlay RGBA / transparent png?

Sat Jul 29, 2017 12:44 am

Quick question guys - I'm getting the following error when I run ./pngview

Code: Select all

[[email protected]:~/raspidmx/pngview $ ./pngview
./pngview: error while loading shared libraries: libraspidmx.so.1: cannot open shared object file: No such file or directory
The libraspidmx.so.1 file is in /raspidmx/lib/ and I've tried re-download the whole package from the GitHub, any thoughts?

AttilaSVK
Posts: 2
Joined: Tue Feb 27, 2018 2:46 pm

Re: picamera overlay RGBA / transparent png?

Tue Feb 27, 2018 2:53 pm

Hi guys,
pngview is working really well for me, I'm using it to overlay some images over omxplayer. However, my problem is the following: when I run

Code: Select all

./pngview 2.png -l 3 -b 0 & omxplayer --loop --layer 2 Background.mov
and press Ctrl+C, pngview is still running, and

Code: Select all

killall pngview
will not stop it. I also tried it with sudo, but it's the same. The png files are just stuck on the screen until I reboot the Pi.
Any help will be really appreciated, thank you very much in advance.

AttilaSVK
Posts: 2
Joined: Tue Feb 27, 2018 2:46 pm

Re: picamera overlay RGBA / transparent png?

Wed Feb 28, 2018 8:17 am

I managed to find the solution, it's

Code: Select all

killall -s SIGKILL pngview
Not the most elegant way, but at least it works :)

Church.
Posts: 5
Joined: Mon Jan 14, 2019 5:36 pm

Re: picamera overlay RGBA / transparent png?

Wed Feb 06, 2019 6:49 pm

So what type of code do we enter cause when i try it it goes to nothing and i have to restart my pi

vicwomg
Posts: 5
Joined: Tue May 24, 2016 4:45 am

Re: picamera overlay RGBA / transparent png?

Sun Feb 24, 2019 3:10 am

For simple text overlay, it's a lot simpler to just pass in a custom subtitle file. The syntax is pretty basic and you can generate one programmatically from your project.

Contents of: ~/custom.srt

Code: Select all

00:00:00,00 --> 00:00:30,00
My amazing custom overlay text
Note you can also change the timecode to whatever you want. This will show the overlay from 0-30 seconds

Then run:

Code: Select all

omxplayer --subtitles ~/custom.srt <path_to_video>

Return to “Camera board”