python-2.x

Python: How to resize an image using PIL module

主宰稳场 提交于 2019-11-28 07:31:54
问题 I'm trying to resize an image to 500x500px but got this error: File "C:\Python27\lib\site-packages\PIL\Image.py", line 1681, in save save_handler = SAVE[format.upper()] KeyError: 'JPG' This is the code: from PIL import Image img = Image.open('car.jpg') new_img = img.resize((500,500)) new_img.save('car_resized','jpg') 回答1: You need to set the format parameter in your call to the save function to 'JPEG': from PIL import Image img = Image.open('car.jpg') new_img = img.resize((500,500)) new_img

Preserve case in ConfigParser?

有些话、适合烂在心里 提交于 2019-11-28 05:36:31
I have tried to use Python's ConfigParser module to save settings. For my app it's important that I preserve the case of each name in my sections. The docs mention that passing str() to ConfigParser.optionxform() would accomplish this, but it doesn't work for me. The names are all lowercase. Am I missing something? <~/.myrc contents> [rules] Monkey = foo Ferret = baz Python pseudocode of what I get: import ConfigParser,os def get_config(): config = ConfigParser.ConfigParser() config.optionxform(str()) try: config.read(os.path.expanduser('~/.myrc')) return config except Exception, e: log.error

how to pick just one item from a generator (in python)?

别来无恙 提交于 2019-11-28 04:13:38
I have a generator function like the following: def myfunct(): ... yield result The usual way to call this function would be: for r in myfunct(): dostuff(r) My question, is there a way to get just one element from the generator whenever I like? For example, I'd like to do something like: while True: ... if something: my_element = pick_just_one_element(myfunct()) dostuff(my_element) ... Create a generator using g = myfunct() Everytime you would like an item, use next(g) (or g.next() in Python 2.5 or below). If the generator exits, it will raise StopIteration . You can either catch this

Is is safe to use a function accepts kwargs keyword arguments that are not identifiers?

梦想的初衷 提交于 2019-11-28 04:05:51
问题 In Python, is it safe to give keyword arguments that are not Python identifiers to a function? Here is an example: >>> '{x-y}'.format(**{'x-y': 3}) # The keyword argument is *not* a valid Python identifier '3' >>> '{x-y}'.format(x-y=3) File "<ipython-input-12-722afdf7cfa3>", line 1 SyntaxError: keyword can't be an expression I am asking this because it is more convenient for me to format with names that contain a dash (because the values correspond to command-line argument with dashes in

Which encoding is used for strings in Python 2.x?

那年仲夏 提交于 2019-11-28 04:04:05
问题 What is the default encoding used for encoding strings in python 2.x? I've read that there are two possible ways to declare a string. string = 'this is a string' unicode_string = u'this is a unicode string' The second string is in Unicode. What is the encoding of the first string? 回答1: As per Python default/implicit string encodings and conversions (reciting its Py2 part concisely, to minimize duplication): There are actually multiple independent "default" string encodings in Python 2, used

In Python 2, how do I write to variable in the parent scope?

一曲冷凌霜 提交于 2019-11-28 03:38:20
I have the following code inside a function: stored_blocks = {} def replace_blocks(m): block = m.group(0) block_hash = sha1(block) stored_blocks[block_hash] = block return '{{{%s}}}' % block_hash num_converted = 0 def convert_variables(m): name = m.group(1) num_converted += 1 return '<%%= %s %%>' % name fixed = MATCH_DECLARE_NEW.sub('', template) fixed = MATCH_PYTHON_BLOCK.sub(replace_blocks, fixed) fixed = MATCH_FORMAT.sub(convert_variables, fixed) Adding elements to stored_blocks works fine, but I cannot increase num_converted in the second subfunction: UnboundLocalError: local variable 'num

Python string to unicode [duplicate]

感情迁移 提交于 2019-11-28 03:11:33
Possible Duplicate: How do I treat an ASCII string as unicode and unescape the escaped characters in it in python? How do convert unicode escape sequences to unicode characters in a python string I have a string that contains unicode characters e.g. \u2026 etc. Somehow it is not received to me as unicode , but is received as a str . How do I convert it back to unicode? >>> a="Hello\u2026" >>> b=u"Hello\u2026" >>> print a Hello\u2026 >>> print b Hello… >>> print unicode(a) Hello\u2026 >>> So clearly unicode(a) is not the answer. Then what is? Unicode escapes only work in unicode strings, so

“ImportError: DLL load failed” when trying to import pymssql on Windows

南楼画角 提交于 2019-11-28 02:05:06
I'm trying to use the example code from here: http://www.pymssql.org/en/latest/pymssql_examples.html I installed the pymmsql module using pip . I also see it in the site-packages folder C:\Python27\Lib\site-packages\pymmsql.pyd But, when I execute the code, I get the following error: Traceback (most recent call last): File "C:\Android\android_workspace\pythonProject\test.py", line 2, in <module> import pymssql ImportError: DLL load failed: The specified module could not be found. What am I missing? Update Due to negative feedback from the user community regarding the 2.1.2 change, pymssql 2.1

Get complete list of all possible Class Attributes

纵饮孤独 提交于 2019-11-28 01:27:44
问题 Is there a way, given a simple Class, to output all the possible attributes for it? Standard attributes like __class__ and __doc__ and special read only attributes like __mro__ , __bases__ et al. Generally, all present attributes? Considering the most simplistic case for a Class: class myClass: pass The dir() , vars() and inspect.getmembers() all exclude certain builtin attributes. The most complete list is offered by using myClass.__dir__(MyClass) which, while adding built in attributes,

Why is this division not performed correctly?

我是研究僧i 提交于 2019-11-28 01:09:02
I've a strange issue in Python: the division is not performed correctly: print pointB[1] print pointA[1] print pointB[0] print pointA[0] print (pointB[1]-pointA[1]) / (pointB[0]-pointA[0]) These are the results: 100 50 100 40 0 thanks user225312 The above behavior is true for Python 2. The behavior of / was fixed in Python 3. In Python 2 you can use: from __future__ import division and then use / to get the result you desire. >>> 5 / 2 2 >>> from __future__ import division >>> 5 / 2 2.5 Since you are dividing two integers, you get the result as an integer. Or, change one of the numbers to a