Reputation: 1
I'm working on CNN using Python and Tensorflow. How can I convert images from PNG to JPEG in this code?
I have some ideas about using for loop and maybe PIL image module, but I have no experience so I don't know how to make it. I'd like to make this transformation in any possible way.
ATTENTION: Layers aren't included because it's big block of code, but in my Jupyter notebook I have them.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
import numpy as np
import os
train_dir = os.path.join('D:/nnfp')
train_tank_dir = os.path.join('D:/nnfp/Tank')
train_plane_dir = os.path.join('D:/nnfp/Planes')
train_datagen = ImageDataGenerator(rescale = 1.0/255)
train_generator = train_datagen.flow_from_directory(train_dir,
target_size = (300,300),
batch_size = 128,
class_mode = 'binary')
model.compile(optimizer = RMSprop(lr = 0.001),
loss = 'binary_crossentropy',
metrics = ['accuracy'])
model.fit_generator(train_generator,
steps_per_epoch = 15,
epochs = 15,
#validation_data = validation_generator,
#validation_steps = 2,
verbose = 1,
callbacks = [callbacks])
I would like to convert all my data by adding some code in already existing if it's possible.
Upvotes: 0
Views: 1556
Reputation: 2689
You can use PIL
library for converting images from png
to jpeg
by using convert()
method defined in the PIL
library.
For example:
from PIL import Image
image = Image.open("<Image-Name>.png")
image_rgb = im.convert('RGB')
image_rgb.save('<Image-Name-Converted>.jpg')
I hope it helps!
You can read the docs here for details: https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.convert
Upvotes: 1