0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ImageMagick | Python 2 (v2.6.6) > ファイル名から日時を取得し、画像に日時ラベル追加 > label_image_171005.py v0.1 > subprocess.callにて項目の一つに空白がある場合は、それを避けて.split()する

Last updated at Posted at 2017-10-05
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

関連

Python > regex | slice > AAA_20170228_030000_BBB.png というstringから 2017/02/28 03:00:00 というstringリストの取得 > 使わないグループは()をはずしていい | re.search() | join()+for | re.sub()での文字置換

処理内容

  • ファイル名
    • 例: AAA_20170228_030000_BBB.png
  • 画像に2017/02/28 03:00というラベルを追加する。

参考

ImageMagick 画像中に文字を入れる

code v0.1

label_image_171005.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import re
import sys
import subprocess as sb

# on Python 2.6.6
#    ImageMagick 6.7.2-7
# coding rule: PEP8

# v0.1 Oct. 5, 2017
#   - execute convert command to add label to the image
#   - add get_datetime_label()
#   - add debug_make_dummy()


def debug_make_dummy():
    acmd = "convert -size 430x360 xc:white AAA_20170228_030000_BBB.png"
    sb.call(acmd.split())


def get_datetime_label(filename):
    # input(filename): e.g. AAA_20170228_030000_BBB.png
    #
    date_hhnn = re.sub(r'.*_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})_.*',
                       r'\1/\2/\3 \4:\5',
                       filename)
    return date_hhnn

# @spec
# convert -pointsize 20 -annotate +80+50 '2017/02/28 03:00'
#   AAA_20170228_030000_BBB.png out_labeled.png

SEPARATOR = "="  # prevent separation by " " of the date_hhnn

IMG_CMD = "convert"
PARAM = "-pointsize 20 -annotate +80+50"
aparam = PARAM.replace(" ", SEPARATOR)
TEXT = "WHITE"
SRC = "AAA_20170228_030000_BBB.png"
DST = "out_labeled.png"

# debug
debug_make_dummy()
#

albl = get_datetime_label(SRC)
print(albl)

acmd = "=".join([IMG_CMD, aparam, albl, SRC, DST])
cmdlist = acmd.split(SEPARATOR)  # prevent separation by " " of the date_time
print(cmdlist)

sb.call(cmdlist)
run
2017/02/28 03:00
['convert', '-pointsize', '20', '-annotate', '+80+50', '2017/02/28 03:00', 'AAA_20170228_030000_BBB.png', 'out_labeled.png']

out_labeled.pngが生成される。

生成画像

out_labeled.png

はまった点

sb.call(cmdlist)時にsplit()とすると「2017/02/28 03:00」文字列中の空白も分離対象となった。
"="をセパレータにすることで対処した。すっきりした方法ではない。

教えていただいた項目

@shiracamus さんのコメントですっきりした方法を教えていただきました。

情報感謝です。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?