Categories
Python Answers

How to take a screenshot via a Python script on Linux

Sometimes, we want to take a screenshot via a Python script on Linux.

In this article, we’ll look at how to take a screenshot via a Python script on Linux.

How to take a screenshot via a Python script on Linux

To take a screenshot via a Python script on Linux, we can use pyscreenshot.

To install it, we run

pip install Pillow pyscreenshot

Then we use it by writing

import pyscreenshot as ImageGrab

im = ImageGrab.grab()
im.show()

im = ImageGrab.grab(bbox=(10, 10, 500, 500))
im.show()

ImageGrab.grab_to_file('im.png')

to call grab to take a screenshot of the whole screen by calling it without arguments.

We call grab with the bbox argument to take the screenshot of part of the screen.

And we call grab_to_file to take a screenshot and save it to a file.

Conclusion

To take a screenshot via a Python script on Linux, we can use pyscreenshot.

Categories
Python Answers

How to fix Matplotlib savefig outputs blank image with Python?

Sometimes, we want to fix Matplotlib savefig outputs blank image with Python.

In this article, we’ll look at how to fix Matplotlib savefig outputs blank image with Python.

How to fix Matplotlib savefig outputs blank image with Python?

To fix Matplotlib savefig outputs blank image with Python, we call gcf and show before we call savefig.

For instance, we write

fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('test.png', dpi=100)

to call gcf to get the current figure and save it.

Then we call show

And then we save the figure saved by calling gcf by calling savefig with the file name.

Conclusion

To fix Matplotlib savefig outputs blank image with Python, we call gcf and show before we call savefig.

Categories
Python Answers

How to run commands over ssh with Python?

Sometimes, we want to run commands over ssh with Python.

in this articl,e we’ll look at how to run commands over ssh with Python.

How to run commands over ssh with Python?

To run commands over ssh with Python, we can use the Paramiko library.

To install it, we run

pip install paramiko

Then we can use it by writing

import paramiko

ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)

to create an ssh client with

ssh = paramiko.SSHClient()

Then we call ssh.connect to connect to the server with the username and password.

And then we call exec_command to run the command we want.

If we use ssh keys, we write

import paramiko

k = paramiko.RSAKey.from_private_key_file(keyfilename)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, pkey=k)

to load the private key file with

paramiko.RSAKey.from_private_key_file(keyfilename)

Then we make the connection with connect.

Conclusion

To run commands over ssh with Python, we can use the Paramiko library.

Categories
Python Answers

How to compare object instances for equality by their attributes with Python?

Sometimes, we want to compare object instances for equality by their attributes with Python.

In this article, we’ll look at how to compare object instances for equality by their attributes with Python.

How to compare object instances for equality by their attributes with Python?

To compare object instances for equality by their attributes with Python, we can add the __eq__ method into our class.

For instance, we write

class MyClass:
    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar
        
    def __eq__(self, other): 
        if not isinstance(other, MyClass):
            return NotImplemented
        return self.foo == other.foo and self.bar == other.bar

to create the MyClass class.

In it, we add the foo and bar instance variables.

And we add the __eq__ method to let us check for object equality by checking the value of each attribute.

We use

self.foo == other.foo and self.bar == other.bar

to do the check if self and other are both MyClass instances.

Then we can do the equality check with x == y where x and y are both MyClass instances.

Conclusion

To compare object instances for equality by their attributes with Python, we can add the __eq__ method into our class.

Categories
Python Answers

How to get string objects instead of Unicode from JSON with Python?

Sometimes, we want to get string objects instead of Unicode from JSON with Python.

In this article, we’ll look at how to get string objects instead of Unicode from JSON with Python.

How to get string objects instead of Unicode from JSON with Python?

To get string objects instead of Unicode from JSON with Python, we can use the encode method.

For instance, we write

nl = json.loads(js)
nl = [s.encode('utf-8') for s in nl]

to call json.loads to load the js JSON string array string into the nl list.

Then we loop through the nl list to call encode with 'utf-8' on each string in the nl list to convert them to byte strings and put the converted strings in a new list.

Finally, we assign the returned list to `nl.

Conclusion

To get string objects instead of Unicode from JSON with Python, we can use the encode method.