text_plain_output_2.png text_plain_output_1.png
from PIL import Image

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import os

from PIL import Image
def change_image_channels(image, image_path):
    if image.mode == 'RGBA':
        r, g, b, a = image.split()
        image = Image.merge('RGB', (r, g, b))
        image.save(image_path)
    elif image.mode != 'RGB':
        image = image.convert('RGB')
        os.remove(image_path)
        image.save(image_path)
    return image
for i in range(212):
    if i >= 189 and i <= 195 or (i >= 203 and i <= 207):
        continue
    im = Image.open('/kaggle/input/fruit-detection/images/fruit{}.png'.format(i))
    change_image_channels(im, 'fruit_change{}.png'.format(i))

import matplotlib.pyplot as plt
ims = np.zeros((200, 300, 400, 3))
q = 0
for i in range(212):
    if i >= 189 and i <= 195 or (i >= 203 and i <= 207):
        continue
    try:
        im = plt.imread('fruit_change{}.png'.format(i))
        ims[q, :, :, :] = im
        q = q + 1
        print(q)
    except:
        try:
            im = Image.open('fruit_change{}.png'.format(i))
        except:
            im = Image.open('/kaggle/input/fruit-detection/images/fruit{}.png'.format(i))
        imBackground = im.resize((400, 300))
        imBackground.save('ProcessedImage{}.png'.format(i), 'png')
        im = plt.imread('ProcessedImage{}.png'.format(i))
        size = im.shape
        x = size[0]
        y = size[1]
        channels = size[2]
        ims[q, 0:x, 0:y, 0:channels] = im
        q = q + 1
        print(q)