Categories
Python Answers

How to combine several images horizontally with Python?

Sometimes, we want to combine several images horizontally with Python.

In this article, we’ll look at how to combine several images horizontally with Python.

How to combine several images horizontally with Python?

To combine several images horizontally with Python, we can use PIL.

For instance, we write

import sys
from PIL import Image

images = [Image.open(x) for x in ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']]
widths, heights = zip(*(i.size for i in images))

total_width = sum(widths)
max_height = max(heights)

new_im = Image.new('RGB', (total_width, max_height))

x_offset = 0
for im in images:
  new_im.paste(im, (x_offset,0))
  x_offset += im.size[0]

new_im.save('test.jpg')

to open the images with Image.open.

Then we zip the image sizes together and get their width and height.

Next, we get the total_width and max_height by adding up the widths and heights.

Then we create a new image with Image.new with total_width and max_height.

Next, we add a for loop to put the pixels of the images into the new image.

Finally, we call save to save the new image.

Conclusion

To combine several images horizontally with Python, we can use PIL.

Categories
Python Answers

How to create case insensitive regular expression without re.compile with Python?

Sometimes, we want to create case insensitive regular expression without re.compile with Python.

In this article, we’ll look at how to create case insensitive regular expression without re.compile with Python.

How to create case insensitive regular expression without re.compile with Python?

To create case insensitive regular expression without re.compile with Python, we can pass in re.IGNORECASE to re.search, re.match, and re.sub.

For instance, we write

re.search('test', 'TeSt', re.IGNORECASE)
re.match('test', 'TeSt', re.IGNORECASE)
re.sub('test', 'foo', 'Testing', flags=re.IGNORECASE)

to call the 3 methods with a regex string and the re.IGNORECASE flag to make them search for matches for a pattern ignoring the case.

Conclusion

To create case insensitive regular expression without re.compile with Python, we can pass in re.IGNORECASE to re.search, re.match, and re.sub.

Categories
Python Answers

How to convert string in base64 to image and save on filesystem with Python?

Sometimes, we want to convert string in base64 to image and save on filesystem with Python.

In this article, we’ll look at how to convert string in base64 to image and save on filesystem with Python.

How to convert string in base64 to image and save on filesystem with Python?

To convert string in base64 to image and save on filesystem with Python, we can call the base64.decodebytes method.

For instance, we write

import base64

with open("imageToSave.png", "wb") as fh:
    fh.write(base64.decodebytes(img_data))

to call open to open imageToSave.png with write permission as a binary file.

Then we call write with the decoded base64 image data that we get from

base64.decodebytes(img_data)

to save the image as imageToSave.png.

img_data is a base64 string with the image data.

Conclusion

To convert string in base64 to image and save on filesystem with Python, we can call the base64.decodebytes method.

Categories
Python Answers

How to fix error UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte error with Python?

Sometimes, we want to fix error UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte error with Python.

In this article, we’ll look at how to fix error UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte error with Python.

How to fix error UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte error with Python?

To fix error UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte error with Python, we should call read to read the file into a string.

For instance, we write

with open(path, 'rb') as f:
    contents = f.read()

to call open to open the file at path as a binary with 'rb'.

Then we call f.read to read the file into a string within the with block.

Conclusion

To fix error UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte error with Python, we should call read to read the file into a string.

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.