How would I center-align (and middle-vertical-align) text when using PIL? Answer Deprecation Warning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead. Code using textbbox instead of textsize. Result Use Draw.textsize method to calculate text size and re…
Tag: python
Convert NumPy array to Python list
How do I convert a NumPy array into a Python List? Answer Use tolist(): Note that this converts the values from whatever numpy type they may have (e.g. np.int32 or np.float32) to the “nearest compatible Python type” (in a list). If you want to preserve the numpy data types, you could call list() o…
Python Datatype for a fixed-length FIFO
I would like to know if there is a native datatype in Python that acts like a fixed-length FIFO buffer. For example, I want do create a length-5 FIFO buffer that is initialized with all zeros. Then, it might look like this: [0,0,0,0,0] Then, when I call the put function on the object, it will shift off the la…
Django TemplateDoesNotExist?
My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk. For every URL I request, it throws: TemplateDoesNotExist at /appname/path appname/template_name.html Django tried loading these templates, in this order: * Using loader django.template.loaders.f…
How to completely sanitize a string of illegal characters in python?
I have a feature of my program where the user can upload a csv file, which my program goes through and uses as input. I have one user complaining about a problem where his input is throwing up an error. The error is caused by there being an illegal character that is encoded wrong. The characters is below: Som…
How to split an integer into a list of digits?
Suppose I have an input integer 12345. How can I split it into a list like [1, 2, 3, 4, 5]? Answer Convert the number to a string so you can iterate over it, then convert each digit (character) back to an int inside a list-comprehension:
How to make a Tkinter window jump to the front?
How do I get a Tkinter application to jump to the front? Currently, the window appears behind all my other windows and doesn’t get focus. Is there some method I should be calling? Answer Assuming you mean your application windows when you say “my other windows”, you can use the lift() method…
Checking File Permissions in Linux with Python
I’m writing a script to check permissions of files in user’s directories and if they’re not acceptable I’ll be warning them, but I want to check permissions of not just the logged in user, but also group and others. How can i do this? It seems to me that os.access() in Python can only …
Python values with units
I need to keep track of units on float and int values in Python, but I don’t want to use an external package like magnitude or others, because I don’t need to perform operations on the values. Instead, all I want is to be able to define floats and ints that have a unit attribute (and I don’t…
Draw bold/italic text with PIL?
How to draw bold/italic text with PIL? ImageFont.truetype(file, size) has an option to specify font size only. Answer Use the bold/italic version of the font