jython: how to crop a png image file - png

I tried PIL (Python Image Library). Unfortunately, jython doesn't support it.
import Image
im = Image.open("tmp.png")
box = (100,200,300,400)
region = im.crop(box)
And the error message is
File "dota\PIL\Image.py", line 37, in __getattr__
raise ImportError("The _imaging C module is not installed")
ImportError: The _imaging C module is not installed
Is there other way?

Related

OSError: libDTKLPR5.so: cannot open shared object file: No such file or directory

enter image description heretrying to use cdll.LoadLibrary function in Python 3.11.0 in Ubuntu 22.04.1 LTS 64-bit.
Below error occured. If you have faced this kind of issue please help.
Traceback (most recent call last):
File "/home/*****/Desktop/python-camera-app-latest/radius-camera-app/main.py", line 10, in <module>
main()
File "/home/*****/Desktop/python-camera-app-latest/radius-camera-app/main.py", line 5, in main
handler = DTKVideoStreamHandler()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/*****/Desktop/python-camera-app-latest/radius-camera-app/Services/Implements/DTKVideoStreamHandler.py", line 28, in _init_
self.__DTKLPR = cdll.LoadLibrary(DTKLPR5Lib)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/ctypes/_init_.py", line 454, in LoadLibrary
return self._dlltype(name)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/ctypes/_init.py", line 376, in __init_
self._handle = _dlopen(self._name, mode)
^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libDTKLPR5.so: cannot open shared object file: No such file or director
Thanks in advance...
In windows 10 64-bit. There is some kind of solution with using
os.add_dll_directory(full_path)
before calling
self.__DTKLPR = cdll.LoadLibrary(DTKLPR5Lib)
but this solution is not working in Ubuntu. Because add_dll_directory is supposed to only windows. (Based on information in its documentation)

Wordcloud installation on Windows (PIL, pillow)

Basically, I am trying to generate simple word cloud using Python on Windows. Hence, I have installed wordcloud as:
pip install wordcloud
I am trying to run the simplest example from here as below:
import os
from os import path
from wordcloud import WordCloud
# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
The error I got:
Traceback (most recent call last):
File "C:\Users\XXXXX\test_wordcloud.py", line 13, in <module>
wordcloud = WordCloud().generate(text)
File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 639, in generate
return self.generate_from_text(text)
File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 621, in generate_from_text
self.generate_from_frequencies(words)
File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 454, in generate_from_frequencies
max_font_size=self.height)
File "C:\Anaconda3\lib\site-packages\wordcloud\wordcloud.py", line 503, in generate_from_frequencies
font = ImageFont.truetype(self.font_path, font_size)
File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 959, in truetype
return freetype(font)
File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 956, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 219, in __init__
if core.HAVE_RAQM:
File "C:\Anaconda3\lib\site-packages\PIL\ImageFont.py", line 58, in __getattr__
raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed
The PIL and Python versions on my side are:
from PIL import Image, ImageDraw, ImageFilter, ImageFont
print('PIL',PIL.__version__)
import sys
print(sys.version)
as:
PIL 9.2.0
3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)]
Any suggestions ?
Thanks
On Windows 10, install the FreeType binding. Hopefully, it is resolved by re-installing the Pillow with disabling the pip cache as:
pip install freetype-py
pip uninstall pillow
pip install --no-cache-dir pillow
Once pillow is recompiled, everything worked properly for me!
In addition, the following check:
from PIL import features
print(features.check('freetype2'))
was False before the recent PIL re-installation. However, it is converted
into True after above listed steps. By the way, no need to reboot!

Google Coral USB Accelerator on Raspbian Lite Buster and Raspberry PI 4

I am following the getting started guide for the Google Coral USB Accelerator and even though I was able to trick the install.sh file to install for RPI4
https://twitter.com/hansamann/status/1154021771554766849
I cannot run the example with the image classification. When I run the sample, I get this:
python3 classify_image.py --model ~/Downloads/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite --label ~/Downloads/inat_bird_labels.txt --image ~/Downloads/parrot.jpg
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/edgetpu/swig/edgetpu_cpp_wrapper.py", line 18, in swig_import_helper
fp, pathname, description = imp.find_module('_edgetpu_cpp_wrapper', [dirname(__file__)])
File "/usr/lib/python3.7/imp.py", line 296, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_edgetpu_cpp_wrapper'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "classify_image.py", line 19, in <module>
from edgetpu.classification.engine import ClassificationEngine
File "/usr/local/lib/python3.7/dist-packages/edgetpu/classification/engine.py", line 17, in <module>
from edgetpu.basic.basic_engine import BasicEngine
File "/usr/local/lib/python3.7/dist-packages/edgetpu/basic/basic_engine.py", line 15, in <module>
from edgetpu.swig.edgetpu_cpp_wrapper import BasicEngine
File "/usr/local/lib/python3.7/dist-packages/edgetpu/swig/edgetpu_cpp_wrapper.py", line 28, in <module>
_edgetpu_cpp_wrapper = swig_import_helper()
File "/usr/local/lib/python3.7/dist-packages/edgetpu/swig/edgetpu_cpp_wrapper.py", line 20, in swig_import_helper
import _edgetpu_cpp_wrapper
ModuleNotFoundError: No module named '_edgetpu_cpp_wrapper'
Any idea whay I could try?
I had the same problem but with an older Rpi 3. This is the solution I used. I don't think it's a make or break to complete the project:
$ sudo cp _edgetpu_cpp_wrapper.cpython-35m-arm-linux-gnueabihf.so _edgetpu_cpp_wrapper.cpython-37m-arm-linux-gnueabihf.so
Can you go through : https://github.com/google-coral/edgetpu-platforms ?
This repository holds auxiliary platform-related material related to Google Coral Edge TPU. Here you can find precompiled images, shared libraries and patches for using the USB Edge TPU accelerator on additional platforms to the main supported ones.
For convenience Google has uploaded prebuilt images for Raspberry Pi Zero, Pi 3 and Pi 4. Simply write the image to an sd card and boot up your Pi. The images contain several examples that should work out of the box.

