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')
Answer from mmgp on Stack OverflowThe 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')
You should be able to simply let PIL get the filetype from extension, i.e. use:
j.save("C:/Users/User/Desktop/mesh_trans.bmp")
Using PIL what is the easiest way to save a jpg File to a folder?
python - Saving Image with PIL - Stack Overflow
Saving PNG images with PIL is 4 times slower than saving them with OpenCV
PIL.Image.save ignores parameters
Videos
I want to save jpg files into a specific directory. I know it should be easy to Google but I somehow can't find what I need. So it would be nice if someone had a small example at hand.
PIL isn't an attribute of newImg1 but newImg1 is an instance of PIL.Image so it has a save method, thus the following should work.
newImg1.save("img1.png","PNG")
Note that just calling a file .png doesn't make it one so you need to specify the file format as a second parameter.
try:
type(newImg1)
dir(newImg1)
and
help(newImg1.save)
As I hate to see questions without a complete answer:
from PIL import Image
newImg1 = Image.new('RGB', (512,512))
for i in range (0,511):
for j in range (0,511):
newImg1.putpixel((i,j),(i+j%256,i,j))
newImg1.save("img1.png")
which yields a test pattern.
To use array style addressing on the image instead of putpixel, convert to a numpy array:
import numpy as np
pixels = np.asarray(newImg1)
pixels.shape, pixels.dtype
-> (512, 512, 3), dtype('uint8')
For Python 3.x use BytesIO instead of StringIO:
temp = BytesIO()
im.save(temp, format="png")
ftp.storbinary('STOR Obama.jpg', temp.getvalue())
Do not pass a string to storbinary. You should pass a file or file object (memory-mapped file) to it instead. Also, this line should be temp = StringIO.StringIO(). So:
temp = StringIO.StringIO() # this is a file object
im.save(temp, format="png") # save the content to temp
ftp.storbinary('STOR Obama.jpg', temp) # upload temp