How can I save an image with PIL?

前端 未结 4 674
青春惊慌失措
青春惊慌失措 2020-11-28 04:09

I have just done some image processing using the Python image library (PIL) using a post I found earlier to perform fourier transforms of images and I can\'t get the save fu

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 04:30

    The error regarding the file extension has been handled, you either use BMP (without the dot) or pass the output name with the extension already. Now to handle the error you need to properly modify your data in the frequency domain to be saved as an integer image, PIL is telling you that it doesn't accept float data to save as BMP.

    Here is a suggestion (with other minor modifications, like using fftshift and numpy.array instead of numpy.asarray) for doing the conversion for proper visualization:

    import sys
    import numpy
    from PIL import Image
    
    img = Image.open(sys.argv[1]).convert('L')
    
    im = numpy.array(img)
    fft_mag = numpy.abs(numpy.fft.fftshift(numpy.fft.fft2(im)))
    
    visual = numpy.log(fft_mag)
    visual = (visual - visual.min()) / (visual.max() - visual.min())
    
    result = Image.fromarray((visual * 255).astype(numpy.uint8))
    result.save('out.bmp')
    

提交回复
热议问题