Relevant Code:
import boto3
from PIL import Image
import base64
client = boto3.client('rekognition')
filename = r'C:\Users\H-63\Pictures\scantests\Rekognition test.JPG'
with open(filename, 'rb') as image_file:
image = image_file.read()
image = base64.b64encode(image).decode('UTF-8')
response = client.detect_text(
Image={'Bytes': image
})
However, When I run this, I get an error:
An error occurred (InvalidImageFormatException) when calling the DetectText operation: Request has Invalid image format
How do I get my image to be the right format for detect_text? The documentation says it has to be base64 encoding.
The Image encoding tool supports loading the Image File to transform to Base64. Click on the Upload Image button and select File. Image to Base64 Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.
In order to encode the image, we simply use the function base64. b64encode(s) . Python describes the function as follows: Encode the bytes-like object s using Base64 and return the encoded bytes.
I'm not sure why the documentation even mentions base64, but the function requires bytes. So just use:
with open(filename, 'rb') as image_file:
image = image_file.read()
client.detect_text(Image={'Bytes': image})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With