Getting average image color from Python

It's quite common to see web pages that show a solid color in place of images, while resources are being loaded. The color is usually averaged from the original image, as to give it a more "consistent" feeling while the page loads.

A simple way to do this from the command line is by using ImageMagick to resize the image to 1x1 size, then get the color of that one pixel:

% convert pic.jpg -resize '1x1!' txt:-
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (16818.3,27417.8,39342.8)  #416B99  srgb(65,107,153)

(The image ...

mypy: function accepting type, returning instance

I recently ended up with a function that looks a bit like this (most of the logic was removed for sake of example):

def build(record, **kwargs):
    # Pretend we're doing stuff with the **kwargs here..
    return record(**kwargs)

Where record could be one of several classes. All we care about is, the function accepts a type as first argument, and returns an instance of that type.

The problem

At first, the most logical way to describe this to mypy was to use a TypeVar like this:

from typing import Any, TypeVar, Type

T = TypeVar('T')

def build(record: Type ...

Python3 development with Emacs

I am currently working on a mix of Python2/Python3 projects, and as such, I need to find a way to customize a few things on a per-project basis. This post contains notes I'm going to take along the path.

Per-directory variables

Using .dir-locals.el, you can customize things on a per-directory basis. Changes will affect all files in the directory containing this file, and all its subdirectories.

Note: you can use the M-x add-dir-local-variable command to quickly setup dir-locals. You'll be prompted to provide all the required information.

Custom flake8 executable

Set the flycheck-python-flake8-executable variable to the ...

Even better testing, with py.test + tox + travis

I recently wrote a post about using py.test and tox. Now I improved the technique a bit, so here it is a new post with all the recent improvements.

(Please refer to that post for the problems I had to solve, and reasons behind the need of this kind of customizations).

The new test class

In order to properly support Python3, I'm using this test class in my

from setuptools.command.test import test as TestCommand

class PyTest(TestCommand):
    test_package_name = 'MyMainPackage'

    def finalize_options(self):
        _test_args = [
            '--cov={0}'.format(self.test_package_name ...

Using Apache Tika from Python, with jnius

I needed a library to extract metadata and plaintext transcript from various file formats, for indexing purposes.

After looking around for a while, I found out that Apache Tika might be the right tool for the job (or, at least, it does quite a good job in extracting information from files).

Sadly though, that thing is written in Java.

At first, I tried it by running the jar via subprocess and then parsing the json output. I quickly discarded that approach, as:

  • It required to launch the process twice (once for extracting metadata, and once to extract the plain-text version ...

Page 1 / 2 »