I have a user with a photo. I want to check if the photo is animated. I ran the code:
```python
from PIL import Image
if (Image.open(user.photo.file).is_animated):
````
I expect is_animated to be defined for all images.
https://stackoverflow.com/a/50808096/1412564
https://github.com/python-pillow/Pillow/pull/1319
If the image if not a GIF, I got an exception:
'JpegImageFile' object has no attribute 'is_animated'
With the current version of Pillow, you could run
if (getattr(Image.open(user.photo.file), "is_animated", False):
I should also point out that opening an image like that won't close the file resource - https://pillow.readthedocs.io/en/stable/deprecations.html#image-del
Hi,
I would like "is_animated" to be defined also for images of non-animated formats (such as PNG, JPEG). Just define it to False.
So it's not recommended to use if (Image.open(user.photo.file).is_animated) because the image is not closed? What is the recommended method to use which the image will also be closed?
from PIL import Image
with Image.open(user.photo.file) as im:
im_is_animated = im.is_animated
if im_is_animated:
pass
or you could use .close()
from PIL import Image
im = Image.open(user.photo.file)
im_is_animated = im.is_animated
im.close()
if im_is_animated:
pass
Thank you.
Now, I would like the following code to work for any image:
with Image.open(user.photo.file) as image:
if (image.is_animated):
I'm kind of reluctant about this.
If we implemented is_animated for all images, then why not n_frames? Why not tell() and seek()? Doesn't this have the potential to confuse a user into thinking that JPEG images, for example, can be animated?
I think is_animated should just return true or false whether the image is animated or not. It doesn't have to do with the file format. I don't think it will confuse users - if they check is_animated, and get false, then the image is not animated.
Why do I have to check if (getattr(image, "is_animated", False)): if I can just check if (image.is_animated): and it works?
We'd need to implement is_animated for each of the file formats:
Are there any in there which _can_ be animated but don't currently have is_animated?
For example, (getattr(image, "is_animated", False) could be returning False for an animated format when we just haven't implemented support. There's some old and obscure ones in there.
I've realised that seek() and tell() are already defined on the base Image class. I've created PR #4628 to add in is_animated and n_frames as well.
Regarding the different image formats, that's an interesting point. For example, for FitsStubImagePlugin, the wiki page for FITS says that
normal image data are usually 2-D or 3-D, with the third dimension representing for example time
So it could have animated images.
Thank you.
Discussion in that PR raised a point that by adding a universal is_animated, it would mean that for images without Pillow animation support, we could be claiming that an image is not animated, when it is, just without Pillow support yet.
Instead, #4739 has documented the current situation.