在剪切和编辑之后,我想要以jpeg格式保存图像。
这是我在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")
但相反的是,所有图像都只是黑色。 这是怎么回事?
答案 0 :(得分:3)
您的图片mymyimage
从0变为1,PIL
期待图片的值介于0到255之间。在截断期间,您的jpeg图片将截断值0或1,从而产生黑色。
要解决此问题,只需将mymyimg
乘以255,例如
outputimg = Image.fromarray(mymyimg*255)
希望它有所帮助。
干杯