rpi-rgb-led-matrix icon indicating copy to clipboard operation
rpi-rgb-led-matrix copied to clipboard

Python image-viewer.py example code

Open ElBertrando opened this issue 5 years ago • 40 comments

Hi, I am using a pi 4 with the adafruit HAT with the pwm. I have a 32 x 64 panel that I am simply trying to display an image to. I see that the image-viewer.py code requires and argument with the line "image_file=sys.argv[1]" , so I created a jpg image that is 32 x 64 pixels [the size of my rgb scree] called "Recording.jpg" and opened up terminal and ran “sudo python ./image-viewer.py Recording.jpg –led-gpio-mapping=adafruit-hat-pwm –led-rows=32 –led-cols=64 –led-slowdown-pgio=4” --- However now the image is only taking up the top third or so of the panel and is looking horrible. I am trying to figure that out now, because I created the image in Photoshop to have the exact amount of pixels or the panel. I am able to have it fill the panel by editing the image-viewer.py code "option.cols = 64", but the image is still horrible and flickering -- Any help will be appreciated. Thanks, Bret

ElBertrando avatar Feb 18 '20 20:02 ElBertrando

Did you try first with the native image viewer in https://github.com/hzeller/rpi-rgb-led-matrix/tree/master/utils#image-viewer ? This would be the recommended way to display an image.

I think the Python code uses some function to resize the image, maybe that gets tripped with your image size. Have a look at the code.

(Also, if you want to have an image the right size, I'd try to store the image as PNG image to avoid jpg artifacts which will be quite pronounced with these few pixels)

hzeller avatar Feb 18 '20 20:02 hzeller

Hi -- I am now testing with the "lcd-image-viewer.cc" code in the Utils direcotry. I created two .png's -- one with just a red background and one with white text on a red background that says "recording". The red background displays fine of course but the one with the white text looks unreadable. So now I am thinking I might be doing this wrong. If I want to display a red background with white text which says "recording" -- is sending an image file the best way ? I want to just be able to send white text with a red background directly to the panel for a studio environment... Thanks, Bret

ElBertrando avatar Feb 18 '20 22:02 ElBertrando

In what sense is it unreadable ? Is the text mumbled up in some way or is it just the contrast ?

If the text does not show right, test out if you have one of the other multiplexing types in your matrix (test with --led-multiplexing)

If it is the contrast, it is often a good idea to put a darker one-pixel outline around each letter to show up with more contrast.

hzeller avatar Feb 18 '20 23:02 hzeller

Here is the picture I am trying to upload https://imgur.com/3zqT3YH The image size is 64x32. However the white letters are all blended together and seem to be flickering. The --led-multiplexing commands do not work unfortunately. I took a pic with my phone of what I am seeing: https://imgur.com/v0aifN0 Is there a recommended font t use in Photoshop ? I have been using Arial -- Still working on this -- Actually I am starting to look at the runtext.py program to post a static text with a different colored background as an alternative

ElBertrando avatar Feb 19 '20 00:02 ElBertrando

What Raspberry Pi are you using ? If it is one of the newer ones, they might be too fast for the panel. In that case, try to --led-gpio-slowdown

Can you try the demo first to see how it looks on your panel ? examples-api-use/demo

hzeller avatar Feb 19 '20 00:02 hzeller

Pi4 . Yes I need to use --led-gpio-slowdown=4.
I found if I run this command “sudo ./text-example --led-cols=64 --led-rows=32 -f ../fonts/7x13.bdf --led-gpio-mapping=adafruit-hat-pwm --led-slowdown-gpio=4 -B 252,2,2 -C 255,255,255” I can get a red background with white text, but the background only is behind the text and not the entire panel -- Is there a parameter to set the background for the entire panel ?

ElBertrando avatar Feb 19 '20 01:02 ElBertrando

There is no option, but you can modify this example program to do what you need:

--- a/examples-api-use/text-example.cc
+++ b/examples-api-use/text-example.cc
@@ -146,13 +146,14 @@ int main(int argc, char *argv[]) {
            "Supports UTF-8. CTRL-D for exit.\n");
   }
 
