使用PIL和NumPy将图像转换为Lab数组,修改值,然后转换回 [英] Using PIL and NumPy to convert an image to Lab array, modify the values and then convert back

查看:1813
本文介绍了使用PIL和NumPy将图像转换为Lab数组,修改值,然后转换回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用NumPy将PIL图像转换为数组。然后,我想将该数组转换为Lab值,修改值,然后将数组转换回图像并保存图像。我有以下代码:

  import Image,color,numpy 

#打开图像文件
src = Image.open(face-him.jpg)

#尝试确保图像是RGB
src = src.convert(mode =RGB)

#使用numpy创建图像数组
srcArray = numpy.asarray(src)

#将RGB数组转换为Lab
srcArray = color.rgb2lab srcArray)

#修改此处的数组

#将数组转换回Lab
end = color.lab2rgb(srcArray)

#Create image from array
final = Image.fromarray(end,RGB)

#保存
final.save(out.jpg)

此代码依赖于PIL,NumPy和颜色。颜色可以在SciPy树干中找到

测试所需的所有源文件 - colour-test.zip

解决方案

没有尝试过,缩放错误在转换颜色时很常见:

RGB是字节0 .. 255,例如黄色[255,255,0],
,而 rgb2xyz()等工作在三个浮点数,黄色[1.,1.,0]
color.py 没有范围检查: lab2rgb(rgb2lab([255,255,0]))



在IPython中,%run main.py ,然后打印srcArray的两个角, / p>

添加13July:对于记录/ google,这里是NumPy成语,打包,解压缩和转换RGB图像数组:

 #unpack image array,10 x 5 x 3  - > rgb  -  
img = np.arange(10 * 5 * 3).reshape((10,5,3))
printimg.shape:,img.shape
r, g,b = img.transpose(2,0,1)#3 10 5
printr.shape:,r.shape

#pack 10 x 5 rgb - 10 x 5 x 3再次
rgb = np.array((r,g,b))。transpose(1,2,0)#10 5 3再次
printrgb.shape: ,rgb.shape
assert(rgb == img).all()

#rgb 0 .. 255& float 0 .. 1 -
imgfloat = img.astype(np.float32)/ 255.
img8 =(imgfloat * 255).round()。astype(np.uint8)
assert(img == img8).all()


I am trying to convert a PIL image into an array using NumPy. I then want to convert that array into Lab values, modify the values and then convert the array back in to an image and save the image. I have the following code:

import Image, color, numpy

# Open the image file
src = Image.open("face-him.jpg")

# Attempt to ensure image is RGB
src = src.convert(mode="RGB")

# Create array of image using numpy
srcArray = numpy.asarray(src)

# Convert array from RGB into Lab
srcArray = color.rgb2lab(srcArray)

# Modify array here

# Convert array back into Lab
end = color.lab2rgb(srcArray)

# Create image from array
final = Image.fromarray(end, "RGB")

# Save
final.save("out.jpg")

This code is dependent on PIL, NumPy and color. color can be found in the SciPy trunk here. I downloaded the color.py file along with certain colordata .txt files. I modified the color.py so that it can run independently from the SciPy source and it all seems to work fine - values in the array are changed when I run conversions.

My problem is that when I run the above code which simply converts an image to Lab, then back to RGB and saves it I get the following image back:

What is going wrong? Is it the fact I am using the functions from color.py?

For reference:
Source Image - face-him.jpg
All source files required to test - colour-test.zip

解决方案

Without having tried it, scaling errors are common in converting colors:
RGB is bytes 0 .. 255, e.g. yellow [255,255,0], whereas rgb2xyz() etc. work on triples of floats, yellow [1.,1.,0].
(color.py has no range checks: lab2rgb( rgb2lab([255,255,0]) ) is junk.)

In IPython, %run main.py, then print corners of srcArray and end ?

Added 13July: for the record / for google, here are NumPy idioms to pack, unpack and convert RGB image arrays:

    # unpack image array, 10 x 5 x 3 -> r g b --
img = np.arange( 10*5*3 ).reshape(( 10,5,3 ))
print "img.shape:", img.shape
r,g,b = img.transpose( 2,0,1 )  # 3 10 5
print "r.shape:", r.shape

    # pack 10 x 5 r g b -> 10 x 5 x 3 again --
rgb = np.array(( r, g, b )).transpose( 1,2,0 )  # 10 5 3 again
print "rgb.shape:", rgb.shape
assert (rgb == img).all()

    # rgb 0 .. 255 <-> float 0 .. 1 --
imgfloat = img.astype(np.float32) / 255.
img8 = (imgfloat * 255).round().astype(np.uint8)  
assert (img == img8).all()

这篇关于使用PIL和NumPy将图像转换为Lab数组,修改值,然后转换回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