Take a screen shot of a specif area and save as variable - python

I want to make a program taking a cropped screenshot (like Windows+Shift+s) and save it as variable, not as a file.
How do I Programmatically open this win+shift+s window?
How to directly get the output without saving this as a picture?
I'm using python

you will need to:
trigger the Print screen key (any keyboard library should allow it)
retrieve the data from the clipboard (you will maybe forced to save it as a temporary file and then read from it)
then parse the data it will be it will png format.

Related

Python - Convert JPG to text file

Good morning all,
I've made a Python script that adds text on top of images, based on a preset template. I'm now developing a template editor that will let the user edit the template in GUI, then save the template as a config file. The idea is that one user can create a template, export it, send it to a new user on a separate computer, who can import it into their config file. The second user will retain full edit abilities on the template (if any changes needs to be made).
Now, in addition to the text, I also want the ability to add up to two images (company logos, ect.) to the template/stills. Now, my question: Is there a way to convert a JPG to pure text data, that can be saved to a config file, and that can be reinterpreted to a JPG at the receiving system. And if not, what would be the best way to achieve this? What I'm hoping to avoid is the user having to send the image files separately.
Sounds questionable that you want to ship an image as text file (it's easy, base64 is supplied with python, but it drastically increases the amount of bytes. I'd strongly recommend not doing that).
I'd rather take the text and embed it in the image metadata! That way, you would still have a valid image file, but if loaded with your application, that application could read the metadata, interpret it as text config.
There's EXIF and XMP metadata, for both there's python modules.
Alternatively, would make more sense to simply put images and config files into one archive file (you know .docx word documents? They do exactly that, just like .odt; java jar files? Same. Android APK files? All archive files with multiple files inside) python brings a zip module to enable you to do that easily.
Instead of an archive, you could also build a PDF file. That way, you could simply have the images embedded in the PDF, the text editable on top of it, any browser can display it, and the text stays editable. Operating on pdf files can be done in many ways, but I like Fitz from the PyMuPDF package. Just make a document the size of your image, add the image file, put the text on top. On the reader side, find the image and text elements. It's relatively ok to do!
PDF is a very flexible format, if you need more config that just text information, you can add arbitrary text streams to the document that are not displayed.
If I understand properly, you want to use the config file as a settings file that stores the preferences of a user, you could store such data as JSON/XML/YAML or similar, such files are used to store data in pure readable text than binary can be parsed into a Python dict object. As for storing the images, you can have the generated images uploaded to a server then use their URL when they are needed to re-download them, unless if I didn’t understand the question?

Can you use python to access PowerPoint add in functions?

I am creating multiple powerpoint decks that have data modified but I need to be able to hit this "refresh slides" button that accesses the updated data from a connected website. Is there a way to do this automatically in python?
No, there is not. However, there is a workaround:
You have to put your data in single .xslx (Excel) files
Use this data within you PowerPointPresentation (see here for a description)
Write a script to refresh your data and output them into the .xlsx files
If you have done everything correctly, next time you open PowerPoint it will ask you, whether or not it should refresh the tables/graphs.
That is the only work around I have found in order to refresh PowerPointPresentations.

Is there a way I can display data in Excel without saving a file first?

I'm using openpyxl to create a workbook in memory and fill it with data. Is there anyway to display that data in Excel at the end of the Python script without saving the file? It would be left up to the user to decide if they want to save the file or not. I'm guessing it's not possible but I wanted to see if I could get a more definitive answer here. Thanks!

How can I modify a sublime text 3 document via the in-app python console?

I want to apply some function to the text of a document.
Like for example run a regexp replacement and then convert the resulting text to lowercase (or some more complicated example that cannot be easily done with the provided tools).
I know how to do this using python, so I could just run a simple script from a python interpreter to load, modify, and save the data back.
This can however be quite annoying, and given the existence of a python API for sublime text there should be a way to directly run a script to modify the open document.
I would also prefer to avoid macros because those would require me to save a .sublime-macro file, but alternative solutions of this sort are equally welcome.
How can I achieve this?
In the sublime console, the symbol view represents the currently focused view (file) while window represents the current window.
So you can use the plugin API method sublime.View.substr() to collect the contents of the currently selected view as a string for further manipulation:
content = view.substr(sublime.Region(0, view.size()))
Or if you wanted, you could select some text first and then grab the contents of the selection. This example grabs only the content of the first selection; modify as needed if you wanted to grab the contents of multiple selections at once.
content = view.substr(view.sel()[0])
From here you can do whatever you want to content. Your issue is in putting the contents back into the buffer when you're done.
All edit operations need to be tracked to allow Sublime the ability to undo the change. For this reason the underlying API requires all calls that would modify the buffer (inserting, appending, or replacing text, etc) to provide an edit object. However these objects are strictly controlled; only Sublime can create one on your behalf.
So the only way to modify the buffer is to either implement your own TextCommand that does it for you, or utilize an existing command via the sublime.View.run_command() method.
content = view.substr(sublime.Region(0, view.size()))
content = content.replace("Hello", "Bonjour")
content = content.replace("Goodbye", "Au Revoir")
view.run_command("select_all")
view.run_command("insert", {"characters": content})
Here I've pulled the text out of the buffer, done some replacements, and then put the entire modified string back into the buffer by first selecting everything and then inserting the new content back.
Note that if you were doing this from a TextCommand, you would need to use self.view everywhere and not just view.
You can launch your script from the Python API, and use and get the contents of the entire file with:
contents = self.view.substr(sublime.Region(0, self.view.size()))

How to return image with GAE?

Each time user accesses http://www.example.com/some-random-symbols-1x1.png, I should return transparent image 1x1px. I've created according file, but how should I read it in my code to return to the user? I know how to display the image from the datastore or blobstore. But have no idea how to read and return binary file.
It can not be static file due to the following reasons:
url will contain some-random-symbols;
once url is accessed, prior to displaying images, I would like to log that somebody accessed the file.
A 1x1 transparent PNG (or GIF) is small enough that you can hard-code the base64 representation directly and emit it directly via self.response.write() (after decoding).
Reading from disk every time is relatively expensive. If you want to go that route, lazily initialize a global variable.
In a more general case, I'd use the blobstore and the BlobstoreDownloadHandler, but for a tiny gif that will definitely fit into memory, something like this to read the file's content:
with open('path/to/file.gif') as f:
img_content = f.read()
I'd put this outside of my handler, so it was done once per instance. If you're using 2.5, then you'll need to import 'with' from future, or open and close the file yourself.
then in your handler, assuming webapp2:
self.response.content_type = 'image/gif'
self.response.write(img_content)

Categories

Resources