Python import problems with image2gif - python

I know I have all packages installed (pip freeze)
I am on a Windows 10 machine and am running python3.4 and am having weird dependency problems
test.py:
from images2gif import writeGif
from PIL import Image, ImageSequence
import os
file_names = ['output\donkey-1.png', 'output\donkey-2.png']
images = [Image.open(fn) for fn in file_names]
size = (600,350)
for im in images:
im.thumbnail(size, Image.ANTIALIAS)
filename = "test.gif"
writeGif(filename, images, duration=0.5, subRectangles=False)
running test.py gives the following errors, could not find this error anywhere else on the web
Traceback (most recent call last):
File "test.py", line 2, in <module>
from images2gif import writeGif
File "C:\Python34\lib\site-packages\images2gif\__init__.py", line 1, in <module>
from images2gif import readGif as readGif
ImportError: cannot import name 'readGif'

I just looked at the actual repository source code: it uses Python 2 style relative imports. That is, images2gif does not support Python 3 out of the box.
One solution for now might be to download the sourcefile from pypi, extract that (not sure if Windows likes tar.gz files), cd into the directory and run 2to3 on it:
2to3 -w .
and then install it manually:
python3.4 setup.py install
But probably easier, just seeing the results of 2to3: manually change the two import statements in your already installed package (in __init__.py):
-from images2gif import readGif as readGif
-from images2gif import writeGif as writeGif
+from .images2gif import readGif as readGif
+from .images2gif import writeGif as writeGif
because it appears everything else is PY3K compatible; the dots in front of images2gif in those two import statements make it proper PY3K relative imports.

Related

Load "whl" module dynamically

I want to be able to load modules dynamically from a *.whl files in my app.
I'm using sys.path.append('module.whl') for that and it works in most cases, I can't get it to work if the module has an *.so file in it, it cannot find it locally, for example using bcrypt module, for eample:
import sys
sys.path.append("bcrypt-3.1.7-cp27-cp27m-macosx_10_6_intel.whl")
import bcrypt
I'm getting:
Traceback (most recent call last):
File "/Users/arossert/tests/whlimport/app.py", line 6, in <module>
import bcrypt
File "bcrypt-3.1.7-cp27-cp27m-macosx_10_6_intel.whl/bcrypt/__init__.py", line 25, in <module>
ImportError: cannot import name _bcrypt
Inside the *.whl there is a _bcrypt.so file but cannot find it, is there a way around it?
Extract the wheel yourself:
import zipfile
import sys
whlPath = '/tmp/bcrypt-3.2.0-cp36-abi3-macosx_10_9_x86_64.whl'
targetDir = '/tmp'
with zipfile.ZipFile(whlPath, "r") as whl:
whl.extractall(targetDir)
sys.path.append(os.path.join(targetDir, 'bcrypt'))
import bcrypt
print(bcrypt)
Out:
python /tmp/script.py
<module 'bcrypt' from '/tmp/bcrypt/__init__.py'>
Note:
Make sure to have all dependencies installed!
One solution would be to unzip the file (as a whl is only an archived file) and then import it like this:
from zipfile import ZipFile
path = 'bcrypt-3.1.7-cp27-cp27m-macosx_10_6_intel.whl'
print(ZipFile(path).namelist())

Python dev_appserver cant import `.so` file (Pillow)

Inside my GAE application I try this:
from PIL import Image
And get this:
Traceback (most recent call last):
...
File "/home/sheena/Workspace/Waxed/code/waxed_backend/src/waxed_backend/concerns/misc/views.py", line 57, in home
from PIL import Image
File "libs/PIL/Image.py", line 56, in <module>
from . import _imaging as core
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 1024, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named PIL._imaging
/path/to/libs/PIL/imaging.so exists. And I have no problem importing anything else from that libs file from within dev_appserver.
And this works fine:
cd /path/to/libs/
python
>>> from PIL import Image
I can only conclude that either dev_appserver somehow breaks impotrt functionality so that .so files aren't recognised.
Has anyone else seen this before? Any idea how to fix it?
_imaging is a PIL dependency written in C. Because it's C and not Python, including the library in your lib folder won't work. You need to define that library in your app.yaml file:
libraries:
- name: PIL
version: latest

