Importing FIles on IDLE (python code editor) - python

This may seem like a stupid question, but I am genuinely having a lot of trouble with this: I need to be able to import a file, like an image, to IDLE so I can open it when the program runs. Am I missing something really obvious or is this not possible on IDLE?
Thanks.
BTW I work on a Chromebook, in Python.

from PIL import Image
with Image.open(image_directory) as image:
pass
From there you can use PIL's various functions to edit the image. Don't forget to save it, though.

You can use open() and read() function.
https://www.techwalla.com/articles/how-to-a-read-text-file-in-python
It is properly shown on the above site.

Related

saving a python figure that later can be viewed interactively

I am using Spyder to edit python code. I plotted a figure like following on my screen:
As you can see I have zoom-in on my menu by playing which I can magnify a ROI like following:
Now I need to send this figure to another person who'd like to view this figure interactively, say do zoom-in/zoom-out as well.So my question is, is there anyway to send this figure in certain format the other person can play with such that me without sending my entire python code?
I tried to the save icon on the menu bar, but I did not see a format that can do what I want. I am new to python, please advice. Thank you.
PS: I think in MATLAB you can do that by saving the figure in certain format, so that as long as the other person has MATLAB installed, he/she does not need the data to see the figure interactively
You need to use the pickle module.
MATLAB saves the figure in a .fig format. This is really just a .mat MATLAB data file with a different extension so MATLAB knows it stores image data. If you change the extension to .mat, you can open it is a refular MATLAB data file and see that it just contains variables storing image information.
The equivalent thing to do in matplotlib is to use the pickle.dump function to save the matplotlib figure object to a file. Someone else can then just load the figure from the file and show it. Although the other person may need to have the same matplotlib version installed.
A better option would be to use something like bokeh to save an interactive HTML plot.
I believe its too late for an answer whereas I think it will help others.
In python, if an interactive figure is plotted in plotly. Then it can be exported as a .html.
For reference please follow official documentation Interactive HTML Export in Python
.
I hope it helps.

Automatically import new SVGs to Inkscape

I want to write an extension or find some way to automatically import to an Inkscape document the new files that appear in a given folder. I don't want to simply update the SVG/XML document because Inkscape has to be open for another extension (AxiDraw) to work.
My problem is that I cannot find a way to make my extension work "in the background", either it goes into an infinite loop that crashes the program or, if I use for example the Timer from threading it just stops after the first execution.
Also using the command line doesn't seems to work since there is not an import command and verbs don't work in shell mode.
Is there some relatively easy way to work around these problems without having to dig into the c++ source code?
Well, turned out it is not possible. From the answer to my same question on the Inkscape forum:
Python extensions currently work by 'halting' the Inkscape main
process. Then they modify the document, which is then loaded back into
Inkscape.
So this cannot work. You'd need to run it manually in regular
intervals, or set up some 'click bot' that does that for you.

Python Tkinter PhotoImage file formats supported?

I appreciate this is a VERY novice question but I just want to check in regard to Tkinter Photoimage class, is it only GIF/PGM/PPM images that it can read from files and nothing else unless I download the Python Image Library.
If thats the case I now know exactly where I went wrong in the code I'm writing. IE: wrong file format
That sounds right. There may be another format or two that native Tkinter supports, but it's very limited. There's a more up-to-date version of PIL named Pillow that you might want to look into. It doesn't seem like PIL is being actively maintained, last I looked. If you want to work with JPEG for example, you need PIL (or Pillow).

Generating an image-report with Python

Hy,
I'm working on a project, where I have to generate a image (e.g. .png, .bmp etc) with a python script.
The Image must have:
Small boxes (8x8px) in 3 different colours
Horizontal(normal) text in 2 different sizes
and 3) vertikal text (rotate normal text) (like this: http://devcity.net/Data/ArticleImages/Dual_Labels.jpg)
So not very complex things.
I spent the last days with PiL (Python Image Library). For the small boxes, it works fine and easy. But to generate a text in the image, it doesn't work fine.
What also works is to write a normal text, with the standard font (pilfont-type).
But I can't set the px-size of this text. When using truetypes, the following error comes:
"The _imagingft C module is not installed"
I allready "googled" this and this seems to be a popular problem. My Problem is, that the script also has to run on other python systems. What I can accept is, that I have to install Pil on each system/computer, but I can't fix the problem with the truetypes each time!
I'm using Python 2.7 with pil 1.1.7.
So to my question:
For the named "forms" my script has to generate, what library (or other ways to generate an image with a script) would you recomment to me?
Would it be possible to create, e.g writing a bitmap-file with text and pixels with colour, with my script in "Pure-Python", so without any extension?(Would be the optimal solution for me)
Have you thought about using PyCairo instead? See this link for an example: https://stackoverflow.com/a/6506825/514031
This is not quite what matplotlib was designed for, but is definitely capable of producing what you're after. Have a look at the gallery, it has usage examples for almost everything you mentioned.

In python, how do you open a file or picture in a new window?

The content of the file or the picture should show up in a new window.
That will depend a lot on your operating system, since there are different programs on different systems to view images, etc. But one trick you might use is
import webbrowser
webbrowser.open(THE_FILE)
That should open up your default web browser pointed to the file, which will display images, and might do what you want for certain types of files.
you could try
os.system("fspot picture.jpeg")
But, obviously, I'm assuming you're using fspot to view images, and that might only work in linux.
Check out the documentation for os.
-EDIT-
Mu Mind's solution works pretty well in Ubuntu Karmic. Not sure what it will do on a windows machine.
you can use
os.system("start "+"anyfile.txt")
assuming you have windows. This basically opens the file in it's extension (For example if you had a .txt it would open in notepad.)

Categories