I would like to do a screenshot with lackey of ideally the screen of an app (but to begin with, a screenshot of the whole screen would be OK).
I have tried
from lackey import *
notepad = App('notepad.exe')
notepad.open()
focusWindow = notepad.focusedWindow()
s = Screen(0)
r = s.capture()
with open("toto.bmp", "wb") as f:
f.write(r)
The picture cannot be open because the function capture returns a numpy.ndarray.
I also tried to do the following but the result is the same:
r = Screen.capture(focusWindow)
Anyone knows how to do a screenshot?
Thanks
You can use the Image.fromarray and Image.save methods from the PIL library to save the image. For some reason the code below captures the window running the script as well as the notepad app, sp I guess you might have to tweak it.
from lackey import *
from PIL import Image
notepad = App('notepad.exe')
notepad.open()
focusWindow = notepad.focusedWindow()
sleep(5) # allow some time for the notepad window to appear before capture.
screen = Screen()
capture = screen.capture(focusWindow)
image = Image.fromarray(capture)
image.save("test.bmp")
notepad.close()
Related
I have a chart function that saves the end figure as a file. After I run the function, I also want it to display the figure at the end. So, I use this:
from PIL import Image
filepath = 'image.png'
img = Image.open(filepath)
img.show()
It works just fine, but when the file opens, it opens with a random file name, not the actual file name.
This can get troublesome as I have a lot of different chart functions that work in a similar fashion, so having logical names is a plus.
Is there a way I can open an image file with Python and have it display it's original file name?
EDIT
I'm using Windows, btw.
EDIT2
Updated the example with code that shows the same behaviour.
Instead of PIL you could use this:-
import os
filepath = "path"
os.startfile(filepath)
Using this method will open the file using system editor.
Or with PIL,
import Tkinter as tk
from PIL import Image, ImageTk # Place this at the end (to avoid any conflicts/errors)
window = tk.Tk()
#window.geometry("500x500") # (optional)
imagefile = {path_to_your_image_file}
img = ImageTk.PhotoImage(Image.open(imagefile))
lbl = tk.Label(window, image = img).pack()
window.mainloop()
The function img.show() opens a Windows utility to display the image. The image is first written to a temporary file before it is displayed. Here is the section from the PIL docs.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.show
Image.show(title=None, command=None)[source] Displays this image. This
method is mainly intended for debugging purposes.
This method calls PIL.ImageShow.show() internally. You can use
PIL.ImageShow.register() to override its default behaviour.
The image is first saved to a temporary file. By default, it will be
in PNG format.
On Unix, the image is then opened using the display, eog or xv
utility, depending on which one can be found.
On macOS, the image is opened with the native Preview application.
On Windows, the image is opened with the standard PNG display utility.
Parameters title – Optional title to use for the image window, where
possible.
"
The issue is that PIL uses a quick-and-dirty method for showing your image, and it's not intended for serious application use.
Using python module ffpyplayer, How can I see the frames or get the img object to display or show the video image/frames to the screen?, in the tutorial that I followed, it seems very simple, it reads the frames and plays oudio but (does not display) any video image or frame to the screen, only if I add the (print img, t) will print the frame info to the screen but not video image is displayed on the screen.
I being following tutorials from: https://pypi.python.org/pypi/ffpyplayer, and here: http://matham.github.io/ffpyplayer/player.html, and searched google but the only relevant results point to the same info, I am somewhat new to programming and python, and so maybe I am missing something that seems to be very simple but I can't figure it out myself.
I am using: windows 7 64bit, python 2.7.11 32bit.
Any Help will be appreciated thank you very much.
from ffpyplayer.player import MediaPlayer
vid = 'test_video.flv'
player = MediaPlayer(vid)
val = ''
while val != 'eof':
frame, val = player.get_frame()
if val != 'eof' and frame is not None:
img, t = frame
print img, t #This prints the image object
# display img #This does nothing!
Kivy already provides such a video player, based on ffpyplayer, for you.
It also has the necessary threads already setup for you, to deal with buttons, file reading, audio and timing.
Check this page:
https://kivy.org/docs/api-kivy.uix.videoplayer.html
To install kivy:
https://kivy.org/docs/installation/installation.html
Then you might wish to take a look at the code in:
<< python_path >>\lib\site-packages\kivy\uix\videoplayer.py
That example could be rather complex, so you can also look at this url:
How to play videos from the web like youtube in kivy
Finally, in case Kivy complains that you only have opengl 1.1 (as happened to me), you might try adding the following lines to your code:
from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
These solved the problem to me.
I would like to open a PDF in Photoshop using Python. I know how to open photoshop (.psd) files using python, but I am wondering if there is a way to specify the program used to open a file.
So far, all I do to open a photoshop document is:
psd = "path\to\photoshop\document"
os.startfile(psd)
but when I use os.startfile on a PDF it opens with Adobe Acrobat. I'd like to open the PDF in photoshop instead. Any ideas?
from comtypes.client import GetActiveObject
# Start up Photoshop application
# app = Dispatch('Photoshop.Application')
# Or get Reference to already running Photoshop application instance
app = GetActiveObject("Photoshop.Application")
fileName = "C:\Git\PS_Samples_Files\MyPDFFile.pdf"
docRef = app.Open(fileName)
More examples at https://github.com/lohriialo/photoshop-scripting-python
os.startfile just starts the specified file with its default application. Changing the default application for PDFs to photoshop would get the result you want, but at the cost of making opening PDFs in other circumstances really annoying.
To do this properly you'd need to script it using photshop's COM interface. I haven't tried that but this tutorial looks like if might fit your needs.
Photoshop has options in opening a PDF document, called Photoshop.PDFOpenOptions
You need win32com to dispatch the photoshop application. See sample code below
import win32com.client
import os, glob
folderin = r'D:\in'
if (__name__ == '__main__'):
psApp = win32com.client.Dispatch('Photoshop.Application')
for infile in glob.glob(os.path.join(folderin, '*.pdf')):
options = win32com.client.Dispatch('Photoshop.PDFOpenOptions')
options.CropPage = 0 # BoundingBox
options.Resolution = 300 # Pixels
options.Mode = 1 # Grayscale
options.BitsPerChannel = 8 # 8 bits per channel
options.AntiAlias = True
options.ConstrainProportions = True #Deprecated for Adobe Photoshop CS3
doc = psApp.Open(infile, options)
doc.flatten
doc.Trim(1)
doc.Close(2)
psApp.Quit()
I need to be able to save the main window of a pyqt app in a PS or similar file format so that I can send it to a printer. I would just make a built in screen shot function but my main window exceeds the size of my screen. Anyone know of a way to capture the window in it's entirety or is there a prebuilt class that could do this?
QPixmap has the static method grabWidget.
Pointing this method at your window will give you a pixmap that you can save to a file or use for printing.
If calling from inside your main window class:
sshot = QPixmap.grabWidget(self)
sshot.save('sshot.png')
QPixmap.grabWiget has been deprecated. We can instead use QWidget.grab() function instead to capture window. However, it only captures the currently visible parts of the screen which can be a problem when you have a window with a scroll area. So the only method/hack that worked for me was to use ScrollArea's page step functionality paired with widget grab.
# Get total pages in window
page_count = self.scrollArea.verticalScrollBar().maximum() / self.scrollArea.verticalScrollBar().pageStep()
image_list = []
# iterate through each page step
for i in range(int(round(page_count)) + 1):
step = self.scrollArea.verticalScrollBar().pageStep() * i
self.scrollArea.verticalScrollBar().setValue(step)
# capture and save each image
self.scrollArea.grab().save(f"page - {i}.jpg", quality=100)
# convert all images to Pillow Image() to later convert to pdf
image_list.append(Image.open(f"report_page - {i}.jpg"))
# save as pdf file
pdf_file_name = f'pdf_file.pdf'
image_list[0].save(pdf_file_name, "PDF", resolution=100.0, save_all=True, append_images=image_list[1:])
# delete images if not neccessary
for i in range(len(image_list)):
os.unlink(f"page - {i}.jpg")
P.s. Please let me know if there a more elegant solution to this problem
I'm trying to grab a screenshot every 30 seconds and display it on my GUI, heres what I've got so far.
Code:
from Tkinter import *
from PIL import ImageGrab
window = Tk()
box = (100,100,400,400)
MyImage = ImageGrab.grab(box)
MyPhotoImage = PhotoImage(file=MyImage) #I know this is where its going wrong, just not sure how to fix it
PictureLabel = Label(window, image=MyPhotoImage)
PictureLabel.pack()
window.mainloop()
Python doesnt like the fact I haven't saved the image, is there a possible way to do this without saving the image (not much point since its being renewed every 30 seconds)
Its also not saving every 30 seconds yet, is there a simple way to do this without the program hanging?
As I could just use a time.sleep(30) but the program would just freeze up for 30 seconds take a picture then freeze again.
Thanks :)
You should be able to use StringIO for this:
import cStringIO
fp = cStringIO.StringIO()
MyImage.save(fp,'GIF')
MyPhotoImage = PhotoImage(data=fp.getvalue())
EDITS
Looks like I should read the docs a little closer. The PhotoImage data must be encoded to base64
from Tkinter import *
from PIL import ImageGrab
import cStringIO, base64
window = Tk()
box = (100,100,500,500)
MyImage = ImageGrab.grab(box)
fp = cStringIO.StringIO()
MyImage.save(fp,'GIF')
MyPhotoImage = PhotoImage(data=base64.encodestring(fp.getvalue()))
PictureLabel = Label(image=MyPhotoImage)
PictureLabel.pack()
PictureLabel.image = MyPhotoImage
window.mainloop()
tk images accept a "data" option, which allows you to specify image data encoded in base64. Also, PIL gives you ways to copy and paste image data. It should be possible to copy the data from MyImage to MyPhotoImage. Have you tried that?