Spyder/Pythonxy crashes

Upon installing python xy 2.7.5 on my laptop, I tried to launch the Spyder IDE. This yielded the following error:
C:\Python27\Scripts>spyder.exe
Traceback (most recent call last):
File "C:\Python27\Scripts\spyder-script.py", line 9, in
load_entry_point('spyder==2.2.0', 'console_scripts', 'spyder')()
File "C:\Python27\lib\site-packages\spyderlib\start_app.py", line 72, in main
from spyderlib import spyder
File "C:\Python27\lib\site-packages\spyderlib\spyder.py", line 112, in
from spyderlib.plugins.ipythonconsole import IPythonConsole
File "C:\Python27\lib\site-packages\spyderlib\plugins\ipythonconsole.py", line 31, in
from IPython.frontend.qt.kernelmanager import QtKernelManager
File "C:\Python27\lib\site-packages\IPython\frontend\qt\kernelmanager.py", line 9, in
from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
File "C:\Python27\lib\site-packages\IPython\zmq\__init__.py", line 67, in
check_for_zmq('2.1.4')
File "C:\Python27\lib\site-packages\IPython\zmq\__init__.py", line 51, in check_for_zmq
import zmq
File "C:\Python27\lib\site-packages\zmq\__init__.py", line 35, in
_libzmq = ctypes.CDLL(bundled[0], mode=ctypes.RTLD_GLOBAL)
File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 127] Die angegebene Prozedur wurde nicht gefunden
Using a python console, I was able to run python code as well as matplotlib and numpy code. Ipython works, however Ipython (Qt) does not work. what could be the reason for this?
The host OS is Windows XP.
This is actually a problem with the most recent builds of zeromq, which are messaging libraries that are required for IPython, and not with Spyder itself. The latest zeromq builds are not compatible with Win XP. This is a know issue with recent versions of Python(x,y) on Win XP which can be solved by following the guidance in this bug report.
It works by installing
https://pythonxy.googlecode.com/files/pyzmq-13.0.2-4_py27.exe
and
http://h5py.googlecode.com/files/h5py-2.1.3.win32-py2.7.msi
,
As it is posted in http://code.google.com/p/pythonxy/issues/detail?id=670

Using Gurobi solver in the Enthought Canopy Editor on Linux

I am currently working with Gurobi solver using python. Previously, I have just been using a mix of a text editor and the terminal to write and run my models but I am currently experimenting with using Enthought Canopy Editor. I have a model that is works when I run in in the terminal using python model.py but when I try doing%run model.py in Canopy's iPython shell I get:
%run /home/cdhagmann/Copy/Code_Env/Farmer/model.py
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/home/cdhagmann/Canopy/appdata/canopy-1.0.1.1189.rh5-x86_64/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
181 else:
182 filename = fname
--> 183 __builtin__.execfile(filename, *where)
/home/cdhagmann/Copy/Code_Env/Farmer/model.py in <module>()
----> 1 from gurobipy import *
2 from data_IO import read_data,currency
3 import time
4
5 # Import model data
ImportError: No module named gurobipy
My guess is that the terminal and Canopy look for modules in different places but I don't know how to rectify it. I found this reference on the Gurobi group site about Enthought Canopy and Gurobi on OS X 10.8 but didn't know how much those instruction would differ from doing it on Linux and even what he meant about installing Gurobi in Canopy's site-packages.
UPDATE: Based on information found the first link I clicked on, I did in fact leave the boxed clicked making Canopy my default directory. You have to manually prepend the PATH variable, which I missed them saying that I had to do. So I did that. I now have Canopy's version of iPython running in my terminal (check by using sys.path); however, I get a huge list of errors when I try run python.
cdhagmann#Crispin ~ $ python
Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 563, in <module>
main()
File "/usr/lib/python2.7/site.py", line 545, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python2.7/site.py", line 272, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python2.7/site.py", line 247, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python2.7/site.py", line 237, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python2.7/sysconfig.py", line 578, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python2.7/sysconfig.py", line 505, in get_config_vars
import re
File "/usr/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
Suggestions?
And to answer #Jonathan questions:
sys.prefix = '/usr'
sys.path:
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/pymodules/python2.7
/usr/lib/python2.7/dist-packages/ubuntu-sso-client
gurobipy.__file__ = '/usr/local/lib/python2.7/dist-packages/gurobipy/gurobipy.so'
If the terminal is correctly configured for Canopy Python, then python in terminal and Canopy Python should be looking for modules in the same places. If during installation, you disabled "make Canopy be default Python", or if you have not restarted terminal since installation, then it won't automatically be configured correctly.
These articles should help you understand and configure:
http://docs.enthought.com/canopy/configure/faq.html#where-are-all-of-the-python-packages-in-my-user-python-environment
https://support.enthought.com/entries/23646538-Make-Canopy-s-Python-be-your-default-Python-i-e-on-the-PATH-
https://support.enthought.com/entries/23389761-Installing-packages-into-Canopy-Python-from-the-command-line
If these articles do not suffice for you to solve this, then please report -- when you run python from terminal, what are these?
sys.prefix
sys.path
gurobipy.__file__