NumPy,PIL添加图像 [英] NumPy, PIL adding an image

查看:143
本文介绍了NumPy,PIL添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NumPy和PIL添加两个图像。我在 MATLAB 中执行此操作的方式如下:

I'm trying to add two images together using NumPy and PIL. The way I would do this in MATLAB would be something like:

>> M1 = imread('_1.jpg');
>> M2 = imread('_2.jpg');
>> resM = M1 + M2;
>> imwrite(resM, 'res.jpg');

我得到这样的结果:

alt text http://www.deadlink.cc/matlab.jpg

使用合成程序并添加图像,MATLAB结果似乎是正确的。

Using a compositing program and adding the images the MATLAB result seems to be right.

在Python中我试图做同样的事情这样的事情:

In Python I'm trying to do the same thing like this:

from PIL import Image
from numpy import *

im1 = Image.open('/Users/rem7/Desktop/_1.jpg')
im2 = Image.open('/Users/rem7/Desktop/_2.jpg')

im1arr = asarray(im1)
im2arr = asarray(im2)

addition = im1arr + im2arr

resultImage = Image.fromarray(addition)
resultImage.save('/Users/rem7/Desktop/a.jpg')

我得到这样的结果:

alt text http://www.deadlink。 cc / python.jpg

为什么我会得到所有这些乐趣你的颜色?我也尝试使用 ImageMath.eval(a + b,a = im1,b = im2),但是我收到有关RGB不支持的错误。

Why am I getting all those funky colors? I also tried using ImageMath.eval("a+b", a=im1, b=im2), but I get an error about RGB unsupported.

我还看到有一个 Image.blend()但这需要一个alpha。

I also saw that there is an Image.blend() but that requires an alpha.

达到我想要的最佳方式是什么?

What's the best way to achieve what I'm looking for?

源图像(图像已被删除):

Source Images (images have been removed):

alt text http://www.deadlink.cc/_1.jpg
alt text http://www.deadlink.cc/_2.jpg

嗯,好吧,我使用添加图片图标添加了源图像,当我编辑帖子时它们会出现,但由于某种原因,图像不会t出现在帖子里。

Humm, OK, well I added the source images using the add image icon and they show up when I'm editing the post, but for some reason the images don't show up in the post.

(图片已被删除)2013 05 09

(images have been removed) 2013 05 09

推荐答案

As大家已经建议,你观察到的怪异颜色是溢出的。正如您在对schnaader的回答评论中指出的那样如果你添加这样的图像,仍然会溢出

As everyone suggested already, the weird colors you're observing are overflow. And as you point out in the comment of schnaader's answer you still get overflow if you add your images like this:

addition=(im1arr+im2arr)/2

这种溢出的原因是你的NumPy数组( im1arr im2arr )属于 uint8 类型(即8位)。这意味着数组的每个元素只能保存最多255个值,所以当你的总和超过255时,它会回到0左右:

The reason for this overflow is that your NumPy arrays (im1arr im2arr) are of the uint8 type (i.e. 8-bit). This means each element of the array can only hold values up to 255, so when your sum exceeds 255, it loops back around 0:

>>>array([255,10,100],dtype='uint8') +  array([1,10,160],dtype='uint8')
array([ 0, 20,  4], dtype=uint8)

为避免溢出,您的数组应该能够包含超过255的值。需要将它们转换为浮点数,例如,执行混合操作并将结果转换回uint8

To avoid overflow, your arrays should be able to contain values beyond 255. You need to convert them to floats for instance, perform the blending operation and convert the result back to uint8:

im1arrF = im1arr.astype('float')
im2arrF = im2arr.astype('float')
additionF = (im1arrF+im2arrF)/2
addition = additionF.astype('uint8')

不应该做这个:

addition = im1arr/2 + im2arr/2

因为你丢失信息,通过压缩图像的动态(你有效地使图像7位)是在你执行混合信息之前。

as you lose information, by squashing the dynamic of the image (you effectively make the images 7-bit) before you perform the blending information.

MATLAB注释:你在MATLAB中没有看到这个问题的原因,可能是因为MATLAB需要注意其中一个函数中隐含的溢出。

MATLAB note: the reason you don't see this problem in MATLAB, is probably because MATLAB takes care of the overflow implicitly in one of its functions.

这篇关于NumPy,PIL添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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