Making a nice screenshot

The main reason this particular post exists is to be able to show something I'm marginally proud of. This is about access to a challenge by Google with little problems to be solved with code, either Java or Python. I would say "bragging rights", but I believe anybody who regularly uses certain search queries on google.com will be offered access to the site eventually.

So I wanted to make a screenshot of the webpage. Is any screenshot good enough? No it has to be neat and to my defined size. Can the size of a window be set in Windows? After a thorough search on the internet, no it can't.

Are there other possibilities? Maybe it is possible on linux. I have a virtual environment installed with Manjaro (Arch-based), LxQt, and Openbox as a window manager. Am I using x11 or wayland? Find out how to check for that.

First check the type of display server. Following this question, querying loginctl show-session <#NUMBER> -p Type, where #NUMBER can be found by querying loginctl. Apparently I'm still on x11.

I've found a script that uses xdotool to manipulate window sizes, which depends on the xlib library. So for now it's a good thing my linux environment uses x11.

With a slight modification (I've added sleep 3s && to allow time to select the correct window) this script returns the dimensions of the active window.

bash; getWindowSize.sh
#!/bin/bash
# Get the coordinates of the active window's
#    top-left corner, and the window's size.
#    This excludes the window decoration.
unset x y w h
eval $(xwininfo -id $(sleep 3s && xdotool getactivewindow) |
    sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
           -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
           -e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
           -e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )
echo "$x $y $w $h"

That's nice. But manually trying to set it to the right size, and checking each iteration with this script would be tedious. Instead xdotool can also be used to set window size, as shown here.

Before this works, we need the name of the window. With the query xdotool getactivewindow getwindowname we get the title. Than it's just a matter of combining some of this stuff, so that the window changes to the specified size. With xdotool search --name "windowName" windowsize 640 480 we end up with a window, that has the exact same dimensions as the following image.

This was a nice little problem to solve. Next up is solving the problems of the challenge itself.

Comments

Popular posts from this blog

Quick and dirty image searches

Evolving an image, part I - genome generation

Evolving an image, part II - image rendering