Pillow: 4 channel JPEG in ImageNet LSVRC 2012 validation data set?

Created on 1 Jun 2017  路  2Comments  路  Source: python-pillow/Pillow

EDIT: Apparently there is such a thing as CMYK JPEG files which have 4 channels.

What did you do?

from PIL import Image
print("This image has %d channels."%Image.open("image.jpg").layers)

image.jpg:

image

What did you expect to happen?

This image has 3 channels.

What actually happened?

This image has 4 channels.

What versions of Pillow and Python are you using?

Pillow 4.1.1, Python 2.7.6
and
Pillow 3.1.1, Python 3.5.2

Miscellaneous information:

This bug was first found in scipy.misc.imread which uses pillow:

import scipy.misc
print("Image shape is:", scipy.misc.imread("image.jpg").shape)

Image shape is: (461, 614, 4)

The image is from the ImageNet LSVRC 2012 validation data set where it is named ILSVRC2012_val_00019877.JPEG and can be downloaded from http://academictorrents.com/details/5d6d0df7ed81efd49ca99ea4737e0ae5e3a5f2e5

It is a 3 channel EDIT: 4 channel CMYK JPEG file with 32 bits per channel and can be opened correctly in Windows Photo Viewer, Firefox, Chrome and GIMP.

The rdjpgcom tool seems to have the same bug. This information might help to track down where the bug is in Pillow.

rdjpgcom -verbose image.jpg

Output:

JPEG image is 614w * 461h, 4 color components, 8 bits per sample
JPEG process: Baseline

Most helpful comment

Oh, sorry, I didn't know JPEGs could do that. Never mind then!

Here is how to load the image as RGB if someone should find this with google:

from PIL import Image
# load image with Pillow as RGB
image = Image.open("image.jpg").convert("RGB")


# convert to numpy
import numpy as np
image = np.asarray(image)


# display image
import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()


# or load image with scipy as RGB
import scipy.misc

# mode="RGB" throws "conversion from CMYK to RBG not supported"
image = scipy.misc.imread("image.jpg", mode="RGBA")[:, :, :3]


# display image
plt.imshow(image)
plt.show()

All 2 comments

Looks like it's CMYK image.

Oh, sorry, I didn't know JPEGs could do that. Never mind then!

Here is how to load the image as RGB if someone should find this with google:

from PIL import Image
# load image with Pillow as RGB
image = Image.open("image.jpg").convert("RGB")


# convert to numpy
import numpy as np
image = np.asarray(image)


# display image
import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()


# or load image with scipy as RGB
import scipy.misc

# mode="RGB" throws "conversion from CMYK to RBG not supported"
image = scipy.misc.imread("image.jpg", mode="RGBA")[:, :, :3]


# display image
plt.imshow(image)
plt.show()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Larivact picture Larivact  路  4Comments

thinrhino picture thinrhino  路  3Comments

naaaargle picture naaaargle  路  3Comments

anonymous530 picture anonymous530  路  3Comments

mmalenta picture mmalenta  路  3Comments