Categories
Python Answers

How to convert a PIL Image into a NumPy array with Python?

Sometimes, we want to convert a PIL Image into a NumPy array with Python.

In this article, we’ll look at how to convert a PIL Image into a NumPy array with Python.

How to convert a PIL Image into a NumPy array with Python?

To convert a PIL Image into a NumPy array with Python, we call the Image.open method from PIL.

Next, we call convert with 'L' to convert that to an image object that we can pass into numpy.array.

Then we call numpy.array with the returned image object to return a NumPy array.

For instance, we write:

import numpy
from PIL import Image

img = Image.open("art.png").convert("L")
imgarr = numpy.array(img)
print(imgarr)

to call Image.open with the path to the image file.

Then we call convert with 'L' to return image object that we pass into numpy.array to generate the array and return it,

Therefore, imgarr is something like:

[[29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]
 ...
 [29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]
 [29 29 29 ... 29 29 29]]

Conclusion

To convert a PIL Image into a NumPy array with Python, we call the Image.open method from PIL.

Next, we call convert with 'L' to convert that to an image object that we can pass into numpy.array.

Then we call numpy.array with the returned image object to return a NumPy array.

Categories
Python Answers

How to fix the ‘NumPy array is not JSON serializable’ issue with Python?

Sometimes, we want to fix the ‘NumPy array is not JSON serializable’ issue with Python.

In this article, we’ll look at how to fix the ‘NumPy array is not JSON serializable’ issue with Python.

How to fix the ‘NumPy array is not JSON serializable’ issue with Python?

To fix the ‘NumPy array is not JSON serializable’ issue with Python, we can use the json.dump method with the codecs.open method and the NumPy array’s tolist method.

For instance, we write:

import numpy as np
import codecs, json

a = np.arange(10).reshape(2, 5)
b = a.tolist()
file_path = "data.json"
json.dump(b,
          codecs.open(file_path, 'w', encoding='utf-8'),
          separators=(',', ':'),
          sort_keys=True,
          indent=2)

We create an array with numbers 0 to 9 with np.arange(10).

And we convert that to a 2×5 array with reshape(2, 5).

Next, we call a.tolist to convert the a NumPy array to a list.

Then we call json.dump with b, the file to write to, the separators to insert between entries, whether to sort keys, and the number of spaces to indent to generate the JSON file from the b list.

codecs.open opens the data.json for writing.

And so data.json has:

[
  [
    0,
    1,
    2,
    3,
    4
  ],
  [
    5,
    6,
    7,
    8,
    9
  ]
]

Conclusion

To fix the ‘NumPy array is not JSON serializable’ issue with Python, we can use the json.dump method with the codecs.open method and the NumPy array’s tolist method.

Categories
Python Answers

How to create a GUID or UUID in Python?

Sometimes, we want to create a GUID or UUID in Python.

In this article, we’ll look at how to create a GUID or UUID in Python.

How to create a GUID or UUID in Python?

To create a GUID or UUID in Python, we can use the uuid module.

For instance, we write:

import uuid

id = uuid.uuid4()
print(id)

We call uuid.uuid4 to return a v4 UUID as a string.

Therefore, id is something like 'c55b58d6-aa0d-498e-96be-433f99492cfb'.

Conclusion

To create a GUID or UUID in Python, we can use the uuid module.

Categories
Python Answers

How to delete a character from a string using Python?

Sometimes, we want to delete a character from a string using Python.

In this article, we’ll look at how to delete a character from a string using Python.

How to delete a character from a string using Python?

To delete a character from a string using Python, we can use the string’s replace method.

For instance, we write:

oldstr = 'EXAMPLE'
newstr = oldstr.replace("M", "")
print(newstr)

We call oldstr.replace with 'M' and an empty string to remove ‘M’ from 'EXAMPLE' and return the new string

Therefore, newstr is 'EXAPLE'.

Conclusion

To delete a character from a string using Python, we can use the string’s replace method.

Categories
Python Answers

How to display a float with two decimal places with Python?

Sometimes, we want to display a float with two decimal places with Python.

In this article, we’ll look at how to display a float with two decimal places with Python.

How to display a float with two decimal places with Python?

To display a float with two decimal places with Python, we can use the string’s format method.

For instance, we write:

n = "{:.2f}".format(5)
print(n)

We call format on "{:.2f}" with a number to render the number 5 with 2 decimal places.

Therefore, n is 5.00.

Conclusion

To display a float with two decimal places with Python, we can use the string’s format method.