Post Reply 
Python: How do I save a graphic?
10-06-2021, 10:50 PM (This post was last modified: 10-06-2021 10:51 PM by Guenter Schink.)
Post: #1
Python: How do I save a graphic?
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
Find all posts by this user
Quote this message in a reply
10-06-2021, 11:01 PM
Post: #2
RE: Python: How do I save a graphic?
(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")

?

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
10-07-2021, 04:21 PM
Post: #3
RE: Python: How do I save a graphic?
If you are trying to put tst.jpg into G1, should it be:

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

?

-road
Find all posts by this user
Quote this message in a reply
10-07-2021, 09:19 PM
Post: #4
RE: Python: How do I save a graphic?
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
Find all posts by this user
Quote this message in a reply
10-07-2021, 10:25 PM
Post: #5
RE: Python: How do I save a graphic?
(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')

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
10-08-2021, 01:18 PM
Post: #6
RE: Python: How do I save a graphic?
... 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 ...
Find all posts by this user
Quote this message in a reply
10-08-2021, 07:07 PM
Post: #7
RE: Python: How do I save a graphic?
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
Find all posts by this user
Quote this message in a reply
01-04-2023, 11:50 PM
Post: #8
RE: Python: How do I save a graphic?
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.
Find all posts by this user
Quote this message in a reply
01-05-2023, 08:44 PM
Post: #9
RE: Python: How do I save a graphic?
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
Find all posts by this user
Quote this message in a reply
01-06-2023, 03:22 PM
Post: #10
RE: Python: How do I save a graphic?
... 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.
Find all posts by this user
Quote this message in a reply
01-06-2023, 07:29 PM
Post: #11
RE: Python: How do I save a graphic?
(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
Find all posts by this user
Quote this message in a reply
01-06-2023, 07:36 PM
Post: #12
RE: Python: How do I save a graphic?
No problem - i hope you had some tasty Mandelbrot during the Holidays...
Find all posts by this user
Quote this message in a reply
01-06-2023, 07:40 PM
Post: #13
RE: Python: How do I save a graphic?
(01-06-2023 07:36 PM)Martin Hepperle Wrote:  No problem - i hope you had some tasty Mandelbrot during the Holidays...

Marzipanbrot, hmm.. Smile
Find all posts by this user
Quote this message in a reply
01-07-2023, 10:49 PM
Post: #14
RE: Python: How do I save a graphic?
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.
Find all posts by this user
Quote this message in a reply
09-09-2023, 09:29 AM
Post: #15
RE: Python: How do I save a graphic?
(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.

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)