Screenshot region between two images and multiple monitors - pyautogui

I am trying to take a screenshot using multiple monitors and having no luck. Code below
import pyautogui
f_cast = pyautogui.locateOnScreen('wsb-For.png', confidence="0.85")
end_cast = pyautogui.locateOnScreen('wsb-end.png', confidence="0.85")
screenshot = pyautogui.screenshot(region = (f_cast,end_cast))
screenshot.save("screen.png")

from PIL import ImageGrab
from functools import partial
ImageGrab.grab = partial(ImageGrab.grab, all_screens=True)
import pyautogui
f_cast = pyautogui.locateOnScreen('wsb-For.png', confidence="0.85")
end_cast = pyautogui.locateOnScreen('wsb-end.png', confidence="0.85")
screenshot = pyautogui.screenshot()
screenshot.save("screen.png")
Source

Related

Image locating with pyautogui

With pyautogui I'm trying to locate a object in a screenshot that is being taken but I can't find a way
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
time.sleep(2)
ims = pica = pyautogui.screenshot(region=(569,381,800,450))
iml = pyautogui.screenshot(region=(1040,295,100,30))
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
#Color of center: (255, 219, 195)
while keyboard.is_pressed('q') == False:
flag = 0
pyautogui.screenshot(region=(1040,295,100,30))
ims.save(r"C:\Users\Billy Bong\Desktop\python\saveimage.png")
pica = pyautogui.screenshot(region=(569,381,800,450))
ims.save(r"C:\Users\Billy Bong\Desktop\python\saveimage2.png")
time.sleep (2)
pyautogui.locateAllOnScreen("saveimage.png", confidence=0.6)
print (cords)
pyautogui.click (cords)
time.sleep (10)
break
Here is the minimal code for locating an object on the screen, you should have the image of the object.
import pyautogui as py
import keyboard
while True:
img = py.locateOnScreen("object.png", confidence=0.9) # add path to your object image here
print(img)
if keyboard.is_pressed('q'):
break
there is something i found out about pyautogui if u gonna use locateonscreen or locateallonscreen the image u asking him to locate must be in same folder u running the script from (according to my experience from using VS-code) and idk if there is a way to tell it to set a path for the image u wanna search to (something) so for the current time make sure u include all images u search for in same folder that the script is running from for me it is (E:\coding\game) cuz i make auto bots for games i like
btw locateallonscreenn prints out a list, the "cords" but idk if "pyautogui.click (cords)
" understand that and if it is going to click all the x,y in cords or not

Layout Parser: lp.draw_box not working in for loop

I am trying to visualize pages after passing through layout parser using lp.draw_box. It works fine for a single image, but if I run through a for loop and try for each image at once from a set of images, it doesn't print anything. Could someone please help me here? THanks
Code:
from pdf2image import convert_from_path, convert_from_bytes
import cv2, time
start = time.time()
images = convert_from_path(f"/content/neft-system-faq.pdf")
page_layout_map = {}
for idx, image in enumerate(images):
image.save('out.png', 'PNG')
image = cv2.imread("out.png")
image = image[..., ::-1]
layout = model.detect(image)
lp.draw_box(image, layout,None,show_element_id=True)
text_blocks = lp.Layout([b for b in layout])
print(text_blocks)
block_test = get_coordinates(text_blocks)
result_arr = layout_map_processor(block_test)
page_layout_map[idx] = result_arr
print(time.time()-start)

In Gtk, how to make a window smaller when creating

