Problems using PIL 1.1.7 - python

The trivial
import Image
im = Image.OPEN('C:\abc.bmp')
results in the following exception
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
im = Image.OPEN('C:\Documents and Settings\umair.ahmed\My Documents\My Pictures\avanza.bmp')
TypeError: 'dict' object is not callable
not sure if i am missing something, kindly help.

Use:
Image.open()
It's case-sensitive.

I don't think the error message came from your input, because the file names are different, but you should not use 'C:\abc.bmp', in your open() call, but use either C:/abc.bmp, or r'C:\abc.bmp'. Backslash is an escape character in Python.

Related

strftime returns ValueError

strftime returns Value error. Why?
This is the code
datetime.datetime.now().strftime('%-m')
My output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format string
Based on strftime.org format can depend on system.
On Unix it may needs - like "%-m" but on Windows it may need # like "%#m".
I could test it only on Linux and both works for me: "%-m %#m"
BTW: but if you want to get negative value then you need - before % like "-%m"

Ho to fix playsound excpetion "playbin.set_state returned <enum GST_STATE_CHANGE_FAILURE"?

I am trying to make a simple metronome in Python using the playsound module. But when I run it, there's the following error:
Traceback (most recent call last):
File "/run/media/mb/Volume/Dokumente/Coding/Python/Metronom/metronom.py", line 5, in <module>
playsound("Cowbell.wav")
File "/usr/lib/python3.7/site-packages/playsound.py", line 106, in _playsoundNix
"playbin.set_state returned " + repr(set_result))
playsound.PlaysoundException: playbin.set_state returned <enum GST_STATE_CHANGE_FAILURE of type Gst.StateChangeReturn>
#if its in the same folder as project
playsound('file.mp3')
#somewhere else? then full path (REMEMBER PYTHON USES FORWARDSLASHES /, NOT BACKSLASHES \)
playsound('C:/your/path/to/file.mp3')
Also be aware that some files will throw a playsound exception if they have a high enough bitrate (idk why, i suppose playsound cant handle some high quality bitrates)

Python-Weka-Wrapper3 removing attributes from arff file error

I have an arff file and I need to remove the first 5 attributes from it (without manually deleting them). I tried to use the Python-Weka-Wrapper3 as it is explained here which enables the filtering options of Weka, however I get an error while using the following code:
import weka.filters as Filter
remove = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "1,2,3,4,5"])
The error that I receive is the following:
Traceback (most recent call last):
File "/home/user/Desktop/file_loading.py", line 16, in <module>
removing = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "last"])
TypeError: 'module' object is not callable
What could be the reason for this error? Also I would appreciate if anyone knows an alternative way to remove attributes from an arff file using Python.
You are attempting to call the module object instead of the class object.
Try using:
from weka.filters import Filter
remove = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "1,2,3,4,5"])

Python hex to binary fail

I'm trying to convert a hex string 'aa' to binary as following:
a = bin(int('aa',16))
But it gives me the error of:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
a = bin(int('aa',16))
TypeError: bin(QTextStream): argument 1 has unexpected type 'int'
Can anyone explain what is the problem with the conversion?
You did some sort of import *, probably
from PyQt4.QtCore import *
causing the built-in bin to be shadowed by a different function. Stop using import *, and the problem will go away.

Cannot perform operations using rpy2 in Python: "TypeError: argument 1 must be a str, not int"

So I'm trying to get to grips with using the rpy2 module (I am familiar with R but new to Python). Following this tutorial, I first load the library and assign it to the variable 'r' using:
import rpy2
import rpy2.robjects as robjects
r = robjects.r
then I try to perform a simple operation to confirm everything is working:
print(r[2+2])
but I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\rpy2\robjects\__init__.py", line 248, in _
_getitem__
res = _globalenv.get(item)
TypeError: argument 1 must be str, not int
I'm sure it's just something stupid I'm doing wrong, but any advice would be much appreciated. I'm using python3.4.2 (64bit), rpy2-2.5.6 (64bit) on a Windows 7 machine (64bit).
You should use print(r(2+2)) instead of print(r[2+2]).
When you use r[2+2] you are trying to recover an element corresponding to the index 4 (the result of 2+2) of the r iterable. And your r object doesn't seem to respond to this kind of message.
Ok I think I have figured it out. For R to evaluate the function inside the parenthesis, the function must be in quotes e.g.
r("2+2")
This is what was confusing me because this looks like I'm providing a string.
Oddly I don't print the result (4) by using:
print(r("2+2"))
as this prints:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
print(r("2+2"))
File "C:\Python34\lib\site-packages\rpy2\robjects\robject.py", line 49, in __str__
s = str.join(os.linesep, s)
TypeError: sequence item 0: expected str instance, bytes found
Instead I just print the result using:
answer = r("2+2")
answer[0]
(Because R is vector based, the initial value of the vector is the answer so you have to index it at the first position, otherwise you get:
answer = r("2+2")
answer
<FloatVector - Python:0x0000000005836EC8 / R:0x00000000047A51A0>
[4.000000]
Thanks for you help
Hefin

Categories