+  canvas->Fill(bg_color.r, bg_color.g, bg_color.b);
   char line[1024];
   while (fgets(line, sizeof(line), stdin)) {
     const size_t last = strlen(line);
     if (last > 0) line[last - 1] = '\0';  // remove newline.
     bool line_empty = strlen(line) == 0;
     if ((y + font.height() > canvas->height()) || line_empty) {
-      canvas->Clear();
+      canvas->Fill(bg_color.r, bg_color.g, bg_color.b);
       y = y_orig;
     }
     if (line_empty)

In general, it is probably advisable to just write your own version of this program. If all you need is showing a static string, this can be much easier of course.

hzeller avatar Feb 19 '20 01:02 hzeller

In https://github.com/hzeller/rpi-rgb-led-matrix/commit/051af97e732248e47f507883785de393f2de9418 I've now added a color flooding parameter -F which allows to choose the panel background color.

hzeller avatar Feb 19 '20 01:02 hzeller

I'll download the latest version and see what I can do -- I will report back -- I appreciate all of your help today

ElBertrando avatar Feb 19 '20 01:02 ElBertrando

I've now also adapted the scrolling-text-example to accept a flood-color parameter and also allows to scroll with zero speed. This might be more useful if you just want to display a text without having to enter it manually on a line.

(Need to download even more latest version for that).

This example also shows how the 'outline' option allows us to create a slightly darker red outline around the text (127,0,0) to have the contrast of the white text punch out more nicely:

  • -s0 zero scrolling
  • -y5 shift the text so that it is somewhat in the middle of a 32x64 panel
  • --led-cols, --led-rows: as expected
  • -f use a font with relative path in this distribution
  • -B background color of text
  • -C color of text
  • -O color of outline
  • -F flooding color of panel not text
  • "Recording" the text to display
sudo ./scrolling-text-example -s0 -y7 --led-cols=64 --led-rows=32 -f ../fonts/7x14.bdf -B255,0,0 -C255,255,255 -O127,0,0 -F255,0,0 "Recording"

hzeller avatar Feb 19 '20 02:02 hzeller

Hi -- I have downloaded and replaced the "rpi-rgb-led-matrix" folder in my pi 4 with the latest "rpi-rgb-matrix-master". I am running the command in terminal that you gave as en example above, but am getting the error: /scrolling-text-example: invalid option --'F' However I have cracked open the code and can see the case 'F': that you added in the while loop so I am not sure why I am getting this error message..

ElBertrando avatar Feb 19 '20 21:02 ElBertrando

did you recompile everything ? make clean ; make ?

hzeller avatar Feb 19 '20 21:02 hzeller

Yes of course!!! I had to revisit that again as its been a while since using c++ ( I am using python for this project). I had to add a few variables but this works great: sudo ./scrolling-text-example -s0 -y7 --led-cols=64 --led-rows=32 -f ../fonts/7x14.bdf -B255,0,0 -C255,255,255 -O127,0,0 -F255,0,0 "Recording" --led-gpio-mapping=adafruit-hat-pwm --led-slowdown-gpio=4 I assume I can call this command from within my python code so I can change the text based on the event handlers of the recording device -- for example "RECORDING" to "PAUSED" -- I will be working on this now....

ElBertrando avatar Feb 19 '20 23:02 ElBertrando

There are also text examples in https://github.com/hzeller/rpi-rgb-led-matrix/tree/master/bindings/python/samples if you want to do this in Python.

hzeller avatar Feb 19 '20 23:02 hzeller

I see that however it looks like the parameters are different for the "runtext.py" sample than the "rolling-text-example.cc" file. Do I have all of the same options available [-O, -B, -C,-F], as well as the -s0 option ?

ElBertrando avatar Feb 20 '20 00:02 ElBertrando

you would need to implement these. But the features to set the color and background color etc. are also available in Python, you just need to do the equivalent things as in the C++ code.

hzeller avatar Feb 20 '20 00:02 hzeller

I think you are talking about changing the core.cpp [or maybe the core.pyx] file in \rpi-rgb-led-matrix-master\rpi-rgb-led-matrix-master\bindings\python to add the functionality ?

ElBertrando avatar Feb 20 '20 00:02 ElBertrando

All you need is graphics.DrawText() and canvas.Fill(), both of which are already provided in Python. From that, you can implement the functionality with the various options.

You have to check if the font function that generates an outline font is already there.

hzeller avatar Feb 20 '20 01:02 hzeller

Hi -- I see that runtext.py inherits from samplebase, which in turn imports RGBMatrix and RGBMatrixOptions from "core.pyx" ? It looks like I can use runtext.py as a model to obtain graphics.DrawText() and canvas.Fill(). I can then pass in arguments ("text", "--led-rows" etc) into the fucntion like this runtext.Run(text="hello", --led-rows=32,etc...)

ElBertrando avatar Feb 20 '20 23:02 ElBertrando

Hi Henner, I have been taking a different approach to run the script from my file with subprocess.call() - from my main program I am using PassT.subProcScrollingRECTest("-s0","-f ../fonts/7x14.bdf","-B255,0,0","-C255,255,255","-F255,0,0", "-O127,0,0")

which is calling:

def subProcScrollingRECTest(scrollRate, tFont, bColor, cColor, fColor, oColor):
    subprocess.call(["/home/pi/rpi-rgb-led-matrix-master/examples-api-use/scrolling-text-example", "-y7", "--led-cols=64","--led-rows=32","--led-gpio-mapping=adafruit-hat-pwm","--led-slowdown-gpio=4",'"Recording"', scrollRate, tFont, bColor, cColor, fColor, oColor])

Getting much closer but still error code: Couldn't load font ' ../fonts/7x14.bdf' I feel like I'm getting closer when I can deal with the space in the font file string....

ElBertrando avatar Feb 26 '20 01:02 ElBertrando

The relative path of the ../fonts file is of course sensitive from where you start it. So I'd use the full absolute path to the font-file.

hzeller avatar Feb 26 '20 01:02 hzeller

Hi Henner, Yes this works of course. My next problem is to kill the subprocess programatically. Currently I am using subprocess.Popen to obtain the pid of the return. Then use the command os.kill(x, signal.SIGKILL) to end the program where "x" is the return.pid -- However I am getting an error because I need to run as root. Does this mean I should once again use subprocess to run this command as root ? Am I on the right track ?

ElBertrando avatar Feb 27 '20 18:02 ElBertrando

I still think the right track would be, if you want to use Python, to directly use the Python API and do things from there, not running subprocesses.

hzeller avatar Feb 27 '20 18:02 hzeller

I have succeeded with the subprocess.call() technique. But I agree now I should use the API. However even though I am placing the "rgbmatrix" folder in the same directory as the rest of my .py code files, I am getting error importing it "from rgbmatrix import graphics" -- using the runtext.py code as my guide

ElBertrando avatar Mar 18 '20 18:03 ElBertrando

I am reading the python bindings instructions -- however this command is not working as well: You can also build for Python 3:

sudo apt-get update && sudo apt-get install python3-dev python3-pillow -y
make build-python PYTHON=$(which python3)
sudo make install-python PYTHON=$(which python3)

I am running python 3.8

ElBertrando avatar Mar 18 '20 19:03 ElBertrando

what do you mean with 'not working well' ? Do you have an error message to share ?

hzeller avatar Mar 18 '20 19:03 hzeller

Perhaps I'm doing something wrong.. I see this in visual studio code in windows 10

sudo apt-get update && sudo apt-get install python3-dev python3-pillow -y At line:1 char:21

  • sudo apt-get update && sudo apt-get install python3-dev python3-pillo ...
  •                 ~~
    

The token '&&' is not a valid statement separator in this version. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine

ElBertrando avatar Mar 18 '20 19:03 ElBertrando

This looks like you're using a funny shell. You have to install this on the Pi, and the shell should be standard bash.

hzeller avatar Mar 18 '20 19:03 hzeller

OK -- I am using visual studio code in windows 10 to write the code, then SFTP to transfer over to the pi4 --

ElBertrando avatar Mar 18 '20 19:03 ElBertrando

Any way to make this work as far as referencing the library in the folder "rgbmatrix" from visual studio code in windows ? Just because I am quarantined and need to work this way until I can physically get over the pi and the matrix..

ElBertrando avatar Mar 18 '20 19:03 ElBertrando