Difference between from PIL import Image and import Image - python

I tried to use Python's Image Module on my Mac (new to mac)and I had trouble setting it up. I have Yosemite and some of the posts that I found online didn't work. I ended up installing Homebrew and I finally got PIL on my computer. However, instead of using import image (which I saw people doing it online), I have to use from PIL import image. Is there key difference between import Image and from PIL import Image. It's the first time that I actually use Image module.
One more question, do I actually need to install third party tools like Homebrew and Macports to set up my environment?

If you are using PIL, you might be able to use import Image.
If you are using Pillow, you must use from PIL import Image.
This is by design.

1-Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL.
2-Pillow >= 1.0 no longer supports “import Image”. Please use “from PIL import Image” instead. so be careful with it
3-Pillow >= 2.1.0 no longer supports “import _imaging”. Please use “from PIL.Image import core as _imaging” instead.

In python, modules are represented by *.py files. These files can be imported using import module. That statement will first search for a build-in module, if it fails to find one it will search in a given list of directories (PYTHONPATH).
When you imported your module, you can then access names (aka functions or classes) via module.name.
However, when using from module import myFunc you can reference that function directly without using the modulename. The same goes for from module import *.
Now, to the PIL:
Image is a name (here class) in the PIL module. As, import Image searches for modules called Image. It can't find one, because Image is part of the PIL module.
You can also look at the documentation here: Modules in Python
I hope this helped you understanding modules better.

Related

Alternative for PIL?

I want to use one of the newest versions of Python, (3.5+), and I need to import Image. I use:
from PIL import Image
except this returns an error. I don't have the error on my right now but the important part is there is a problem with PIL (I have already determined that from my Python Discord server)
Can I use pillow? I think I have that installed. I would try it but I want to know how (and if) it's applicable. Thanks
Can I use pillow?
Sure. pillow is a successor project of PIL, as development on the latter was last continued in 2011.
The first thing mentioned in the official pillow documentation is how to import the Image class.

How to find GLCM matrix in python?

We are using Python 2.7.9.and scikit image library. We are not able to use skimage.feature.greycomatrix because there is no file such as greycomatrix.py in the feature folder. Their documentation seems to be wrong as it says this function is available. We get an error module attribute has no object feature. Is there any other image processing library in Python which will help us achieve this goal?
from skimage.feature import greycomatrix
This works for me, just like the instructions at http://scikit-image.org/docs/dev/auto_examples/plot_glcm.html.
Maybe you just have an old version and need to update scikit-image? Or if it was actually a bug on their behalf they have probably fixed it now.
Which version of scikit-image do you have? That is more relevant for this problem than your Python version. For me:
import skimage
print skimage.__version__
'0.11.2'
So for version 0.11.2 up atleast it should work.
In my case I use the following imports and it works for me.
I say
import skimage.feature as sk
##Then I say
glcm = sk.greycomatrix(img, [1],[0])
This should work fine.

Import threshold_yen and threshold_isodata from skimage.filter

I am working with different threshold algorithms from SKimage, and when I go to import certain packages I get an error, but have no problem with others. For example:
from skimage.filter import threshold_adaptive, threshold_isodata
returns the traceback:
ImportError: cannot import name threshold_isodata. I am using python 2.7, and following the documentation found here: http://scikit-image.org/docs/dev/api/skimage.filter.html#skimage.filter.threshold_isodata
Specifically, I'm hoping to use threshold_isodata and threshold_yen. Does anybody have suggestions for how to solve this error? Alternatively, are there other packages that use the same algorithm?
As mentioned in a comment, threshold_isodata is only available in the master repo (i.e. not officially released in v0.9), hence the import error.
It turns out that threshold_yen wasn't properly imported into the filter subpackage in version 0.9. (This has been fixed in master.) Until v0.10 is released, you should import threshold_yen as follows:
from skimage.filter.thresholding import threshold_yen
EDIT: Note that this question and answer are specific to very old versions of scikit-image. The skimage.filter module was renamed skimage.filters in v0.11

Not Able to import PIL in Sikuli

I want to import PIL in Sikuli for doing that I have added the code below
import sys
sys.path.append("C:\\Python27\Lib\site-packages")
sys.path.append("C:\\Python27\Lib\site-packages\PIL")
import PIL
import Image
So it imported the above modules but when I am trying to import ImageFont, it's giving me the error below:
def getmask2(self, text, mode="", fill=Image.core.fill):
File "C:\Python27\Lib\site-packages\PIL\Image.py", line 39, in __getattr__
raise ImportError("The _imaging C module is not installed")
ImportError: The _imaging C module is not installed
I am able to import PIL and Image and ImageFont successfully in Python scripts and code is running fine.
Any idea how to fix this?
The Sikuli IDE uses Jython so packages installed to your local Python version won't be able to be imported within the Sikuli IDE and Jython doesn't support c extensions as far I'm aware which PIL requires ( see http://bugs.jython.org/issue1388 ).
I think you would need to use a Java library, maybe something like ImageJ ( see http://fiji.sc/wiki/index.php/Jython_Scripting )
Since the following (as I think valuable information) was rejected as an edit to the original question (my intention: people should see this in the first place), I put it as an answer now:
I want to add:
Python modules can be imported in Jython/Sikuli scripts as long as they contain only Python code. There are many modules written for Python, that hold this condition (e.g. xlrd, xlwt). But as correctly mentioned: PIL contains C-based stuff and this can normally (exception JNI libs) not be loaded (hence the above error message, which is true, but might lead in the wrong direction).

Is the Python Imaging Library not available on PyPI, or am I missing something?

easy_install pil results in an error:
Searching for pil
Reading http://pypi.python.org/simple/pil/
Reading http://www.pythonware.com/products/pil
Reading http://effbot.org/zone/pil-changes-115.htm
Reading http://effbot.org/downloads/#Imaging
No local packages or download links found for pil
error: Could not find suitable distribution for Requirement.parse(‘pil’)
Any ideas?
--
UPDATE:
Hm, asking it to find-links on the Python Ware site seems to be working:
easy_install -f http://www.pythonware.com/products/pil/ Imaging
Got a heap of warnings along the way though. I’ll see how it turns out.
--
UPDATE: I can import it in Python using import Image, but when I tell Django to syncdb I still get the following error:
Error: One or more models did not validate:
core.userprofile: “avatar”: To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .
I'm using an ImageField in one of my models.
Of course PIL is on PyPi! Specifically, it's right here.
easy_install is case-sensitive. The package is under PIL.
workaround is in easy_install PIL egg directory create link to this directory in name "PIL"
import Image
Django tries to import PIL directly:
from PIL import Image
You should check presence of PIL directory in your site-packages
from http://code.djangoproject.com/ticket/6054
It seems that there is something wrong with easy_install which installs PIL in the root namespace, installing from sources fixes this, strange.

Categories

Resources