2020年1月1日水曜日

OpenCVでスクリーントーンを作成する

漫画は主に黒と白の二色で表現するため、細かい色の濃淡を表現するためにスクリーントーンを切り貼りする。今回、ドット柄のスクリーントーンを OpenCV で作成してみる。

まずスクリーントーンの大きさの白紙を作る。ここでは縦 size_y、横 size_x とする。
import numpy as np

img = np.full((size_y, size_x), 255, np.uint8)
interval をドットとドットの間の間隔、radius をドットの半径とする。起点となる左上のドットの中心を (interval+radius, interval+radius) としたとき、ドット柄は以下のように作成することができる。
import cv2

tmp_y = interval+radius
while(tmp_y < size_y):
    tmp_x = interval+radius
    while(tmp_x < size_x):
        cv2.circle(img, (tmp_x, tmp_y), radius, (0, 0, 0), -1)
        tmp_x += interval+2*radius
    tmp_y += interval+2*radius

2018年1月6日土曜日

テーブル定義を変更せずに model を修正する

通常、model を定義し、その定義に基づき DB のテーブルを生成する。
しかし、何らかの理由により、テーブル定義を変えずに model を変更したい場合がある。

この場合、migrate コマンドに fake オプションを付けて実行すればよい。
> python ./manage.py makemigrations
> python ./manage.py migrate --fake

2018年1月3日水曜日

django-tables2 の SingleTableView を利用してみる

前回は TemplateView で django-tables2 を利用した。
同様の機能として django-tables2 の SingleTableMixin を利用することができる。

以下の例では、Actor model のデータを ActorTable table の構造で表示する。なお、table_pagination は RequestConfig の paginate パラメータへ与える辞書を与えることができる。
from django_tables2 import SingleTableView
class ActorSingleTableView(SingleTableView):
    model = Actor
    table_class = ActorTable
    table_pagination = {"per_page": 20}
    template_name = "actorlistview.html"
このテーブルを表示する際、デフォルトのテンプレート変数は table になる。
テンプレート引数を他の変数に変更したい場合は、context_table_name を設定する。
from django_tables2 import SingleTableView
class ActorSingleTableView(SingleTableView):
    model = Actor
    table_class = ActorTable
    context_table_name = "actors_table"     # テンプレート変数が actors_table に変更される。
    table_pagination = {"per_page": 20}
    template_name = "actorlistview.html"
上記例の場合、Actor に格納されたデータがそのまま ActorTable の形式で表示される。
クエリを変更したい場合、いくつかの方法がある。

まず、queryset を設定する方法である。以下の例では、表示順を last_name, first_name の順にしている。
from django_tables2 import SingleTableView
class ActorSingleTableView(SingleTableView):
    queryset = Actor.objects.order_by("last_name", "first_name")
    table_class = ActorTable
    table_pagination = {"per_page": 20}
    template_name = "actorlistview.html"
get_queryset() により設定することもできる。
from django_tables2 import SingleTableView
class ActorSingleTableView(SingleTableView):
    table_class = ActorTable
    table_pagination = {"per_page": 20}
    template_name = "actorlistview.html"

    def get_queryset(self):
        return Actor.objects.order_by("last_name", "first_name")
また、同様に get_table_data() により設定することができる。
from django_tables2 import SingleTableView
class ActorSingleTableView(SingleTableView):
    model = Actor
    table_class = ActorTable
    table_pagination = {"per_page": 20}
    template_name = "actorlistview.html"

    def get_table_data(self):
        return Actor.objects.order_by("last_name", "first_name")

2017年10月12日木曜日

画像の三原色をヒストグラムで表示する

に記述した通り、カラー画像の場合、cv2.imread 関数の戻り値は、青・緑・赤の色成分の配列となる。
> import cv2
> img = cv2.imread(IMG_FILE)
> print(img)

[[[26 34 71]
  [27 35 72]
  [27 35 72]
  ..., 
  [31 29 59]
  [37 35 64]
  [48 45 71]]

 [[24 33 67]
  [24 33 67]
  [24 33 67]
  ..., 
  [30 28 58]
  [30 28 57]
  [38 35 61]]

 ..., 
 [[26 19 54]
  [26 18 55]
  [24 16 56]
  ..., 
  [21 11 27]
  [21 10 30]
  [20  8 30]]]
最も内側のリストは、1ピクセルの青・緑・赤の要素を表している。上の例の場合、最初の1ピクセルは、青=26, 緑=34, 赤=71 で色が作られている。
さて、このリスト形式から各色のリストを作る。
> b = img[:, :, 0]
> g = img[:, :, 1]
> r = img[:, :, 2]
このデータに対してヒストグラムを表示する。
> from matplotlib import pyplot as plt
> plt.hist([r.ravel(), g.ravel(), b.ravel()], 10, [0, 256], alpha=0.3,
           color=["R", "G", "B"], stacked=False)
上側の画像に対して、作成したヒストグラムが下側の図になる。

2017年9月20日水曜日

グラフのタイトル・ラベル・補助線を表示する

前回のグラフにタイトルやラベルを表示する。タイトルは matplotlib.pyplot.title関数、ラベルは matplotlib.pyplot.xlabel 関数(x軸のラベル)、matplotlib.pyplot.xlabel 関数(y軸のラベル)を使用する。
なお、表示データは前回のものを使用する。
import matplotlib.pyplot as plt

plt.title("The Population of Italy")
plt.xlabel("YEAR")
plt.ylabel("Thousand unit", fontproperties=fp)
plt.plot(x, y)
plt.show()
また、補助線を表示したい場合は、matplotlib.pyplot.grid関数の引数に True を与えればよい。
plt.grid(True)