How to import Image in Python3.5 under windows from PIL/Pillow - python

I want to write a little app to convert some images.
I think to have to use Image.open(), so I have to import the Image module. Right?
If so, here my problem. I have read other questions like this but any of them worked for me. I tried:
import Image => ImportError: cannot import name 'VERSION'
from PIL import Image => ImportError: cannot import name 'VERSION'
from Pillow import Image => ImportError: No module named 'Pillow'
In documentation I read:
Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL.
Pillow >= 1.0 no longer supports “import Image”. Please use “from PIL import Image” instead.
In my virtual enviroment I had PIL and Pillow directory but pip list gave only Pillow (3.1.0) so, even reading other answers, I try to uninstall PIL using pip but it can't find PIL, so I just delete the directory PIL and installed Pillow-PIL (now it appears on pip list and there's the directory Pillow_PIL on my venv\Lib\site-packages\) now:
import Image => ImportError: No module named 'PIL'
from PIL import Image => ImportError: No module named 'PIL'
from Pillow import Image => ImportError: No module named 'Pillow'
from Pillow-PIL import Image => SyntaxError: invalid syntax (on the minus sign)
from Pillow_PIL import Image => ImportError: No module named 'Pillow_PIL'
apt-get install python-imaging => "apt-get" command unknow (my free translation)
So, what now?
Edit: full error is
Traceback (most recent call last):
File "prova.py", line 1, in <module>
import Image
File "D:\Python\Envs\possedimenti\lib\site-packages\Image.py", line 1, in <mod
ule>
from PIL.Image import *
File "D:\Python\Envs\possedimenti\lib\site-packages\PIL\Image.py", line 29, in
<module>
from PIL import VERSION, PILLOW_VERSION, _plugins
ImportError: cannot import name 'VERSION'

When you install Pillow, it installs a module that you import as PIL. Pillow-PIL is apparantly a "Pillow wrapper for PIL compatibility".
It is generally not a good idea to delete files from site-packages, but to use a package installer like pip to do it.
To remedy this, I would suggest uninstalling Pillow-PIL, PIL (if you have it) and Pillow, and then reinstalling just Pillow.
pip3 uninstall Pillow-PIL ; pip3 uninstall PIL ; pip3 uninstall Pillow ; pip3 install Pillow

Related

pyautogui / pyscreeze can't find pillow even though it is successfully installed into the same package folder and I can import it