importing installed python module x.features2d from Opencv

I have installed Opencv3 module on my Ubuntu 14.04 system and the installation of sub-modules done without problem. But when I called x.features2d in my code after importing cv2, as shown in the code below
import sys
import cv2
import cv2.xfeatures2d
import numpy as np
sys.path.append('/usr/local/lib/python2.7/site-packages')
img_bgr=cv2.imread('sc2.png')
img_gray=cv2.cvtColor(img_bgr,cv2.COLOR_BGR2GRAY)
template=cv2.imread('template.png',0)
w, h=template.shape[::-1]
res=cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold=0.7
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0,255,0), 2)
freakExtractor=cv2.xfeatures2d.FREAK_create()
keypoints,descriptors=freakExtractor.compute(img_bgr,keypoints)
cv2.imshow('detected', img_bgr)
cv2.waitKey(0)
it gives me the error of not being able to find x.features2d module as below
Traceback (most recent call last):
File "test.py", line 3, in <module>
import cv2.xfeatures2d
ImportError: No module named xfeatures2d
I tried to re-install it again and also installed opencv-contrib, but same problem happened.
Any idea to solve it?
You have not given many details on your installation process.
All the contrib features such as xfeatures2d are no longer part of the prebuilt libraries. This means xfeatures2d will only work if you compile openCV yourself.
Example of how to do that (e.g. for Windows) is given here:
https://putuyuwono.wordpress.com/2015/04/23/building-and-installing-opencv-3-0-on-windows-7-64-bit/

Python - Cannot import name - 3.4

I am working with numpy and Pillow (replacement for PIL in 3.4), but have been having problems with the importing of Pillow. I found a similar post here:
ImportError: Cannot import name X
However, this post was using his own created libraries and the problem was that his modules imported each other, creating circular dependent imports.
My code however does not use my own module, here is the code:
import PIL
from PIL import ImageGrab
import numpy
img = ImageGrab.grab()
imgLoad = img.load()
size = img.size()
And this then returns the error:
Traceback (most recent call last):
File "E:/Family Documents/Matthew's Documents/Python/PIL.py", line 1, in <module>
import PIL
File "E:/Family Documents/Matthew's Documents/Python\PIL.py", line 2, in <module>
from PIL import ImageGrab
ImportError: cannot import name 'ImageGrab'
One other interesting thing about this is that when I first installed Pillow (PIL), I tried it in the shell and the "from PIL import ImageGrab" worked.
Also, if I restart the shell (close it and re-open it) the commands, being manually typed in, work as well. This suggests to me that something is bugging out with python, since retyping "import PIL" throws the same error message "cannot import name 'ImageGrab'".
Thanks for any help
Ha, this already bit me multiple times.
Your traceback shows the file name:
E:/Family Documents/Matthew's Documents/Python/PIL.py
Your PIL.py is found first, so you're trying to import names from the module that's just executing, not from the actual library you have installed.

Unable to import PIL.Image for qrcode

I run into this weird error, I need to use qrcode with pillow, so I did pip install pillow qrcode (after initiating the virtual environment). Then, the following thing happens
>>> from PIL import Image
>>> Image
<module 'PIL.Image' from '/vagrant/env/local/lib/python2.7/site-packages/PIL/Image.pyc'>
>>> import qrcode;
>>> qrcode.make("1").show()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/vagrant/env/local/lib/python2.7/site-packages/qrcode/main.py", line 8, in make
return qr.make_image()
File "/vagrant/env/local/lib/python2.7/site-packages/qrcode/main.py", line 186, in make_image
from qrcode.image.pil import PilImage
File "/vagrant/env/local/lib/python2.7/site-packages/qrcode/image/pil.py", line 5, in <module>
import Image
ImportError: No module named Image
from PIL import Image works but qrcode doesn't work. Not sure what is going on
install PIL on Ubuntu:
https://askubuntu.com/questions/156484/how-do-i-install-python-imaging-library-pil
header for your program, code:
##some magic for 3rd party packages
import site
site.main()
##headers
from PIL import Image
##programm
#here your program!!!
--
Your friend!!!

Categories