HP Forums

Full Version: Python: How do I save a graphic?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In my App Mandelbrot Explorer I tried to save a picture in graphic G1 by:

name="tst.jpg"
hpprime.eval("MandelExpl.AFiles(name):=G1")

this line is executed without any error, but there is no graphic stored. When I terminate the App, I still can save this picture from the command line by AFiles(name):=G1. And then it shows up in the connectivity kit as a file at the right place.

Does someone have a clue?

Günter
(10-06-2021 10:50 PM)Guenter Schink Wrote: [ -> ]In my App Mandelbrot Explorer I tried to save a picture in graphic G1 by:

name="tst.jpg"
hpprime.eval("MandelExpl.AFiles(name):=G1")

this line is executed without any error, but there is no graphic stored. When I terminate the App, I still can save this picture from the command line by AFiles(name):=G1. And then it shows up in the connectivity kit as a file at the right place.

Does someone have a clue?

Günter

Have you tried:

name="tst.jpg"
hpprime.eval("MandelExpl.AFiles(" & name & "):=G1")

or

hpprime.eval("MandelExpl.AFiles(tst.jpg):=G1")

?
If you are trying to put tst.jpg into G1, should it be:

hpprime.eval("G1:=MandelExpl.AFiles(name)")

?

-road
Thanks for your inputs, but that didn't help

Anyway a little bit of progress.

When I end the Mandelbrot Explorer, and I am in "Python Numeric View" and I enter

Code:
from hppprime import *
eval('AFiles("test.png"):=G1')

then G1 is successfully stored in test.png. But when I exchange quotes and double quotes a Syntax Error is raised. So far so good, that's an easy one.

Next try:
Code:
from hppprime import *
aa="test.png"
eval('AFiles(aa):=G1')

Results in Syntax Error

Next try:
Code:
from hppprime import *
aa="test.png"
xx=eval('AFiles(aa):=G1')

No Error, but also no file: test.png
type(xx) gives <class 'str'>, but print(xx) gives Error:Syntax Error of course as you can't print a class. I think.

Next try: Function in the Mandelbrot Explorer

Code:
def save():
    eval('AFiles("test3.png"):=G1')
    line(0,0,240,320,0,0) #to show the function was executed

Tataa test3.png is there

Next try:
Code:
def save():
    aa="test3.png"
    eval('AFiles(aa):=G1')          #prefacing with xx=  doesn't change anything
    line(0,0,240,320,0,0) #to show the function was executed
No error, but also no file,
Hmm ....

Conclusion so far: providing a file name directly works but submitting a file name as a variable does not.

any suggestions?
Günter
(10-07-2021 09:19 PM)Guenter Schink Wrote: [ -> ]Thanks for your inputs, but that didn't help

Anyway a little bit of progress.

When I end the Mandelbrot Explorer, and I am in "Python Numeric View" and I enter

Code:
from hppprime import *
eval('AFiles("test.png"):=G1')

then G1 is successfully stored in test.png. But when I exchange quotes and double quotes a Syntax Error is raised. So far so good, that's an easy one.

Next try:
Code:
from hppprime import *
aa="test.png"
eval('AFiles(aa):=G1')

Results in Syntax Error

Next try:
Code:
from hppprime import *
aa="test.png"
xx=eval('AFiles(aa):=G1')

No Error, but also no file: test.png
type(xx) gives <class 'str'>, but print(xx) gives Error:Syntax Error of course as you can't print a class. I think.

Next try: Function in the Mandelbrot Explorer

Code:
def save():
    eval('AFiles("test3.png"):=G1')
    line(0,0,240,320,0,0) #to show the function was executed

Tataa test3.png is there

Next try:
Code:
def save():
    aa="test3.png"
    eval('AFiles(aa):=G1')          #prefacing with xx=  doesn't change anything
    line(0,0,240,320,0,0) #to show the function was executed
No error, but also no file,
Hmm ....

Conclusion so far: providing a file name directly works but submitting a file name as a variable does not.

any suggestions?
Günter

Did you try eval('AFiles(' + aa + '):=G1')
... to the last proposal using the string concatenation, one might probably have to add double quotes in the leading and trailing strings to quote the content of variable aa ...
Thank you Tom and Martin,

but that didn't work either. Whether with single quotes or double quotes in various combinations. I don't understand, how string concatenation is supposed to help at all?

I guess the problem is not in Python itself but in the implementation of the hpprime module, specifically "eval()". I.e. I wasn't able to have TEXTOUT deliver something through hpprime.eval().

