Predictive Hacks

How to resize an image and convert it to byte array

Assume that the API that you work, like the AWS Rekognition, takes as input images in a byte format with a size limit. Thus, you may need to resize the image and then to convert it to a byte array. Let’s see how we can do it:

from PIL import Image
import io


photo = 'my_image.jpg'

#open the image
img = Image.open(photo, mode='r')

# resize the image
img = img.resize((300,300))
    
byteIO = io.BytesIO()
img.save(byteIO, format='PNG')
byteArr = byteIO.getvalue()

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s