I am trying to display both an image and a box with an Entry widget. I can do that, but the window is so large that the widget at the bottom is mostly out of view. I have tried several calls to set the window's size or unmaximize it, but they seem to have no effect. I determined that the problem only occurs when the image is large, but still wonder how to display a large image in a resizable window or, for that matter, to make any changes to the window's geometry from code. All the function call I tried seem to have no effect.
Here is my code:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from urllib.request import urlopen
class Display(object):
def __init__(self):
self.window = Gtk.Window()
self.window.connect('destroy', self.destroy)
self.window.set_border_width(10)
# a box underneath would be added every time you do
# vbox.pack_start(new_widget)
vbox = Gtk.VBox()
self.image = Gtk.Image()
response = urlopen('http://1.bp.blogspot.com/-e-rzcjuCpk8/T3H-mSry7PI/AAAAAAAAOrc/Z3XrqSQNrSA/s1600/rubberDuck.jpg').read()
pbuf = GdkPixbuf.PixbufLoader()
pbuf.write(response)
pbuf.close()
self.image.set_from_pixbuf(pbuf.get_pixbuf())
self.window.add(vbox)
vbox.pack_start(self.image, False, False, 0)
self.entry = Gtk.Entry()
vbox.pack_start(self.entry, True,True, 0)
self.image.show()
self.window.show_all()
def main(self):
Gtk.main()
def destroy(self, widget, data=None):
Gtk.main_quit()
a=Display()
a.main()
Most of the posted information seems to pertain to Gtk2 rather than Gtk3, but there is a solution: to use a pix buf loader and set the size:
from gi.repository import Gtk, Gdk, GdkPixbuf
#more stuff
path = config['DEFAULT']['datasets']+'working.png'
with open(path,'rb') as f:
pixels = f.read()
loader = GdkPixbuf.PixbufLoader()
loader.set_size(400,400)
loader.write(pixels)
pb = GdkPixbuf.Pixbuf.new_from_file(path)
self.main_image.set_from_pixbuf(loader.get_pixbuf())
loader.close()

Bokeh: Slider is not updating results on Hbar plot

I wrote the following code for using a slider to filter and update values on a Hbar plot in Bokeh.
The plot (as shown in the picture) outputs correctly, but when I move the slider nothing happens.
I'd greatly appreciate any feedback.
import pandas as pd
from bokeh.core.properties import value
from IPython.display import display, HTML
from bokeh.plotting import figure, show
from bokeh.layouts import row, column, gridplot
from bokeh.io import output_notebook, save, curdoc
from bokeh.models import ColumnDataSource, HoverTool, DatetimeTickFormatter, FactorRange, DataTable, TableColumn, DateFormatter
from bokeh.models.widgets import Panel, Tabs, Slider
import matplotlib.pyplot as plt
xls=pd.ExcelFile(path)
test_data=pd.read_excel(xls, 'test_data')
display(test_data)
AREA counts
A 500
B 100
C 70
D 50
E 40
F 20
G 10
H 2
def myplot(doc):
source = ColumnDataSource(pd.DataFrame(data=test_data))
area_list=source.data['AREA'].tolist()[::-1]
# Creating the Bar Chart
p = figure(y_range=area_list ,plot_height=500, plot_width=500, title="Total counts per area",
x_axis_label='counts', y_axis_label='AREA')
p.hbar(y='AREA', right='counts', height=1,
line_color="black", fill_color='red',line_width=1,
source=source)
def update_plot(attr, old, new):
Number_of_counts = slider.value
new_data = test_data.loc[test_data['counts'] >=Number_of_counts]
source = ColumnDataSource(data=new_data)
# Make a slider object: slider
slider = Slider(start=1, end=100, step=1, value=1, title='counts')
# Attach the callback to the 'value' property of slider
slider.on_change('value', update_plot)
doc.add_root(column(slider, p))
show(myplot)
You're replacing the value of the source variable, but the old source is still there, being used by all the created models.
Instead of recreating the source, try to reassign the data attribute of the old source:
# source = ColumnDataSource(data=new_data)
source.data = ColumnDataSource.from_df(new_data)

How to align a text of cell in a table to center using python-docx?

I searched a lot but i am not able to get through it. If anybody has experience of python-docx can help be in few thing as below.
My code as under:
from docx import Document
from docx.shared import Cm, Inches
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.table import WD_ALIGN_VERTICAL
from docx.enum.section import WD_ORIENT
from docx.enum.style import WD_STYLE_TYPE
from docx.enum.text import WD_ALIGN_PARAGRAPH
chart = Document()
style = chart.styles['Normal']
font = style.font
from docx.shared import Pt
font.size = Pt(12)
font.underline = False
# Table Starts
print("Table Making Starts..")
table = chart.add_table(rows=1, cols=2, style='Table Grid')
# Adding Table Heading Text
cell0 = table.cell(0, 0)
cell0.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
cell0.bold = True
cell0.text = "Column1"
cell1 = table.cell(0, 1)
cell1.bold = True
cell1.text = "Column2"
cell1.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
chart.save('./test7.docx')
But the problem is that it is not doing formatting to align the text to center.
My Code Output:
Expected Output: