I had an idea for a program I want to write for my boss at work. My idea was that I was going to load a template into python, run through a .txt file and fill the template with the corresponding information using the graphics library.
My only problem is I don't know how to save the final result. Some of my colleagues in a python class I took a year ago did it and now I regret not asking how... Any help will be appreciated. Thanks.
You can use Selenum for this purpose.
Selenium using a webdriver provides the facility to take screenshots!
from selenium import webdriver
driver = webdriver.Firefox()
# code goes in here
driver.save_screenshot(title+".png")
I believe this will serve your purpose!
Hope it helps.
TO take screenshots you can use pyscreenshot module.
import pyscreenshot as ImageGrab
im=ImageGrab.grab()
im.show()
Here im variable is the image of the screenshot
Source: https://pypi.python.org/pypi/pyscreenshot
Related
Environment:
win10 PC notbook
python 3.9
PyQt5==5.15.4 pywin32==304 opencv-python==4.5.5.64 Pillow==9.1.0
Question:
I am trying to grab my whole screen by python. The function is easy, but I get a strange question.
The screenshot image is always blurry and unsharp.
screenshot by windows10 self: windows10 self (PNG)
screenshot by my codes: my codes (PNG)
My Codes as follows:
import cv2
import os
from PIL import ImageGrab
import numpy as np
if os.path.exists('xx.jpg'):
os.remove('xx.jpg')
captureImage = ImageGrab.grab()
width,height = captureImage.size
img = cv2.cvtColor(np.asarray(captureImage), cv2.COLOR_RGB2BGR)
cv2.imwrite('./xx.jpg',img)
cv2.waitKey(0)
I have try alternative method to achieve screenshot function, just like pyqt5/mss/pywin32/pyautogui, but every method leads to the same question.
And I google lots of articles, I found that my screen has scaled to 125%(as recommended), as follow:
PNG:
So I revise it to 100%, the screenshot is sharp. But 125% is normal and comfortable vision.
Then I doubt if it's DPI. But I try these codes, it does not work.
from ctypes import windll
user32 = windll.user32
user32.SetProcessDPIAware()
I am so crazy and curious that how those screenshot software resolve it. After all, we can not ensure every user has the 100% scale and has normal dpi.
I will be grateful if u could give me solutions or advices.
Solution:
Thanks to Christoph Rackwitz.
I had grab a PNG screen and update it and the two PNG image still same. But it's really different in my PC before upload.
I test a lot and I found that I open my codes screenshot by iQIYI Player. But I open windows10 self screenshot by windows Player.
It's the Reason!!!My codes is right and everything is right except iQIYI Player Software. Maybe it's the software bug.
Thanks again.
So, with python i've seen that you can open a website using the code
import webbrowser
webbrowser.open("http://www.example.com", new=2)
but i really want to know how you could close the same specific tab using python.
If you can help please do. Thank you.
webbrowser module has no methods to close browser's tabs
I don't think you can. You might wanna try Selenium though.
How to open and close a website using default browser with python
I need to write a python script which opens a website and when the website is completly opened it takes a screenshot of the opened website.
I wrote sth like this:
import webbrowser
import wx
wx.App()
link = "http://stackoverflow.com/questions"
webbrowser.get('firefox %s').open_new_tab(link)
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem
bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)
It only opens a new tab in firefox but it doesnt take a screenshot of it :(
I want the solution to be cross-platform. Thanks for any help:)
EDIT:
The main problem here is that the script musnt take a picture BEFORE my webpage is completly opened. How to solve that issue?
I believe that you need different solutions for different operating systems. Use sys.platform to find out on which platform you are on. The rest you have to figure out yourself, but a quick internet search revealed:
On Linux systems, you can then take a screenshot as described here:
Take a screenshot via a python script. [Linux]
On Windows systems, you can base your solution on this answer:
Fastest way to take a screenshot with python on windows
On Mac systems, this will help:
Take screenshot in Python on Mac OS X
This solution is fairly cross-platform, but if you're trying to show a bit of a web page open in a desktop with menu/toolbars etc... it's not what you want.
You could use SeleniumHQ to automate a browser of your choice, have that browser render the page, then get the complete image of the rendered page - ie, not just a screenshot of the portion of the page that's displayed on screen. You could then crop that accordingly.
According to this question, you should be able to use ImageGrab to take a screenshot. Since ImageGrab uses PIL, this should be cross-platform. Here is some sample code to get you on your way
Hope this helps
I want to open a page and grab the screen and obtain its mean color with PIL. I want to open about 100 pages so I thought a screen capture script with Python would be useful. I found several of them and I decided to use this simple one:
"""
A simple screen grabbing utility
#author Fabio Varesano -
#date 2009-03-17
"""
from PIL import ImageGrab
import time
time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")
But I need to run this from Command Prompt which then shows on the screen. How do I make this into a script so that I can call it programmatically? Or, what do you think is the best way to achieve this? Thanks!
You can use Selenium and any browser:
Take a screenshot with Selenium WebDriver
This is cross-platform, works on every browser.
Selenium is designed for functional web browser testing, but is suitable also for use cases like yours. More info about Python + Selenium:
http://pypi.python.org/pypi/selenium
The benefit of Selenium is that you have fine-tuned control of the browser - you can click buttons, execute Javascript, whatever you wish and the browser is no longer a black box.
This solution seems to do exactly what you want by bringing the browser to the foreground before performing the screen grab. Just replace 'firefox' with 'chrome'
I use python on Ubuntu, and I am (as my name suggests) a complete noob at it all. I want to insert a very basic image, I have looked around, copied coding etc, but mothing seems to work. I want to insert an image, nothing else, so it just appears on the screen when I run it in shell. Please help? and please explain in simple terms, i'd like to know what I'm actually doing! Thank-you in advance.
you can install PIL imagemagick
then:
import Image
im = Image.open("your_image")
im.show()
I would try something like:
import webbrowser
webbrowser.open('file:///path/to/my/file.jpg')