How to check if a image has any text or not? - python

I'm looking for a simple solution that would return a boolean if ANY kind of English text is present in an image file. I wish to use this to detect memes. For example, the following file should be detected as an image with text.
I've come across elaborate machine learning techniques using OpenCV but I haven't been able to fully implement it. Is there any quicker, simpler, and just as effective solution for this?
I look forward to your valuable feedback!

There is indeed simple way with opencv and pytessaract after installing you will only need to use a few lines in order to get the text
pip install opencv-python
pip install pytesseract
import cv2
import pytesseract
img = cv2.imread('yourimage.jpeg')
text = pytesseract.image_to_string(img)
Read Text from Image with One Line of Python Code
Also if you don't like the first way you can use Google vision, keep in mind it will return Json and you will extract what you need.
https://cloud.google.com/vision/docs/ocr
Python Client for Google Cloud Vision

We can use pytesseract python package for get text form the images. You can easily install like pip install pytesseract
Here is the example code:
import cv2
import pytesseract
image = cv2.imread('test.jpeg')
text = pytesseract.image_to_string(image)
print(text)
Here is my sample image
So, the output should be like
IS BITCOIN
GOING TO
$20.000
BY CHRISTMAS?

You can use OpenCV and pytesseract to perform your task.
import cv2
import pytesseract
img = cv2.imread('YOUR_IMAGE_PATH')
text = pytesseract.image_to_string(img)
print(text)

Related

How can I achieve a photoshop "bevel / emboss" like depth effect using Python?

I want to place an image on top of a background-image and apply a "bevel/emboss" effect on the image using Python. I made an attempt using the PIL library but suggestions for other libraries are welcome too.
This is what it should look like:
I have the following code:
from PIL import Image
from PIL import ImageFilter
img = Image.open('./image.jpeg', 'r')
# this doesn't do what I want...
img = img .filter(ImageFilter.EMBOSS)
background = Image.open('./bg.png', 'r')
background.paste(img)
I used Affinity Photo for the example image. Should be pretty much the same in Photoshop. Here are the settings I used:
I still don't know how I can do this in python but I found a way around it using a combination of the the batch job and the macro functionality in Affinity Photo.
record a macro that applies the desired effect to an image
place all images that need the effect in a folder
start a batch job and apply the macro to all images
How to start a batch job is described here: http://www.millermattson.com/blog/batch-processing-with-affinity-photo/

Tesseract unable to read simple number

I have this image and I need tesseract to read the value.
import cv2
import pytesseract
im = cv2.imread("num.png")
print(pytesseract.image_to_string(im))
It does not print anything. Am I doing something wrong since it is pretty clear that it is a 7.
Even after scaling the image up by 5x with intercubic it still would not work. This is the image now
As described here:
By default Tesseract expects a page of text when it segments an image. If you’re just seeking to OCR a small region, try a different segmentation mode, using the --psm argument.
In this case, --psm from 6 to 10 should work fine. Example:
pytesseract.image_to_string(im, config='--psm 6')
The code is correct. I think that image of 7 is not clear enough for pytesseract. You need to preprocess the image. This link might help.

OCR text extraction from user interfaces image

I am currently using Pytesseract to extract text from images like Amazon, ebay, (e-commerce) etc to observe certain patterns. I do not want to use a web crawler since this is about recognising certain patterns from the text on such sites. The image example looks like this:
However every website looks different so template matching wouldn't help as well. Also the image background is not of the same colour.
The code gives me about 40% accuracy. But if I crop the images into smaller size, it gives me all the text correctly.
Is there a way to take in one image, crop it into multiple parts and then extract text? The preprocessing of images does not help. What I have tried is using: rescaling, removing noise, deskewing, skewing, adaptiveThreshold, grey scale,otsu, etc but I am unable to figure out what to do.
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
# import pickle
def ocr_processing(filename):
"""
This function uses Pillow to open the file and Pytesseract to find string in image.
"""
text = pytesseract.image_to_data(Image.open(
filename), lang='eng', config='--psm 6')
# text = pytesseract.image_to_string(Image.open(
# filename), lang='eng', config ='--psm 11')
return text
Just for a recommendation if you have a lot of text and you want to detect it through OCR (example image is above), "Keras" is a very good option. Much much better than pytesseract or using just EAST. It was a suggestion provided in the comments section. It was able to trace 98.99% of the text correctly.
Here is the link to the Keras-ocr documentation: https://keras-ocr.readthedocs.io/en/latest/

Can OpenCV or PyTesseract recognize fonts

Using the below code I am able to read all text in an image:
import cv2
img = cv2.imread(r'/<path_to_image>/text.png')
print(pytesseract.image_to_string(img))
What I want to know is does OpenCV or PyTesseract support text extraction based on font name? For example, if particular text is in Times New Roman and the rest of the text is Arial only extract the Times New Roman. Something like this:
print(pytesseract.image_to_string(img, lang='font'))
Of course no. Tesseract hardly recognizes G from 6 and OpenCV is computer vision library.

How to find the last modified png file using python or django

I want to find which file is last modified.
I am modifying or updating the 'png' image on each request.
For example: piechart.png is an image; I am modifying this same image to create a new image (having same image name 'piechart.png'). I will be saving & modifying 5 images: (piechart1.png,piechart2.png,... ,piechart5.png).
How can I find which image was modified last ?
Well, this doesn't really answer your question, but it come close.
What you can do, is first download the PIL module, or the Python Imaging Library.
After that, get it installed, and then do the following:
from PIL import Image
Jpeg = Image.open("Yourfile.jpg")
print(Jpeg.info, Jpeg.format, Jpeg.mode, Jpeg.size)
This gives you some of the properties of the photo.
The same thing you can do for PNG images.
For more information about using the PIL module and getting your desired results you can go here:
http://python.developpez.com/cours/pilhandbook/php/image.php
Hope this helps!

Categories

Resources