I'm following a course called Automate the Boring Stuff, and I'm on the last part about pyautogui and pillow. I can import pyautogui and pillow successfully and I can use the keystroke functions of pyautogui but if I try to run a function in pyautogui that involves pillow it gives me this error. The function I'm trying to run is pyautogui.screenshot()
File "<input>", line 1, in <module>
File "C:\Users\offic\PycharmProjects\test\venv\Lib\site-packages\pyscreeze\__init__.py", line 134, in wrapper
raise PyScreezeException('The Pillow package is required to use this function.')
pyscreeze.PyScreezeException: The Pillow package is required to use this function.
and here is the code in pyscreeze that determines if pillow is there
try:
from PIL import Image
from PIL import ImageOps
from PIL import ImageDraw
if sys.platform == 'win32': # TODO - Pillow now supports ImageGrab on macOS.
from PIL import ImageGrab
_PILLOW_UNAVAILABLE = False
except ImportError:
# We ignore this because failures due to Pillow not being installed
# should only happen when the functions that specifically depend on
# Pillow are called. The main use case is when PyAutoGUI imports
# PyScreeze, but Pillow isn't installed because the user is running
# some platform/version of Python that Pillow doesn't support, then
# importing PyAutoGUI should not automatically fail because it
# imports PyScreeze.
# So we have a `pass` statement here since a failure to import
# Pillow shouldn't crash PyScreeze.
_PILLOW_UNAVAILABLE = True
def requiresPillow(wrappedFunction):
"""
A decorator that marks a function as requiring Pillow to be installed.
This raises PyScreezeException if Pillow wasn't imported.
"""
#functools.wraps(wrappedFunction)
def wrapper(*args, **kwargs):
if _PILLOW_UNAVAILABLE:
raise PyScreezeException('The Pillow package is required to use this function.')
return wrappedFunction(*args, **kwargs)
return wrapper
if I try to just type 'from PIL import Image' it gives this error
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm Edu 2019.1\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "C:\Users\offic\PycharmProjects\test\venv\Lib\site-packages\PIL\Image.py", line 93, in <module>
from . import _imaging as core
ImportError: cannot import name '_imaging' from 'PIL' (C:\Users\offic\PycharmProjects\test\venv\Lib\site-packages\PIL\__init__.py)
and this is when I installed Pillow
C:\Users\offic>py -m pip install --user Pillow
Requirement already satisfied: Pillow in c:\users\offic\pycharmprojects\test\venv\lib\site-packages (7.1.1)
this is what pillow looks like in my package directory
and pyscreeze says it's supposedly satisfied with the install of Pillow
C:\Users\offic>py -m pip install --user pyscreeze
Requirement already satisfied: pyscreeze in c:\users\offic\pycharmprojects\test\venv\lib\site-packages (0.1.26)
Requirement already satisfied: Pillow>=5.2.0 in c:\users\offic\pycharmprojects\test\venv\lib\site-packages (from pyscreeze) (7.1.1)
I don't know what is going on because I've had almost no problems with installing packages before and it seems to be in the right location and up to date. Please help me!
Even I had the same error, here is what worked for me:
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
I found this in the official documentation of Pillow package
I'm using windows and this method worked for me.
pip install --upgrade pip
pip install --upgrade Pillow
Despite the misleading contextual evidence, what you are looking for is the cv2 library, not PIL strictly.
So install cv2, e.g. using pip:
pip install opencv-python
Then import it within your Python script:
import cv2
Then run it.
I also had the same issue, I have the latest version of the python interpreter, the latest version of the pyscreeze, I read the docs, but nothing found, I google it, but hopelessly nothing,
But I was about losing hope when I found this, we can call the pyscreeze module instead of the pyautogui module,
import pyautogui
import pyscreeze
image = pyscreeze.screenshot('screenshot.png')
print(image.size)
Finding more at https://pypi.org/project/PyScreeze/
For my case, in pycharm,
move these two files:
PIL,
Pillow-9.4.0.dist-info,
from
C:\Users\xxxxxxxxxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages
to PyCharm project
C:\Users\xxxxxxxxxxxx\PycharmProjects\xxxxxxxxxxxx\venv\Lib\site-packages
enter image description here
It's a version issue, you can solve it by
uninstalling current Pillow version
and re-installing the proper version, e.g. 8.1.2
pip uninstall Pillow,
pip install Pillow==8.1.2
(call me "LEI, FENG")

I am getting the error : ModuleNotFoundError: No module named 'PIL'

This error is coming from this line:
from PIL import ImageTk, Image
Anyone have any suggestions?
You need to install PIL first with:
python3 -m pip install Pillow

Have pillow installed and yet getting 'ImportError: No module named PIL'

