[PYTHON] Operate Paints Chainer with Selenium to automatically color images in the directory

It's a small application of a browser automation system.

</ i> Overview

PaintsChainer -Line art automatic coloring service- Operate with Selenium to color the images in the specified directory without hints.

</ i>: gear: Verification environment

  • Linux (Windows environment often comes out when I check it)
  • Python 2.7.5
  • selenium (2.53.6)
  • Chrome 59.0.3071.115
  • ChromeDriver 2.30.477691

At first I was building with phantomjs, but one day after I started building

From 10:30 on June 27th to 16:00 on June 28th, Japan time, there was a problem that Paints Chainer could not be automatically colored in some browsers. We would like to thank the users who made inquiries regarding this matter. Currently, we have restored.

I changed to Chrome because there was a change that caused a JS error other than Chrome. I thought I had to change trains, so I'm afraid I'm unhappy. Also, it was easy to switch to the stable version of Chrome which became 59.

</ i> code


# 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/Not needed because I moved the binaries to
        options.add_argument('--headless')
        driver = webdriver.Chrome(chrome_options=options)
        driver.get('https://paintschainer.preferred.tech/index_ja.html')

        #I don't think it's necessary, but it interferes with the screenshot for checking the operation, so close the contract screen
        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)
            #Transparent png measures that do not support TODO Uppercase and lowercase?
            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()

I still can't write cleanly ...

</ i> Impressions

With PaintsChainer (and the imaged file) you can read full-color manga! The result of looking through it. It wasn't very good. The color is vague, but the font is blurred.

Actually, I was thinking of posting a coloring example of "Say Hello to Blackjack", but I stopped because I thought it was troublesome to contact me by e-mail just before the release. If you refuse, it's a really loose and good license because you only have to contact after the fact and display the title and author's name. "Say Hello to Blackjack" manga data is open to everyone for free for secondary use.

I think that "pseudo four-color printing" tools such as manga viewers have a more unified feeling. Rather than the completed manuscript, if you are rude, it's more like a Shintosha system? A cartoon that is closer to a line art may be more suitable for use.

The learning target is different in the first place. However, the color page of the manga seems to have a different technique from monochrome, and it seems difficult to collect samples. And if you can do that, you wouldn't bother to stand up and freeride elsewhere.

I'm not sure what this code is used for.

</ i> Reference

Prohibitions Humans should use a standard web browser to send more request messages to our managed servers in a similar amount of time than can reasonably generate in a given amount of time. Acts that interfere with the server or network system of this service in any way.

  • [Okazaki Municipal Central Library Case-Wikipedia]

[Okazaki Municipal Central Library Case-Wikipedia]: https://ja.wikipedia.org/wiki/%E5%B2%A1%E5%B4%8E%E5%B8%82%E7%AB%8B%E4%B8 % AD% E5% A4% AE% E5% 9B% B3% E6% 9B% B8% E9% A4% A8% E4% BA% 8B% E4% BB% B6 [Getting started - ChromeDriver - WebDriver for Chrome]:https://sites.google.com/a/chromium.org/chromedriver/getting-started [Install Chrome on CentOS 7-Qiita]: http://qiita.com/shadowhat/items/af6b973df43d75abfe8e [I tried using Headless Chrome from Selenium --Qiita]: http://qiita.com/orangain/items/db4594113c04e8801aad

[Freeze with send_keys of file selection when running Selenium WebDriver in Python [PhantomJS] --Qiita]: http://qiita.com/khsk/items/d017191905db99a94ffe

Recommended Posts