difference in using the import and from * import statement - python

When I install PIL http://www.pythonware.com/products/pil/ on 32 bit python, I can use import Image and then use the Image library inside PIL. However, for the 64 bit python, I downloaded http://www.lfd.uci.edu/~gohlke/pythonlibs/. However, when I use import Image, it gives me error, "No Module named Image". However, I can use from PIL import Image.
Why is that I can use import Image directly in one instance and not in another?

You want to install Pillow instead.
It comes in both 32 and 64 bit variants for Windows, and has fixed long-standing issues with the original PIL library.
The PIL library has long been broken when it comes to packaging. Pillow was started as a fork to fix these issues once and for all, and version 2.0.0 added Python 3 support (with a grant from the Python Software Foundation).

They're different libraries. Or, to be specific, they're different packaging of the same library.
PIL is notorious for these sort of issues. Just use Pillow instead.

Related

Is there a way to capture an image of the screen in python with ctypes?

I want to have python save an image file of the whole screen as a variable with ctypes so that I could access the screen in a program and do something with it (like put it on a pygame window). I can't use any other libraries unless they are included with python (no installing or pip). Does anyone know how to do this?
Edit: I'm using windows 10.
PIL.ImageGrab is from PILLOW (a python image library fork which you can install with pip). You can give a bounding box or capture the entire screen.
Update: OP now mentions he can't use external libraries.
Then you could virtually hit printscreen and read the clipboard. The code of PILLOW is open-source feel free to use it.
Remember that you can always call a command from within python:
>>> import os
>>> os.system("pip install pillow")
Or download the zip of the library and import it in your code.

python module that imports another python module

I'm new to python and I'm having an issue importing a module that imports numpy,PIL and os packages. I'll try and be as clear as possible with my problem
So I have a module lets call it preprocessing.py in which I've written a class to process an image imported from PIL using Image and converting it to a numpy array so the structure looks like the following (note method1 converts a jpg to numpy array)
----- preprocessing.py
import numpy as np
import os
from PIL import Image
Class process_object:
method1
Now I want to use this module as follows I want to import process_object from preprocessing.py and use method1 to process an image again imported using Image in PIL. So my script computation.py looks like the following
---computation.py
import os
import numpy as np
from PIL import Image
a = process_image(input)
a.method1()
However, when I do this I get the following error message
ImportError: No module named numpy
Could someone explain to me what is going on and how to fix it? I'd really appreciate an explanation which allows me to understand what is going on under the hood, so I can avoid situations like this. I really appreciate any help! Thanks!!
Check in which version of Python pip is installing numpy. It could be that when pip installs it, it's pointing to a different Python version on your system.
For problems like these, I would recommend using:
https://github.com/pyenv/pyenv-virtualenv
Will handle Python versions for you, so that you can differentiate which packages are being installed.
I will also recommend using PyCharm's Community Edition.
https://www.jetbrains.com/pycharm/download
Excellent tool and lets you create your own environment.
Hope this helps.
https://sourceforge.net/projects/numpy/files//NumPy/1.5.0/NOTES.txt/view. This is the support for numpy in Python 3.0. You probably need a newer version of numpy. You can also use:
pip install numpy
or
pip3 install numpy

Python PIL import Mac compatiblity

I wonder now for a long time, can I redirect python imports.
So on my Machine I did:
pip install pil
and then I can do:
from PIL import Image
That works great but the problem is many programs just want Image, they want:
import Image
Now my question is can I "redirect" the above statement so that I can use import Image?
If that does not work, how could I make it work?
Basically you can use any of the methods mentioned here.
On my linux installation, PIL already uses one of those - there's a directory called PILcompat which has files like Image.py, and a file called PILcompat.pth which is used to tell python to add this directory to its module search path.
This way when
import Image
is executed, python finds PILcompat/Image.py which executes its contents -
from PIL.Image import *
I'm guessing either the .pth file or the directory are missing on your machine.

cannot import cv on OpenCV2.1

I have downloaded OpenCV2.1 and Python2.6.
when i try to import cv using sys.path.append("C:\OpenCV2.1\Python2.6\Lib\site-packages") and then import cv, it tells me ImportError: DLLload failed..
I have tried the solutions given in previous thread to copy the two files in the above path to C:\Python26\Lib\site-packages, but i still can't import cv.
Is it because I missed out doing some steps after installing? What could be the reasons to why the system cannot find the module cv?
The OpenCV DLLs seem not to be found. Did you add OpenCV's bin directory to your PATH, "C:\OpenCV2.1\bin" in your case?
To use the Python wrappers
sys.path.append("C:\OpenCV2.1\Python2.6\Lib\site-packages")
import cv
should work, as well as copying cv.lib and cv.pyd to your C:\Python26\Lib\site-packages (assuming you installed Python to C:\Python26).
But those wrappers have to find the DLLs they should wrap. So make sure, they are in a path Python can find them.
Maybe this question Installing OpenCV on Windows 7 for Python 2.7 may help, especially Gia Thuy's journal post he mentions in his answer. Although he uses Python 2.7 and OpenCV 2.2, the procedure remains the same.

Difference between "import cv" and "import opencv.cv" using Python + OpenCV?

I'm trying to use OpenCV with Python and converting some C++ code. Anyway, if I do:
import cv
img = cv.LoadImage('image.jpg')
It's ok. Or:
import opencv.cv as opcv
size = opcv.cvSize(40, 50)
But anyway, the cv module doesn't have the cvSize data structure and the opencv.cv doesn't have the LoadImage. So, what exactly does each module have? I tried looking in the documentation but couldn't find it. Am I supposed to use it like this or is my setup misconfigured?
The real answer is :) that both "import opencv.cv" or "from opencv import cv" are the old-style wrapping imports.
Since OpenCV 2.0, the new-style Python wrapping is used, and the style you should use looks like this:
# import only cv, no opencv
# this also brings in sub modules such as highgui
import cv
# no "cv" prepended before all method names
src_mat = cv.LoadImageM('yourfilename.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
# let's show the image in a window
cv.NamedWindow('your name', 1)
cv.ShowImage('your name', src_mat)
cv.WaitKey
The old-style wrappings made use of SWIG, the new-style wrappings, judging by the opencv 2.2 source code, seem to be self-made.
With:
import cv
You're importing the cv module from wherever it exists in the python modules search directories. This could be a different version of the module stored somewhere outside the opencv package install as it appears to be in this case.
But with:
import opencv.cv
You're explicitly importing the opencv packages version of cv, i.e. the one in the install directory for the opencv package. This version almost guarantees you have the one from the opencv package and it seems to be the same as using the syntax:
from opencv import cv

Categories