Pillow was working fine for me and then I got this error :
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 2452, in open
% (filename if filename else fp))
IOError: cannot identify image file 'dataSet/.DS_Store'
From answers on SO I got that it is possible I have both PIL and pillow installed and so I uninstalled PIL and installed pillow using easy_install and ever since then I keep getting this error.
ImportError: No module named PIL
I tried everything.
pip install image
pip install pillow
pip upgrade pip
import PIL from Image
pip --version gives : pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)
pip freeze | grep Pillow gives : Pillow==4.1.0
The code I'm running is:
import os
import cv2
import numpy as np
from PIL import Image
And at line 4 I keep getting the error
Traceback (most recent call last):
File "finder.py", line 4, in <module>
from PIL import Image
ImportError: No module named PIL
What else can I do to fix the situation? Did I read all the posts related to this topic on SO? Pretty much. And there are MANY posts related to it as well,but please,help me.
It seems that You have Installed Pillow for python3.*
pip install pillow
Install the right version for python2.7
from here
https://pypi.python.org/pypi/Pillow/4.0.0
python2.7 version has cp27 in it's name, download the right wheel file based on your system architecture.
pip install some-package.whl
I hope it helps.
Regarding the first error, this is an expected error:
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 2452, in open
% (filename if filename else fp))
IOError: cannot identify image file 'dataSet/.DS_Store'
You're trying to open a non-image with Pillow, and it's responding by saying it cannot open it. This is correct behaviour.
I guess you're trying to open all the images in a directory, but it's failing on .DS_Store, which a a metadata file created by macOS when viewing the contents of the directory. This file won't have existed before you looked in the directory with Finder, hence the code worked. What you need to do is either ignore this file (as you would with Thumbs.db on Windows), be more careful with globbing (eg. make sure only .jpg or .png or such) or handle the IOError with a try:/except IOError: block.

PIL and pillow. ImportError: cannot import name '_imaging'

Running
from PIL import Image
import pytesseract as pt
text = pt.image_to_string(Image.open("text.png"))
gives me
Traceback (most recent call last):
File "C:\Users\Rasmus\workspace\PythonMTGO\src\OCR.py", line 1, in <module>
from PIL import Image
File "C:\Users\Rasmus\AppData\Local\Programs\Python\Python35\lib\site-packages\PIL\Image.py", line 66, in <module>
from PIL import _imaging as core
ImportError: cannot import name '_imaging'
I installed pillow from https://pypi.python.org/pypi/Pillow/3.0.0 for python 3.5
I read an answer that PIL and pillow can't work together? But if I install from above link with the windows msi installer it'll install PIL and pillow and put it into C:\Users\Rasmus\AppData\Local\Programs\Python\Python35\Lib\site-packages
I've spend an entire day getting 3 lines of code to work. Hope anyone know what may be wrong.
What is your version of pillow,
Pillow >= 2.1.0 no longer supports “import _imaging”. Please use “from
PIL.Image import core as _imaging” instead.
this is the official document
https://pillow.readthedocs.io/en/5.1.x/installation.html#warnings
Uninstall and install pillow
mostly this will solve this issue

error importing pptx library python

i want to import pptx but i'm using this
import sys
sys.path.insert(0,'D:/apera/python27/python-pptx-0.5.6')
import pptx
but somehow it shows error like this
Traceback (most recent call last):
File "D:/apera/Workspace/Python scripting test 6/ppt.py", line 5, in <module>
import pptx
File "D:/apera/python27/python-pptx-0.5.6\pptx\__init__.py", line 15, in <module>
from pptx.api import Presentation # noqa
File "D:/apera/python27/python-pptx-0.5.6\pptx\api.py", line 14, in <module>
from pptx.package import Package
File "D:/apera/python27/python-pptx-0.5.6\pptx\package.py", line 16, in <module>
from .parts.image import Image, ImagePart
File "D:/apera/python27/python-pptx-0.5.6\pptx\parts\image.py", line 13, in <module>
import Image as PIL_Image
ImportError: No module named Image
and when i want to install PIL it can't install for 64 bit. Is there a ppt library that don't need PIL?
remove the PIL package due to conflicts with Pillow and python-pptx
Delete the PIL directory located in C:\Users\user1\AppData\Local\Continuum\Anaconda\Lib\site-packages\ or wherever your library is
run pip install python-pptx to install Pillow
I had about the same issue. When I updated the pillow library (pretty easy to do with Anaconda which I use) the issue was gone.
Uninstall Pillow
Uninstall python-pptx
Install Pillow
Install python-pptx
Run above commands using "pip". This worked for me.
I had the same problem.
My system is Windows XP 32bit and the python version is 2.7.13.
I just degraded the version of pillow and python-pptx. It worked for me
pip install pillow==3.1.0
pip install python-pptx==0.5.5

Categories