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: 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 * 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 * Results in Syntax Error Next try: Code: from hppprime import * 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(): Tataa test3.png is there Next try: Code: def save(): 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 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 RE: Python: How do I save a graphic? - leibniz - 01-04-2023 11:50 PM 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:
If the EVAL() function isn't used, no file is saved. RE: Python: How do I save a graphic? - Guenter Schink - 01-05-2023 08:44 PM 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 Mandelbrot Explorer Günter RE: Python: How do I save a graphic? - Martin Hepperle - 01-06-2023 03:22 PM ... 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. RE: Python: How do I save a graphic? - Guenter Schink - 01-06-2023 07:29 PM (01-06-2023 03:22 PM)Martin Hepperle Wrote: ... that's what I meant in my reply #6 ... 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 But anyway, thanks for trying to help. Günter RE: Python: How do I save a graphic? - Martin Hepperle - 01-06-2023 07:36 PM No problem - i hope you had some tasty Mandelbrot during the Holidays... RE: Python: How do I save a graphic? - Guenter Schink - 01-06-2023 07:40 PM (01-06-2023 07:36 PM)Martin Hepperle Wrote: No problem - i hope you had some tasty Mandelbrot during the Holidays... Marzipanbrot, hmm.. RE: Python: How do I save a graphic? - leibniz - 01-07-2023 10:49 PM 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:
Thanks for the recommendation to use the Python versions of the graphics routines. They are faster than the PPL versions. RE: Python: How do I save a graphic? - komame - 09-09-2023 09:29 AM (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: 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" It works 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" and it prints: My text - label number: 12345 Here '%s' is used to embed strings, '%d' for integers and '%f' for floating-point values. |