ValueError: image has wrong mode

>>> im = im.convert(‘RGB’, rgb2yiq)

Traceback (most recent call last):

File “<stdin>”, line 1, in <module>

File “/usr/lib/python2.7/dist-packages/PIL/Image.py“, line 685, in convert

im = self.im.convert_matrix(mode, data)

ValueError: image has wrong mode

Now, when can you actually compare the images by using the function ImageChops.difference ?

First, both images have to have pixels that can be stored in an unsigned byte.

This is a very common type of image, but this excludes comparison between images even if they are the same mode. So, you cannot compare an image x and y when one or /both/ of them have a mode of: FII;16I;16LI;16BBGR;15,BGR;16BGR;24, or BGR;32. Just to make it clear: it doesn’t matter if both images are in the same mode if they happen to be in one of the modes above, the function will refuse to work.

So, the comparison can be done when the images are in the modes 1PLLARGBRGBARGBX,RGBaCMYK, or YCbCr as long as they have the same number of bands. This means the images don’t have to have the same mode to be compared. For instance, difference(x.convert('CMYK'), x.convert('RGBA')) or difference(x.convert('1'), x.convert('P')) work just fine. Of course this means difference(x.convert('LA'), x.convert('L')), fails. Finally, the resulting image will always have the mode equal to the first image passed to the function.

This is valid at least for the PIL 1.1.7.

16-bit BGR is listed as "BGR;16" and creates a "RGBX" output.

As it says in the PIL documentation you need to look at the file Unpack.c, you’ll find the list near the end of the file.

image_file = image_file.convert('1') # convert image to black and white

>>> import Image
>>> path=’./upload/sketch/20130129_191637.jpg’
>>> im = Image.open(path)

>>> im.mode
‘RGBA’


>>> im2=Image.open(‘./portrait/T01893_10.jpg’)
>>> im2.mode
‘RGB’

Convert RGBA color to RGB

  •  RGBA stands for R ed G reen B lue A lpha, incorporates the alpha value into the actual colour result based on an opaque background colour (or ‘matte’ as it’s referred to).

Assuming that 0 is opaque and 255 is transparent, then 255,255,255,127 RGBA with a black matte will result in 127,127,127 RGB value.

PNG is an image format that uses RGBA.

def reduce_opacity(im, opacity):
    """Returns an image with reduced opacity."""
    assert opacity >= 0 and opacity <= 1
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    else:
        im = im.copy()
    alpha = im.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    im.putalpha(alpha)
    return im
  • RGB模式下,每个图片由三个通道叠加而成,每个模式下为一个灰度图,当有一个调色板来调色的时候,这三张灰度图的叠加即可合成3*8位(每个像素)的一个真彩图片。

 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: