Pillow: Tried to find the detailed implementation of ImageFilter

Created on 6 Jun 2019  路  3Comments  路  Source: python-pillow/Pillow

What did you do?

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?

What did you expect to happen?

Tried to find the detailed implementation of ImageFilter.

Question

Most helpful comment

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 -

https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/Image.py#L1205

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

https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/ImageFilter.py#L36-L40

image being an ImagingCore object, 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

All 3 comments

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 -

https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/Image.py#L1205

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

https://github.com/python-pillow/Pillow/blob/169961649d1d946c95155d4f046b8cbcdff49e61/src/PIL/ImageFilter.py#L36-L40

image being an ImagingCore object, 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

Thanks

Was this page helpful?
0 / 5 - 0 ratings