Relative path for OpenCV imread in python
main_image = cv2.imread('data/image.jpg')
img1 = os.path.join(os.path.dirname(__file__), 'images', 'main_image.jpg') main_image = cv2.imread(img1)
main_image = cv2.imread('data/image.jpg')
img1 = os.path.join(os.path.dirname(__file__), 'images', 'main_image.jpg') main_image = cv2.imread(img1)
import pyautogui screenWidth, screenHeight = pyautogui.size() currentMouseX, currentMouseY = pyautogui.position() pyautogui.moveTo(100, 150) pyautogui.click() pyautogui.moveRel(None, 10) # move mouse 10 pixels down pyautogui.doubleClick() pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds. pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key pyautogui.press('esc') pyautogui.keyDown('shift') pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left']) pyautogui.keyUp('shift') pyautogui.hotkey('ctrl', 'c')
References
https://pyautogui.readthedocs.io/en/latest/introduction.html
To get the work area size (desktop minus taskbar):
from win32api import GetMonitorInfo, MonitorFromPoint monitor_info = GetMonitorInfo(MonitorFromPoint((0,0))) work_area = monitor_info.get("Work") print("The work area size is {}x{}.".format(work_area[2], work_area[3]))
To get the taskbar height:
from win32api import GetMonitorInfo, MonitorFromPoint monitor_info = GetMonitorInfo(MonitorFromPoint((0,0))) monitor_area = monitor_info.get("Monitor") work_area = monitor_info.get("Work") print("The taskbar height is {}.".format(monitor_area[3]-work_area[3]))
import cv2 import numpy as np img1 = cv2.imread("img1.png") img2 = cv2.imread("img2.png") diff = cv2.absdiff(img1, img2)) mask = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) th = 1 imask = mask>th canvas = np.zeros_like(img2, np.uint8) canvas[imask] = img2[imask] cv2.imwrite("result.png", canvas)
References
https://stackoverflow.com/questions/27035672/cv-extract-differences-between-two-images
from PIL import ImageGrab im = ImageGrab.grab() im.save('screenshot.png')
Or
import win32gui, win32ui, win32con, win32api hwin = win32gui.GetDesktopWindow() width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN) top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) hwindc = win32gui.GetWindowDC(hwin) srcdc = win32ui.CreateDCFromHandle(hwindc) memdc = srcdc.CreateCompatibleDC() bmp = win32ui.CreateBitmap() bmp.CreateCompatibleBitmap(srcdc, width, height) memdc.SelectObject(bmp) memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY) bmp.SaveBitmapFile(memdc, 'screenshot.bmp')
Don’t forget to SetProcessDPIAware if necessary
import ctypes user32 = ctypes.windll.user32 user32.SetProcessDPIAware()
References
https://stackoverflow.com/questions/4589206/python-windows-7-screenshot-without-pil
import ctypes user32 = ctypes.windll.user32 user32.SetProcessDPIAware() screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1) print(screensize)
References
https://stackoverflow.com/questions/3129322/how-do-i-get-monitor-resolution-in-python
https://stackoverflow.com/questions/32541475/win32api-is-not-giving-the-correct-coordinates-with-getcursorpos-in-python
When you add new fonts to your system, you need to delete your fontList.cache file in order for matplotlib to find them.
The file fontList.cache is located at your Userfolder –> .matplotlib/fontList.cache
or ~/.cache/matplotlib/fontList.cache
, for Windows that would normally be C:\Users\yourUsername\.matplotlib\fontList.cache
matplotlib.font_manager matplotlib.font_manager._rebuild()
References
https://stackoverflow.com/questions/26085867/matplotlib-font-not-found
http://andresabino.com/2015/08/18/fonts-and-matplotlib/
https://stackoverflow.com/questions/45877746/changing-fonts-in-matplotlib
# Create your ticker object with M ticks M = 3 yticks = ticker.MaxNLocator(M) # Set the yaxis major locator using your ticker object. You can also choose the minor # tick positions with set_minor_locator. ax.yaxis.set_major_locator(yticks)
References
https://stackoverflow.com/questions/27425974/change-ticks-number-on-a-subplot
plt.savefig("test.png",bbox_inches='tight')
References
https://stackoverflow.com/questions/11837979/removing-white-space-around-a-saved-image-in-matplotlib
from flask import send_file @app.route('/get_image') def get_image(): if request.args.get('type') == '1': filename = 'ok.gif' else: filename = 'error.gif' return send_file(filename, mimetype='image/gif')
References
https://stackoverflow.com/questions/8637153/how-to-return-images-in-flask-response
https://pythonprogramming.net/flask-send-file-tutorial/