user961627
user961627

Reputation: 12747

PIL images converted to RGB, getting saved as plain black images (python)

I have images that I want to save in jpeg format, after shearing and editing them.

This is my function in python:

import numpy as np
from skimage import data, io, filter, color, exposure
import skimage.transform as tf
from skimage.transform import resize, rescale, rotate, setup, warp, AffineTransform
import os
from os import listdir
from os.path import isfile, join
from PIL import Image


def generateHoGSamples(path, readfile):
    print "generating samples from  " + path+"\\"+readfile
    img = color.rgb2gray(io.imread(path+"\\"+readfile))
    img = resize(img, (50,100))
    filename = os.path.splitext(readfile)[0]
    angles = [3, 0, -3]
    shears = [0.13, 0.0, -0.13]
    imgidx = 0
    for myangle in angles:
        myimg = rotate(img, angle=myangle, order=2)
        for myshear in shears:
            imgidx+=1
            afine_tf = tf.AffineTransform(shear=myshear)
            mymyimg = tf.warp(myimg, afine_tf)
            outputimg = Image.fromarray(mymyimg)
            # Saving as "jpg" without the following line caused an error
            outputimg = outputimg.convert('RGB')
            outputimg.save(path+"//"+str(imgidx)+".jpg", "JPEG")

But what happens instead is that all images are nothing but black. What's the matter here?

Upvotes: 3

Views: 7729

Answers (1)

mrcl
mrcl

Reputation: 2190

Your image mymyimage goes from 0 to 1 and PIL is expecting an image with values between 0 and 255. During the truncation your jpeg image will have truncated values 0 or 1, resulting in black.

To fix that, just multiply mymyimg by 255, such as

outputimg = Image.fromarray(mymyimg*255)

Hope it helps.

Cheers

Upvotes: 8

Related Questions