Categories
Python Answers

How to convert Python date string to date object?

Sometimes, we want to convert Python date string to date object.

In this article, we’ll look at how to convert Python date string to date object.

How to convert Python date string to date object?

To convert Python date string to date object, we use the strptime method.

For instance, we write

import datetime

d = datetime.datetime.strptime('24052022', "%d%m%Y").date()

to call strptime with a date string and a string with the format of the date string to parse the date string into a datetime object.

Then we call date to get the date object.

%d is the 2 digit date.

%m is the 2 digit month.

And %Y is the 4 digit year.

Conclusion

To convert Python date string to date object, we use the strptime method.

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.