python-kerasHow do I use Python and Keras to resize an image?
Using Python and Keras, you can resize an image by using the ImageDataGenerator
class. This class allows you to easily preprocess images, including resizing them.
To use this class, first import the ImageDataGenerator
class from Keras:
from keras.preprocessing.image import ImageDataGenerator
Then, create an instance of the ImageDataGenerator
class, and set the rescale
parameter to the desired size:
datagen = ImageDataGenerator(rescale=1./255)
Finally, use the flow_from_directory
method to generate batches of images, specifying the directory containing the images and the desired target size:
generator = datagen.flow_from_directory(
'data/',
target_size=(224, 224),
batch_size=32,
class_mode='categorical')
The above code will resize all images in the data/
directory to a size of (224, 224).
Code explanation
from keras.preprocessing.image import ImageDataGenerator
: imports theImageDataGenerator
class from Kerasdatagen = ImageDataGenerator(rescale=1./255)
: creates an instance of theImageDataGenerator
class, and sets therescale
parameter to the desired sizegenerator = datagen.flow_from_directory('data/', target_size=(224, 224), batch_size=32, class_mode='categorical')
: uses theflow_from_directory
method to generate batches of images, specifying the directory containing the images and the desired target size
Helpful links
More of Python Keras
- How do I use Python Keras to zip a file?
- How do I use validation_data when creating a Keras model in Python?
- How do I use Python and Keras to create a VGG16 model?
- How can I enable verbose mode when using Python Keras?
- How can I resolve the issue of Python module Tensorflow.keras not being found?
- How do I check which version of Keras I am using in Python?
- How do I use zero padding in Python Keras?
- How can I improve the validation accuracy of my Keras model using Python?
- How can I use YOLO with Python and Keras?
- How can I use Python and Keras to create a Variational Autoencoder (VAE)?
See more codes...