PIL cannot write mode F to jpeg

前端 未结 3 392
清酒与你
清酒与你 2021-01-03 17:28

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\" 


        
相关标签:
3条回答
  • 2021-01-03 17:59

    Try convert the image to RGB:

    ...
    new_p = Image.fromarray(fft_p)
    if new_p.mode != 'RGB':
        new_p = new_p.convert('RGB')
    ...
    
    0 讨论(0)
  • 2021-01-03 18:06

    I think it may be that your fft_p array is in float type and the image should have every pixel in the format 0-255 (which is uint8), so maybe you can try doing this before creating the image from array:

    fft_p = fft_p.astype(np.uint8)
    new_p = Image.fromarray(fft_p)
    

    But be aware that every element in the fft_p array should be in the 0-255 range, so maybe you would need to do some processing to that before to get the desired results, for example if you every element is a float between 0 and 1 you can multiply them by 255.

    0 讨论(0)
  • 2021-01-03 18:08

    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.

    0 讨论(0)
提交回复
热议问题