Send an image to be printed to default printer...python - python

So, currently I am creating an application that when you press a button on the GUI it needs to send the current image to the printer. It is running on windows. I have looking all around the standard library and for third party applications that will help me do this.
Does anyone know of something that could help me with this problem?
Thanks.

Check this link: http://timgolden.me.uk/python/win32_how_do_i/print.html
And use it to print a bitmap file that you have saved to disk. jpg and png won't work for some reason - probably because they need to be converted to a printer-friendly format.
I don't know much about this stuff, but printing a bitmap with tim golden's code definitely works for me.

Related

Is there a way to take a screenshot as bytes (Fedora 35)

I'm trying to find a way to take a screenshot and save it as bytes like in this video: https://youtu.be/WymCpVUPWQ4 where he uses win32ui in Python to do that.
I've searched around and haven't found anyome who has answered this.
I've done this before in windows where I've screenshot as bytes, read as numpy, pass it into opencv and basically have the viewer open refreshing the image between 30-60fps.
Any leads would be great.
Python has a library called mss that does whats required: https://github.com/BoboTiG/python-mss

Create a basic Python interface to save images drawn with a mouse

I am working on the following project and I am having really difficulties in finding the right way of doing that. I would like to build in Python (but I am open to other possibilities) a very basic interface that allows the user to draw with the mouse (or the pen if used on a surface laptop) something and then save the image. Ideally I would like this to work on a website or at least in a jupyter notebook (at least I imagine this to be utterly difficult).
Anyone can point me in the right direction? The goal would be to use the images as input to a neural network model to demonstrate its result with real life examples.
I am looking at tk but I don't seem to find much in terms of examples.
Thanks in advance, Umberto
I'd take a look at pyautogui to capture the mouse location then "draw" it in matplotlib -- should be able to do this in a loop. You'll want to watch the tkinter window size to sync the mouse coordinates with the relative location.
Why not just have your script open create a new blank img and automatically open it with paint - then read it on close? Seems easier than creating a drawing GUI.
Have a look at my Github repository which have exactly what you need.
Link : CanvasDraw Repo
Depending on the complexity you could either use tkinter which is a package for complex GUIs or something from the gaming community like pygames. You have user input and graphical output so libraries made for games will do what you want but provide way more stuff then you need. This site might help you: Drawing Libarys
Also the answere draw-on-python-tkinter-canvas-using-mouse-and-obtain-points-to-a-list might help you.

pyautogui.locateOnScreen() Returns... Nothing?

I'm trying to use PyAutoGui's image recognition functions. (OS X)
Needless to say, I'm running into some slight issues that I can't seem to solve myself no matter where I look or what I do. I'm attempting to have PyAutoGui click on the Chrome shortcut based off a .png screenshot saved to my desktop.
Here's my code in terminal:
>>>import pyautogui
>>>chrome = pyautogui.locateOnScreen('/Users/ianscalzo/Desktop/chrome.png")
>>>
I get no backfire on my filepath, but it causes my shell/terminal to return nothing but go to a new line. (As shown in the code example above - Just causes terminal to go to a blank ">>>")
I don't really understand why it doesn't do anything but go to a new line, so any insight would be greatly appreciated.
Thank you so much!
After struggling with this forever also, finally figured out that you either use command line to take the screenshot or using the screenshot button with windows key. It doesn't work with the snipping tool.
so try:
image = pyautogui.screenshot()
image.save('testing.png')
Go and crop testing.png as small as possible so that locateOnScreen works faster. Then go back to the terminal and type:
pyautogui.locateOnScreen('testing.png')

Open files with python script in Gimp like File->Open

TL;DR: How to open an Image in Gimp with Python like you do with (File->Open) in order to get the Overwrite file.png option and avoid the Save the changes to image 'sample.png' before closing? Dialog.
I wrote a Python script for Gimp which does a few modifications on a selected area, and then saves it as a PNG. After that I want to open the file so the user can check, whether everything is correct or change something. Since the users of the plugin dont necessarily know how to use Gimp, so the "Save the changes to image 'sample.png' before closing?" Dialog might be confusing.
The method I currently use is
pdb.file_png_save(image, drw, new_file, new_file,0,9,0,0,0,0,0)
gimp.Display(pdb.gimp_file_load(new_file, new_file))
This cleans the “dirty” bit – you will not be asked to save the image if you close it afterwards (unless you do further edits):
image.clean_all()
See it referred to here: https://www.gimp.org/docs/python/
Have a look at gimp_image_clean_all. This resets the image's dirty flag and allows it to be closed.
Note that you can't prevent it from being set again if the users change something in the image.

adding a quit timer that closes a photo

After many hours of searching online and in my python book I can't seem to find the answer to my question which is what do I add to my code so I can put in a timer that automatically closes the photo? It pulls itself up but then I have to manually close the photo to get back to my main program. Any help would be appreciated.
from PIL import Image
img = Image.open('battleship load screen.png')
img.show()
This is not possible using PIL alone - img.show() is just launching another program, it's intended for debugging really, not for presenting things to the user.
From the docs.
Displays an image. This method is mainly intended for debugging
purposes.
On Unix platforms, this method saves the image to a temporary PPM
file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it.
This method returns None.
If you want to display an image and have control over it, use a graphical toolkit and construct a UI for your purpose. I've linked there to an example using PySide, a set of QT bindings, but of course you could use any toolkit - each will be different.

Categories

Resources