Tried to find the detailed implementation of ImageFilter, but I can't figure out where filterargs are used? That is, I can't figure out the implementation detail for image filter. I just saw the filter function in Image.py and ImageFilter in ImageFilter.py , but still could't find the detailed implementation. Could you help me?
Tried to find the detailed implementation of ImageFilter.
Is the ImageFilter implemented in libImaging/Filter.c ?
Starting from code -
from PIL import Image, ImageFilter
im = Image.new("RGB", (100, 100), "#f00")
im.filter(ImageFilter.BLUR)
im.filter calls -
https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/Image.py#L1185
Which then calls this, or a similar line -
Since filter is ImageFilter.BLUR that was passed in, the filter method of ImageFilter.BLUR is called. Note that self.im is not a PIL.Image.Image instance, but an ImagingCore object.
ImageFilter.BLUR inherits ImageFilter.BuiltinFilter - https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/ImageFilter.py#L212
image being an ImagingCore object, this moves into the C code.
This parses the filterargs that were passed in -
and calls -
leading to
So yes, src/libImaging/Filter.c
Thanks
Most helpful comment
Starting from code -
im.filtercalls -https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/Image.py#L1185
Which then calls this, or a similar line -
https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/Image.py#L1205
Since
filterisImageFilter.BLURthat was passed in, thefiltermethod ofImageFilter.BLURis called. Note thatself.imis not aPIL.Image.Imageinstance, but anImagingCoreobject.ImageFilter.BLURinheritsImageFilter.BuiltinFilter- https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/ImageFilter.py#L212https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/ImageFilter.py#L36-L40
imagebeing anImagingCoreobject, this moves into the C code.https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/_imaging.c#L3194
https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/_imaging.c#L1004-L1005
This parses the filterargs that were passed in -
https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/_imaging.c#L1014-L1015
and calls -
https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/_imaging.c#L1032
leading to
https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/libImaging/Filter.c#L316-L317
So yes, src/libImaging/Filter.c