Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL cannot write mode F to jpeg

I am taking a jpg image and using numpy's fft2 to create/save a new image. However it throws this error

"IOError: cannot write mode F as JPEG"  

Is there an issue with CMYK and JPEG files in PIL???

p = Image.open('kibera.jpg') bw_p = p.convert('L') array_p = numpy.asarray(bw_p) fft_p = abs(numpy.fft.rfft2(array_p)) new_p = Image.fromarray(fft_p) new_p.save('kibera0.jpg') new_p.histogram() 
like image 518
JHHP Avatar asked May 23 '13 17:05

JHHP


People also ask

What is Mode P in PIL?

If you have a P mode image, that means it is palettised. That means there is a palette with up to 256 different colours in it, and instead of storing 3 bytes for R, G and B for each pixel, you store 1 byte which is the index into the palette.


2 Answers

Semente's answer is right for color images For grayscale images you can use below:-

new_p = Image.fromarray(fft_p) new_p = new_p.convert("L") 

If you use new_p = new_p.convert('RGB') for a grayscale image then the image will still have 24 bit depth instead of 8 bit and would occupy thrice the size on hard disk and it wont be a true grayscale image.

like image 23
Himanshu Punetha Avatar answered Sep 18 '22 19:09

Himanshu Punetha


Try convert the image to RGB:

... new_p = Image.fromarray(fft_p) if new_p.mode != 'RGB':     new_p = new_p.convert('RGB') ... 
like image 89
semente Avatar answered Sep 17 '22 19:09

semente