Günter
Guenter,

I figured out a way to save graphic images to files from within Python. What works for me is to embed the function calls inside a call to the HOME EVAL() function inside the Python "hpprime.eval()" call. The EVAL() function is placed inside escaped single quotes like this: hpprime.eval('\'EVAL(<inserted function>)\'').

The following Python program saves a GROB image created in G1 to the file "pic.png".

Code:

import hpprime

def screen_cap():
  hpprime.eval('\'EVAL(DIMGROB_P(G1,320,240))\'') 
  hpprime.eval('\'EVAL(RECT())\'')
  hpprime.eval('\'EVAL(RECT_P(G1,40,40,280,200,#000000,#00FF00))\'') 
  b="\"pic.png\""
  t='\'EVAL(AFiles('+b+'):=G1)\''
  print(t)
  m=hpprime.eval(t)
  print(m)
screen_cap()

If the EVAL() function isn't used, no file is saved.
Thank you for reviving this thread. I've resolved this problem quite a while ago, but omitted to report the solution here, apologies.

It's even easier than your proposal. Consider you deal with a graphic G1 in Python and wish to save it as "pic.png" using the variable "b", a simple two liner would do:
b="pic.png"
hpprime.eval('AFiles(" '+b+' "):=G1')


It's important to use the single and double quotes as shown.
The spaces I put between double and single quotes have to be deleted, I inserted them here just to show what's single and what's double quoting.

Single quotes around what needs to be passed as a command to the Home environment and double quotes what should be a string for the Home environment. The command above would show as AFiles("pic.png"):=G1 in the Home environment.

BTW the first three lines in your example can be replaced by the graphic commands within the hpprime module. Look there for dimgrob() and fillrect()

More about interfacing between Python and Home can be seen in my wonderful Smile Mandelbrot Explorer

Günter
... that's what I meant in my reply #6 ...

Usually it is a good debugging procedure to output such a string composed of various elements e.g. as a message box or to a terminal window and inspect the string for proper matching of quotes.
(01-06-2023 03:22 PM)Martin Hepperle Wrote: [ -> ]... that's what I meant in my reply #6 ...

Usually it is a good debugging procedure to output such a string composed of various elements e.g. as a message box or to a terminal window and inspect the string for proper matching of quotes.

Problem is, I'm not an experienced programmer and as 75 years old, dealing with, for me, a brand new programming language, I lack the flexibility to turn some abstract proposal into something useful. Slow typing, slow thinking Smile

But anyway, thanks for trying to help.
Günter
No problem - i hope you had some tasty Mandelbrot during the Holidays...
(01-06-2023 07:36 PM)Martin Hepperle Wrote: [ -> ]No problem - i hope you had some tasty Mandelbrot during the Holidays...

Marzipanbrot, hmm.. Smile
Guenter,

Thank you. I see now from your example code that the use of PPL 'EVAL()' routine is not needed.

The code below works without the need for 'EVAL()':
Code:

import hpprime

def screen_cap():
  hpprime.eval('DIMGROB_P(G1,320,240)') 
  hpprime.eval('RECT()')
  hpprime.eval('RECT_P(G1,40,40,280,200,#000000,#00FF00)') 
  b="pic.png"
  t='AFiles("'+b+'"):=G1'
  print(t)
  m=hpprime.eval(t)
  print(m)
screen_cap()

Thanks for the recommendation to use the Python versions of the graphics routines. They are faster Smile than the PPL versions.
(01-05-2023 08:44 PM)Guenter Schink Wrote: [ -> ]Consider you deal with a graphic G1 in Python and wish to save it as "pic.png" using the variable "b", a simple two liner would do:
b="pic.png"
hpprime.eval('AFiles(" '+b+' "):=G1')


It's important to use the single and double quotes as shown.

There is another solution, and it seems even better to me because MicroPython on HP Prime supports %-formatting of strings, which can be used here.
Code:
b="pic.png"
hpprime.eval('AFiles("%s"):=G0' %b)

It works Smile And as you can see, there is no use of string concatenation.

You can also create a string containing several placeholders marked with '%', and then specify all the variables you want to substitute into those places one by one:
Code:
a="number"
b=12345
s='My text - label %s: %d' %(a,b) 
print(s)

and it prints:
My text - label number: 12345

Here '%s' is used to embed strings, '%d' for integers and '%f' for floating-point values.
Reference URL's