Categories
Python Answers

How to display image as grayscale using Python matplotlib?

Sometimes, we want to display image as grayscale using Python matplotlib.

In this article, we’ll look at how to display image as grayscale using Python matplotlib.

How to display image as grayscale using Python matplotlib?

To display image as grayscale using Python matplotlib, we can use thge imshow method with the cmap argument set to 'gray'.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

fname = 'image.png'
image = Image.open(fname).convert("L")
arr = np.asarray(image)
plt.imshow(arr, cmap='gray', vmin=0, vmax=255)
plt.show()

to open the image.png file with Image.open.

And then we convert the image to a NumPy array with np.asarray.

Then we call imshow with the arr NumPy array and cmap set to 'gray' to render the image as a grayscale image.

Then we call show to show the image.

Categories
Python Answers

How to show a file dialog in Python?

Sometimes, we want to show a file dialog in Python.

In this article, we’ll look at how to show a file dialog in Python.

How to show a file dialog in Python?

To show a file dialog in Python, we can call filedialog.askopenfilename.

For instance, we write

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

to call filedialog.askopenfilename to open the file dialog.

And once a file is selected, its path will be returned as the value.

Conclusion

To show a file dialog in Python, we can call filedialog.askopenfilename.

Categories
Python Answers

How to JSON serialize sets with Python?

Sometimes, we want to JSON serialize sets with Python.

In this article, we’ll look at how to JSON serialize sets with Python.

How to JSON serialize sets with Python?

To JSON serialize sets with Python, we can use the json.dumps method with a custom json.JSONEncoder subclass.

For instance, we write

import json

class SetEncoder(json.JSONEncoder):
   def default(self, obj):
      if isinstance(obj, set):
         return list(obj)
      return json.JSONEncoder.default(self, obj)

s = json.dumps(set([1,2,3,4,5]), cls=SetEncoder)

to create the SetEncoder class which is subclass of the json.JSONEncoder class.

In it, we add the default method.

And in the default method, we return the set converted to a list if the obj object is a set.

And then we call json.JSONEncoder.default(self, obj) to serialize the obj object.

Next, we call json.dumps with a set and the cls argument set to SetEncoder to use SetEncoder to serialize the set into a JSON string.

Conclusion

To JSON serialize sets with Python, we can use the json.dumps method with a custom json.JSONEncoder subclass.

Categories
Python Answers

How to run Selenium Webdriver with a proxy in Python?

Sometimes, we want to run Selenium Webdriver with a proxy in Python.

In this article, we’ll look at how to run Selenium Webdriver with a proxy in Python.

How to run Selenium Webdriver with a proxy in Python?

To run Selenium Webdriver with a proxy in Python, we can use the Proxy class.

For instance, we write

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"

capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)

to create a Proxy object.

Then we set the proxy settings for various protocols with

prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"

Next, we add the Proxy object to the capabilities with

prox.add_to_capabilities(capabilities)

And then we create the Chrome driver with the capabilities with

driver = webdriver.Chrome(desired_capabilities=capabilities)

to set the proxy settings when starting Chrome.

Conclusion

To run Selenium Webdriver with a proxy in Python, we can use the Proxy class.

Categories
Python Answers

How to call Python function from JavaScript code?

Sometimes, we want to call Python function from JavaScript code.

In this article, we’ll look at how to call Python function from JavaScript code.

How to call Python function from JavaScript code?

To call Python function from JavaScript code, we run the Python script from the Node.js script with spawn.

For instance, we write

const { spawn } = require("child_process");
const temperatures = [];

const sensor = spawn("python", ["sensor.py"]);
sensor.stdout.on("data", (data) => {
  temperatures.push(parseFloat(data));
  console.log(temperatures);
});

to call spawn with 'python' and an array with the arguments which includes the path to the script file to run.

Then we listen for stdout changes with

sensor.stdout.on("data", (data) => {
  temperatures.push(parseFloat(data));
  console.log(temperatures);
});

to get the output from data and so things with it in the callback.

Conclusion

To call Python function from JavaScript code, we run the Python script from the Node.js script with spawn.