LoginSignup
1
1

More than 5 years have passed since last update.

PaintsChainerをSeleniumで操作しディレクトリ内画像を自動着色

Posted at

ブラウザのオートメーションシステムのちょっとした応用だ。

概要

PaintsChainer -線画自動着色サービス-
をSeleniumで操作し、指定ディレクトリ内の画像をヒント無し着色していきます。

:gear: 検証環境

  • Linux(調べるとよくWindows環境が出てきます)
  • Python 2.7.5
  • selenium (2.53.6)
  • Chrome 59.0.3071.115
  • ChromeDriver 2.30.477691

最初はphantomjsで組んでいたのですが、組み始めた一日後に

日本時間6月27日10:30~6月28日16:00にかけて、一部のブラウザでPaintsChainerの自動着色ができない障害がありました。本件でお問い合わせをいただいたユーザーの方に感謝いたします。現在は復旧しております。

とChrome以外でJSエラーが出る変更が入りまして、Chromeに乗り換えました。
乗り換えなきゃと思ってはいたので不幸中の幸いか。
また、Chromeの安定版が59になったのも乗り換えやすかったです。

コード


# coding:utf-8
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from pyquery import PyQuery as pq
import sys
import glob
import os
import time
import urllib

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

from selenium.webdriver.chrome.options import Options


# https://stackoverflow.com/questions/12093940/reading-files-in-a-particular-order-in-python
import re
numbers = re.compile(r'(\d+)')
def numericalSort(value):
    parts = numbers.split(value)
    parts[1::2] = map(int, parts[1::2])
    return parts


# https://stackoverflow.com/questions/43813170/using-selenium-webdriver-to-wait-the-attribute-of-element-to-change-value
class wait_for_the_attribute_value(object):
    def __init__(self, locator, attribute, value):
        self.locator = locator
        self.attribute = attribute
        self.value = value

    def __call__(self, driver):
        try:
            element_attribute = EC._find_element(driver, self.locator).get_attribute(self.attribute)
            return (element_attribute != self.value) 
        except StaleElementReferenceException:
            return False

if __name__ == '__main__':
    args = sys.argv
    if (len(args) < 2):
        quit('required directory path')

    original_dir = os.path.abspath(args[1])
    color_dir = original_dir + '_color'
    try:
        os.mkdir(color_dir)
    except OSError:
        pass

    try:
        # driver = webdriver.PhantomJS()
        options = Options()
        # options.binary_location = '' /usr/bin/ にバイナリを移動したから不要
        options.add_argument('--headless')
        driver = webdriver.Chrome(chrome_options=options)
        driver.get('https://paintschainer.preferred.tech/index_ja.html')

        # 要らないと思うけど動作確認用のスクリーンショットに邪魔だから規約画面閉じる
        close = driver.find_element_by_css_selector('.close')
        close.click()

        for path in sorted(glob.glob(original_dir + '/*'), key=numericalSort):
            dummy, ext = os.path.splitext(path)
            # TODO 未対応の透過png対策 大文字小文字?
            if (ext != '.jpg' and ext !=  '.jpeg' and ext !=  '.png'):
                continue

            driver.execute_script("document.getElementById('load_line_file').style.display = '';")
            elm = driver.find_element_by_id("load_line_file")
            elm.send_keys(os.path.abspath( path ))
            src = driver.find_element_by_id("output").get_attribute('src')
            WebDriverWait(driver, 60 * 5).until(wait_for_the_attribute_value((By.ID, 'output'), 'src', src))

            output = driver.find_element_by_id("output").get_attribute('src')
            urllib.urlretrieve(output, color_dir + '/' + os.path.basename(path))

            time.sleep(10)

    except TimeoutException as e:
        print(e)
        driver.save_screenshot('./timeout.png')
    except Exception as e:
        print(e)
        driver.save_screenshot('./exception.png')
    finally:
        driver.quit()

未だにきれいに書けない…

感想

PaintsChainer(と画像化したファイル)があればフルカラーの漫画が読める!
と思い立って、通して見た結果。
あまり良くはなかったです。
ぼんやり色は付きますが、フォントが滲んだりします。

本当は「ブラックジャックによろしく」の着色例を載せようと思ってましたが、直前で公開後のメール連絡がめんどうだなと思ってやめました。
断っておきますと、事後連絡とタイトルと著作者名の表示だけすればいいので、本当にゆるくて良いライセンスですよ。
『ブラックジャックによろしく』マンガデータを、二次利用フリーですべての方に公開しています。

漫画ビューアーなどの「擬似四色刷り」系ツールのほうが統一感があっていいと思います。
完成された原稿よりは、失礼を承知で言うと新都社系のような?線画に近い漫画のほうが用途的に適しているかもしれません。

そもそも学習対象が違いますし。
でも漫画のカラーページはモノクロとまた技法が違いそうで、サンプル集めも大変そうですね。
そしてそんなことできる人なら自鯖で立ててわざわざ他所様にフリーライドしないだろうという。

このコードの使用用途がよくわからない。

参考

1
1
0

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
1
1