wagtail / Willow

A wrapper that combines the functionality of multiple Python image libraries into one API
https://willow.wagtail.org/
BSD 3-Clause "New" or "Revised" License
273 stars 53 forks source link

Always getting AttributeError: 'JPEGImageFile' object has no attribute <bar> #109

Closed tombreit closed 1 year ago

tombreit commented 1 year ago

Dear Willow-Team,

I am not able to use any attribute (eg. get_size() or resize) on a Willow Image instance. It seems these attributes are not available on the Image subclass (here: JPEGImageFile). I tried the code samples from the README but I must have taken a wrong turn somewhere and can't find my mistake ;-)

I'm using Willow 1.4.1

from willow.image import Image

f = open("/tmp/foo.jpg", "rb")
img = Image.open(f)
img
<willow.image.JPEGImageFile object at 0x7fc7445d2c10>

img.get_size()
AttributeError: 'JPEGImageFile' object has no attribute 'resize'
Full traceback ```python3 Python 3.11.2 (main, Feb 12 2023, 00:48:52) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from willow.image import Image >>> f = open("/tmp/foo.jpg", "rb") >>> f <_io.BufferedReader name='/tmp/foo.jpg'> >>> img = Image.open(f) >>> img >>> img.get_size() Traceback (most recent call last): File "/projects/bar/.venv/lib/python3.11/site-packages/willow/registry.py", line 306, in find_operation func = self.get_operation(from_class, operation_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/projects/bar/.venv/lib/python3.11/site-packages/willow/registry.py", line 79, in get_operation return self._registered_operations[image_class][operation_name] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^ KeyError: 'get_size' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/projects/bar/.venv/lib/python3.11/site-packages/willow/image.py", line 67, in __getattr__ operation, _, conversion_path, _ = registry.find_operation(type(self), attr) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/projects/bar/.venv/lib/python3.11/site-packages/willow/registry.py", line 323, in find_operation raise UnroutableOperationError( willow.registry.UnroutableOperationError: The operation 'get_size' is available in the image class 'RGBAImageBuffer, RGBImageBuffer' but it can't be converted to from 'JPEGImageFile' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/projects/bar/.venv/lib/python3.11/site-packages/willow/image.py", line 70, in __getattr__ raise AttributeError("%r object has no attribute %r" % ( AttributeError: 'JPEGImageFile' object has no attribute 'get_size' >>> img = img.resize((100, 100)) Traceback (most recent call last): File "/projects/bar/.venv/lib/python3.11/site-packages/willow/registry.py", line 306, in find_operation func = self.get_operation(from_class, operation_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/projects/bar/.venv/lib/python3.11/site-packages/willow/registry.py", line 79, in get_operation return self._registered_operations[image_class][operation_name] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^ KeyError: 'resize' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/projects/bar/.venv/lib/python3.11/site-packages/willow/image.py", line 67, in __getattr__ operation, _, conversion_path, _ = registry.find_operation(type(self), attr) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/projects/bar/.venv/lib/python3.11/site-packages/willow/registry.py", line 313, in find_operation image_classes = self.get_image_classes( ^^^^^^^^^^^^^^^^^^^^^^^ File "/projects/bar/.venv/lib/python3.11/site-packages/willow/registry.py", line 109, in get_image_classes raise UnavailableOperationError('\n'.join([ willow.registry.UnavailableOperationError: The operation 'resize' is available in the following image classes but they all raised errors: PillowImage: No module named 'PIL' WandImage: No module named 'wand' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/projects/bar/.venv/lib/python3.11/site-packages/willow/image.py", line 70, in __getattr__ raise AttributeError("%r object has no attribute %r" % ( AttributeError: 'JPEGImageFile' object has no attribute 'resize' ```
tombreit commented 1 year ago

Sorry, forgot to install Pillow, Wand and did not read the whole traceback...