HP Forums
Python: How do I save a graphic? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Python: How do I save a graphic? (/thread-17554.html)



Python: How do I save a graphic? - Guenter Schink - 10-06-2021 10:50 PM

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


RE: Python: How do I save a graphic? - toml_12953 - 10-06-2021 11:01 PM

(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")

?


RE: Python: How do I save a graphic? - roadrunner - 10-07-2021 04:21 PM

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

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

?

-road


RE: Python: How do I save a graphic? - Guenter Schink - 10-07-2021 09:19 PM

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


RE: Python: How do I save a graphic? - toml_12953 - 10-07-2021 10:25 PM

(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')


RE: Python: How do I save a graphic? - Martin Hepperle - 10-08-2021 01:18 PM

... 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 ...


RE: Python: How do I save a graphic? - Guenter Schink - 10-08-2021 07:07 PM

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