Categories
Python Answers

How to crop an image in OpenCV using Python?

Sometimes, we want to crop an image in OpenCV using Python.

In this article, we’ll look at how to crop an image in OpenCV using Python.

How to crop an image in OpenCV using Python?

To crop an image in OpenCV using Python, we can call cv.imread to read the image file.

Then we take a slice of the image file object with the square bracket notation.

And then we call cv2.imshow to display the image in a window.

For instance, we write:

import cv2

img = cv2.imread("test1.png")
crop_img = img[0:100, 0:150]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

We call cv2.imread with the path string for the image and assign it to img.

Then we crop the image by passing the start and end pixels of the y-axis, and start and end pixels of the x-axis respectively.

Both numbers are in pixels.

Next, we call cv2.imshow with the crop_img image to display the cropped image in a window.

Conclusion

To crop an image in OpenCV using Python, we can call cv.imread to read the image file.

Then we take a slice of the image file object with the square bracket notation.

And then we call cv2.imshow to display the image in a window.

Categories
Python Answers

How to get a list of all subdirectories in the current directory with Python?

Sometimes, we want to get a list of all subdirectories in the current directory with Python.

In this article, we’ll look at how to get a list of all subdirectories in the current directory with Python.

How to get a list of all subdirectories in the current directory with Python?

To get a list of all subdirectories in the current directory with Python, we can use the os.walk method.

For instance, we write:

import os

directory = '/'
dirs = [x[0] for x in os.walk(directory)]
print(dirs)

We call os.walk with the directory path to return an iterator with the tuples containing the directory path strings.

Then we can get them get the directory path string from each tuple with x[0].

Therefore, dirs is something like ['./', './.upm'].

Conclusion

To get a list of all subdirectories in the current directory with Python, we can use the os.walk method.

Categories
Python Answers

How to JSON serialize a Decimal object with Python?

Sometimes, we want to JSON serialize a Decimal object with Python.

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

How to JSON serialize a Decimal object with Python?

To JSON serialize a Decimal object with Python, we can use the json.dumps method from the simplejson module with use_decimal set to True.

To install it, we run:

pip install simplejson 

For instance, we write:

import simplejson as json
from decimal import Decimal

s = json.dumps(Decimal('3.9'), use_decimal=True)
print(s)

We use the Decimal constructor to create a decimal number object.

Then we call json.dumps from the simplejson module to convert the decimal number object to a number string.

Conclusion

To JSON serialize a Decimal object with Python, we can use the json.dumps method from the simplejson module with use_decimal set to True.

Categories
Python Answers

How to do an element-wise addition of 2 lists with Python?

Sometimes, we want to do an element-wise addition of 2 lists with Python.

In this article, we’ll look at how to do an element-wise addition of 2 lists with Python.

How to do an element-wise addition of 2 lists with Python?

To do an element-wise addition of 2 lists with Python, we can use the operator.add method.

For instance, we write:

from operator import add

list1 = [1, 2, 3]
list2 = [4, 5, 6]
added = list(map(add, list1, list2))
print(added)

We call map with add and list1 and list2 to add the entries in list1 and list2 element-wise.

Then we call list to convert the returned iterable object back to a list.

Therefore, added is [5, 7, 9].

Conclusion

To do an element-wise addition of 2 lists with Python, we can use the operator.add method.

Categories
Python Answers

How to get the first item from an iterable that matches a condition with Python?

Sometimes, we want to get the first item from an iterable that matches a condition with Python.

In this article, we’ll look at how to get the first item from an iterable that matches a condition with Python.

How to get the first item from an iterable that matches a condition with Python?

To get the first item from an iterable that matches a condition with Python, we can use the next function.

For instance, we write:

a = [1, 2, 3, 4, 5, 6]
first = next(x for x in a if x > 3)
print(first)

We define the list a that has some integers in it.

And we get the all the integers that’s bigger than 3 with x for x in a if x > 3 and return the list.

Finally, we get the first one on the returned list with next and assign it to first.

Therefore, first is 4.

Conclusion

To get the first item from an iterable that matches a condition with Python, we can use the next function.