SMI / dicompixelanon

DICOM Pixel Anonymisation
3 stars 0 forks source link

AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS' (Python 3.9.1) #25

Closed darshad-github closed 1 year ago

darshad-github commented 1 year ago

STACK TRACE

./dicom_ocr.py --ocr easyocr --pii flair --rects ../../../SmiServices/tests/common/Smi.Common.Tests/TestData/burned-in-text-test.dcm 
ERROR:root:flair requested but no data directory found
WARNING:root:Cannot run NLP on the OCR output because flair is not installed
Traceback (most recent call last):
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/dicompixelanon/src/applications/./dicom_ocr.py", line 313, in <module>
    process_dicom(file, ocr_engine = ocr_engine, nlp_engine = nlp_engine,
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/dicompixelanon/src/applications/./dicom_ocr.py", line 223, in process_dicom
    process_image(np.asarray(img), filename=filename, frame=frame, overlay=overlay, **meta)
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/dicompixelanon/src/applications/./dicom_ocr.py", line 123, in process_image
    ocr_data = ocr_engine.image_to_data(img)
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/venv/lib/python3.9/site-packages/DicomPixelAnon-1.0.13-py3.9.egg/DicomPixelAnon/ocrengine.py", line 178, in image_to_data
    res = self.easyreader.readtext(img, paragraph=True, rotation_info=rotates)
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/venv/lib/python3.9/site-packages/easyocr/easyocr.py", line 464, in readtext
    result = self.recognize(img_cv_grey, horizontal_list, free_list,\
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/venv/lib/python3.9/site-packages/easyocr/easyocr.py", line 398, in recognize
    image_list, max_width = get_image_list(horizontal_list, free_list, img_cv_grey, model_height = imgH)
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/venv/lib/python3.9/site-packages/easyocr/utils.py", line 613, in get_image_list
    crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height)
  File "/Users/daniyalarshad/Desktop/PythonProject/tmp/venv/lib/python3.9/site-packages/easyocr/utils.py", line 576, in compute_ratio_and_resize
    img = cv2.resize(img,(int(model_height*ratio),model_height),interpolation=Image.ANTIALIAS)
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
darshad-github commented 1 year ago

SOLUTION

Upon investigation, we learned that ANTIALIAS was removed in Pillow 10.0.0 (after being deprecated through many previous versions). Now you need to use PIL.Image.LANCZOS or PIL.Image.Resampling.LANCZOS. However, a workaround for that is to use pip install Pillow==9.5.0 instead. (StackOverflow)

(venv) dicompixelanon/src/applications % mkdir $SMI_ROOT/data/flair
(venv) dicompixelanon/src/applications % pip install Pillow==9.5.0
howff commented 1 year ago

easyocr will be updated in due course https://github.com/JaidedAI/EasyOCR/issues/1077

However this is a fault in Pillow - their release notes said "The ANTIALIAS constant is left for backward compatibility and is an alias for LANCZOS" but then later they say "Backwards Incompatible Changes: Image.ANTIALIAS has been removed". So they are aware of the need for backwards compatibility, yet they go ahead and deliberately break everyone else's code for no reason!