8

I'm trying to run this code:

import pyautogui
import time
from PIL import _imaging
from PIL import Image
import pytesseract

time.sleep(5)
captura = pyautogui.screenshot()
codigo = captura.crop((872, 292, 983, 337))
codigo.save(r'C:\autobot_wwe_supercard\imagenes\codigo.png')
time.sleep(2)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program     
Files\Tesseract-OCR\tesseract'
print(pytesseract.image_to_string(r'D:\codigo.png'))

And this error pops up: ImportError: cannot import name 'imaging' from 'PIL' (C:\Users\Usuario\AppData\Roaming\Python\Python38\site-packages\PIL_init.py)

I installed pillow in console with pip install pillow

I installed pytesseract in console with pip install pytesseract

Andresnex
  • 87
  • 1
  • 1
  • 5
  • 1
    Why are you doing `from PIL import _imaging`? If such an object even exists, it's an internal detail of PIL that you shouldn't need to use. For that matter, why are you importing PIL at all? You aren't directly using it (and any indirect use by the other modules you import does not require *you* to import it). – jasonharper Nov 25 '20 at 03:55
  • the thing is that when I import pytesseract it gives me two errors, but when I import them it only gives me one and is that. – Andresnex Nov 25 '20 at 03:59
  • 1
    You should show *those* errors, then. – Karl Knechtel Nov 25 '20 at 04:10
  • my bad, I'm new at this so I don't really get how it works. Do I edit this post with whats happening now or do I create a new one? – Andresnex Nov 25 '20 at 04:20
  • Does this answer your question? [Why can't Python import Image from PIL?](https://stackoverflow.com/questions/26505958/why-cant-python-import-image-from-pil) – questionto42 Jun 10 '22 at 20:35

3 Answers3

11

It appears as if a lot of PIL ImportErrors can simply be fixed by uninstalling and reinstalling Pillow again according to this source and your specific problem can be found here.

Try these three commands:

pip uninstall PIL
pip uninstall Pillow
pip install Pillow
Samrat Sahoo
  • 565
  • 8
  • 17
  • thanks, that error disapeared but now i got the "OSError: [WinError 193] %1 is not a valid Win32 application" when it imports pytesseract. – Andresnex Nov 25 '20 at 04:00
  • Does [this](https://stackoverflow.com/questions/62337501/oserror-winerror-193-1-is-not-a-valid-win32-application-nltk) help? – Karl Knechtel Nov 25 '20 at 04:12
  • i think i have to change the python program to 32bit, but I cant find where to download a recent version in 32bit – Andresnex Nov 25 '20 at 05:59
  • 2
    `python3.9 -m pip install --user --upgrade Pillow` worked for me. – Patol75 Aug 13 '21 at 03:27
3

I needed Pillow in PyCharm with Python3.9. Pillow was installed in Python3.8. Perhaps as user. PyCharm could find it with Py3.8, but not with Py3.9

This solved it for me:

sudo python3.9 -m pip install Pillow --upgrade

Upgrade it as sudo with that python version, which the script shall run with. Maybe, sudo is not needed, if you want to run it only in a virtual environment or as actual user.

rundekugel
  • 1,118
  • 1
  • 10
  • 23
0

PIL has some binary dependencies which are tied to the version of Python they were built against.

In my case, the solution was to edit the runtime settings of my Amazon Lambda function and change it to an older version of Python which matched my dev environment.

The same error will occur if pillow was installed for an incompatible CPU architecture.

copycat
  • 83
  • 1
  • 5