I'm trying to blur an image using PIL:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
When I do im.show() and save it to my hard drive, it saves as a BMP file, which is incompatible with the place where I'm trying to upload it. How do I change the file format from BMP to something else that is compatible?
Just use the save() function directly:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
im.save("saved.jpg")
This function supports many formats, as explained in the documentation.
Related
I am using tinytags module in python to get the cover art of a mp3 file and want to display or store it. The return type of the variable is showing to be bytes. I have tried fumbling around with PIL using frombytes but to no avail. Is there any method to convert the bytes to image?
from tinytag import TinyTag
tag = TinyTag.get("03. Me, Myself & I.mp3", image=True)
img = tag.get_image()
I actually got a PNG image when I called tag.get_image() but I guess you might get a JPEG. Either way, you can wrap it in a BytesIO and open it with PIL/Pillow or display it. Carrying on from your code:
from PIL import Image
import io
...
im = tag.get_image()
# Make a PIL Image
pi = Image.open(io.BytesIO(im))
# Save as PNG, or JPEG
pi.save('cover.png')
# Display
pi.show()
Note that you don't have to use PIL/Pillow. You could look at the first few bytes and if they are a PNG signature (\x89PNG) save data as binary with PNG extension. If the signature is JPEG (\xff \xd8) save data as binary with JPEG extension.
I currently have an image in the wand.image format. I need to convert it to PIL format so that I can continue working with it. It is and RGB image. I cant seem to find anywhere how to do it. I have tried to convert it to an np array and then read the np array into the PIL image. Thank you for any help. Here is my code if it helps:
from PIL import Image, ImageFilter
from wand.image import Image as Image2
with Image2(filename=join(img_path,file)) as img:
img.virtual_pixel = 'transparent'
test_image = Image.fromarray(np.array(img), 'RGB')
data_image = Image.open(io.BytesIO(img.make_blob("png"))).convert('RGB') fixes the issue for some reason
So I was working with skimage for some image preprocessing (i'm very new to it). I have a PIL Image object and wanted to convert to a skimage image with skimage.io.imread(). I know that I can just save the image and then run imread on that file, but I was wondering if there was a way I could read the PIL Image object from the code itself. I tried to run imread on the PIL Image object itself but I end up getting errors.
OSError: Cannot understand given URI: <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=192....
Anyone know how I can solve this in skimage.
Scikit-Image stores images as Numpy arrays, so you just need to make a Numpy array from your PIL Image:
ImageForScikit = np.array(YourPILImage)
You may review imageio documentation related to the function imread for including in your code the attribute format and code as follows just in case the image format is PNG: imageio.imread(filename, format = 'PNG')
After loading an image file with PIL.Image, how can I determine whether the image file is a PNG/JPG/BMP/GIF? I understand very little about these file formats, can PIL get the format metadata from the file header? Or does it need to 'analyze' the data within the file?
If PIL doesn't provide such an API, is there any python library that does?
Try:
from PIL import Image
img = Image.open(filename)
print(img.format) # 'JPEG'
More info
https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.format
https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html
i use mongodb and save file to gridfs
now i want edit images from gridfs ...
i use this code
def thumbnail(file_obj):
import StringIO
from PIL import Image
im = StringIO.StringIO()
im.write(file_obj.raw_file)
im_ful = Image.open(im)
return im_ful.info
but pil said "cannot identify image file"
thats image also ;)
how can fix it
You need an im.seek(0) before the Image.open(im) call. Otherwise PIL tries to read from the end of the file, gets no data, and fails.