Categories
Python Answers

How to use pytesseract OCR to recognize text from an image with Python?

Sometimes, we want to use pytesseract OCR to recognize text from an image with Python.

In this article, we’ll look at how to use pytesseract OCR to recognize text from an image with Python.

How to use pytesseract OCR to recognize text from an image with Python?

To use pytesseract OCR to recognize text from an image with Python, we call the image_to_string function.

For instance, we write

import pytesseract
from PIL import Image

text = pytesseract.image_to_string(Image.open("temp.jpg"), lang='eng',
                        config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')

print(text)

to open the image with PIL’s Image.open method.

Then we call image_tp_string with the --psm flag to read the image in as a string.

The string would have the characters in the opened image.

Conclusion

To use pytesseract OCR to recognize text from an image with Python, we call the image_to_string function.

Categories
Python Answers

How to find the max of two or more columns with Python Pandas?

Sometimes, we want to find the max of two or more columns with Python Pandas.

In this article, we’ll look at how to find the max of two or more columns with Python Pandas.

How to find the max of two or more columns with Python Pandas?

To find the max of two or more columns with Python Pandas, w ecan use the max method.

For instance, we write

df["C"] = df[["A", "B"]].max(axis=1)

to add column C to the df data frame by getting a data frame from with df‘s columns A and B with df[["A", "B"]] and call max on it.

We set axis to 1 to getc the max of the value in the row between the 2 columns.

Conclusion

To find the max of two or more columns with Python Pandas, w ecan use the max method.

Categories
Python Answers

How to override save for model with Python Django?

Sometimes, we want to override save for model with Python Django.

In this article, we’ll look at how to override save for model with Python Django.

How to override save for model with Python Django?

To override save for model with Python Django, we add our own save method to the model class.

For instance, we write

class Model(model.Model):
    _image=models.ImageField(upload_to='folder')
    thumb=models.ImageField(upload_to='folder')
    description=models.CharField()

    def set_image(self, val):
        self._image = val
        self._image_changed = True

        # Or put whole logic in here
        small = rescale_image(self.image,width=100,height=100)
        self.image_small=SimpleUploadedFile(name,small_pic)

    def get_image(self):
        return self._image

    image = property(get_image, set_image)

    def save(self, *args, **kwargs):
        if getattr(self, '_image_changed', True):
            small=rescale_image(self.image,width=100,height=100)
            self.image_small=SimpleUploadedFile(name,small_pic)
        super(Model, self).save(*args, **kwargs)

to add the save method into the Model class.

In it, we call rescale_image to rescale an image,

Then we create a SimpleUploadedFile object from the image.

And then we call save to save the Model instance with the parent class’ save method.

Conclusion

To override save for model with Python Django, we add our own save method to the model class.

Categories
Python Answers

How to use Python regular expression with Unicode?

Sometimes, we want to use Python regular expression with Unicode.

In this article, we’ll look at how to use Python regular expression with Unicode.

How to use Python regular expression with Unicode?

To use Python regular expression with Unicode, we can use the re.compile method.

For instance, we write

myre = re.compile(ur'[\u0000\u0000\u0000\u0000\u0000\u0000]+', 
                      re.UNICODE)

to call re.compile with the Unicode regex string ur'[\u0000\u0000\u0000\u0000\u0000\u0000]+'.

The 2nd argument is re.UNICODE so re.compile will treat the first argument as a Unicode regex string.

Conclusion

To use Python regular expression with Unicode, we can use the re.compile method.

Categories
Python Answers

How to convert a datetime object to seconds with Python?

Sometimes, we want to convert a datetime object to seconds with Python.

In this article, we’ll look at how to convert a datetime object to seconds with Python.

How to convert a datetime object to seconds with Python?

To convert a datetime object to seconds with Python, we can use the datetime timestamp method.

For instance, we write

from datetime import datetime

dt = datetime.today()
seconds = dt.timestamp()

to get today’s datetime with datetime.today.

Then we call timestamp on the returned datetime object to get the timestamp of it in seconds.

Conclusion

To convert a datetime object to seconds with Python, we can use the datetime timestamp method.