Archive
Improving the sidebar of /r/python
In the sidebar of /r/python, there was a very unpythonic infinite loop for a long time:
while 1:
# do something
Today I sent a message to the moderators and they changed it:
Update: I got a message from them.
Unicode box-drawing characters
I wanted to make a logo for my project PrimCom. Since it runs in the command line, I wanted to draw the logo with characters.
You can find a list of box-drawing characters here. I designed the logo on a paper, translated the box characters to normal characters (this way it was easier to type them in), then translated the characters back with a script.
Python source:
#!/usr/bin/env python
# encoding: utf-8
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import sys
chars = {
'a': '┌',
'b': '┐',
'c': '┘',
'd': '└',
'e': '─',
'f': '│',
'g': '┴',
'h': '├',
'i': '┬',
'j': '┤',
'k': '╷',
'l': '┼',
}
logo = """
aeeb aeeb
fabf faec
fdcheiieejf aeeieeb
faejaljkkff fabfkkf
ff fffffffdejdcffff
dc dcdggggeegeegggc
"""
def main():
for c in logo:
if c in chars:
sys.stdout.write(chars[c])
else:
sys.stdout.write(c)
if __name__ == "__main__":
main()
Output:
┌─────┐ ┌─────┐ │ ┌─┐ │ │ ┌───┘ │ └─┘ ├───┬─┬─────┤ │ ┌─────┬─────┐ │ ┌───┤ ┌─┼─┤ ╷ ╷ │ │ │ ┌─┐ │ ╷ ╷ │ │ │ │ │ │ │ │ │ │ └───┤ └─┘ │ │ │ │ └─┘ └─┘ └─┴─┴─┴─┴─────┴─────┴─┴─┴─┘
Update (20140322)
If you want rainbow colors, check out the colout project.
get the command pip3
Problem
You have Python 2 and Python 3 on the same machine. You want to install a package that requires Python 3. You cannot use the command “pip” because it will install the package as if it were written in Python 2. You want a “pip3” command.
Solution #1 (20140912)
sudo apt-get install python3-pip
Solution #2
I found the solution here.
Steps:
$ wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py $ sudo python3 get-pip.py
Now you have a “pip3” command that you can use to install Python 3 libraries.
installing pgmagick
pgmagick is yet another boost.python based wrapper for GraphicsMagick.
“GraphicsMagick is the swiss army knife of image processing. …it provides a robust and efficient collection of tools and libraries which support reading, writing, and manipulating an image in over 88 major formats including important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF.” (source)
Here I found an interesting blog post on how to remove image backgrounds with a Python script (comments on it here). The script uses the pgmagick library.
Problem
How to install pgmagick and GraphicsMagick? That is, the following line shouldn’t drop any error :)
>>> import pgmagick >>>
Solution
It may not be an optimal solution because it installed on my machine LOTS OF extra packages… However, it worked for me.
$ sudo add-apt-repository ppa:dhor/myway $ sudo apt-get update $ sudo apt-get install graphicsmagick $ sudo apt-get install libmagick++-dev $ sudo apt-get install libboost-python-dev $ sudo pip install pgmagick -U
Usage example
scale example (copied from here):
>>> from pgmagick import Image, FilterTypes
>>> im = Image('input.jpg')
>>> im.quality(100)
>>> im.filterType(FilterTypes.SincFilter)
>>> im.scale('100x100')
>>> im.sharpen(1.0)
>>> im.write('output.jpg')
Links


You must be logged in to post a comment.