Add x-y vertex coordinates to empty numpy.ndarray

頂点座標を空のnumpy.ndarrayに追加する操作。
よくやるのでメモ。

—(sample code)—–

import cv2
import numpy as np

## cat pol_int   (x-y point list)
## -126 133
## -126 131
## -117 131
## -117 134


in_data = open("./plist")
lines = in_data.readlines()

arr = np.empty((0,2), int)

for i in range(len(lines)):

  lines[i].strip()
  jj = lines[i].split()

  xx = int(jj[0])
  yy = int(jj[1])
  arr = np.append(arr, np.array([[xx, yy]]), axis=0)

  i = i + 1

print(arr)

—(exec result)—–
[[-126 133]
[-126 131]
[-117 131]
[-117 134]]

—(memo)—-
initialize ndarray for x-y point list.
at first array is empty(no entry) and add_type is [x,y].
so initialize parameter is (0,2).

arr = np.empty((0,2), int)

if append for ndarray, append element must ndarray.
we use only 1 axis, so axis = 0(axis memo)

arr = np.append(arr, np.array([[xx, yy]]), axis=0)

phantomjs を使い、wordpress へ記事を投稿するスクリプト

var page = new WebPage(), testindex = 0, loadInProgress = false;

page.onConsoleMessage = function(msg)
{
        console.log(msg);
};

page.onLoadStarted = function()
{
        loadInProgress = true;
};

page.onLoadFinished = function()
{
        loadInProgress = false;
};
var steps = [
        function() {
                page.open("http://hogehoge_wordpress.com/wp-login.php");
        },
        function() {
                page.evaluate(function(){
                        document.getElementById("user_login").value="<user id>";
                        document.getElementById("user_pass").value="<user pass>";
                        document.querySelector('*[name="wp-submit"]').click();
                });
        },
        function() {
                page.open('http://hogehoge_wordpress.com/wp-admin/post-new.php');
        },
        function() {
                page.evaluate(function() {
                document.querySelector('*[name="post_title"]').value='title';
                document.querySelector('*[name="content"]').value='contents';
                document.querySelector("#publish").click;
                var a = document.querySelectorAll("#publish");
                var e = document.createEvent('MouseEvents');
                e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                a[0].dispatchEvent(e);
                waitforload = true;
                });
        },
        function() {
                page.render('images/LastLoad.png');
        }

];

interval = setInterval(function()
{
        if (!loadInProgress && typeof steps[testindex] == "function")
        {
                console.log("step " + (testindex + 1));
                steps[testindex]();
                page.render("images/step" + (testindex + 1) + ".png");
                testindex++;
        }
        if (typeof steps[testindex] != "function")
        {
                                page.render('images/finalxx.png');
                console.log("Scrapping complete...!");
                phantom.exit();
        }
}, 5000);// JavaScript Document

Script sample to posting to wordpress by phantomjs.

pythonでmechanizeを使う

まずはmechanizeのインストール

pip install mechanize

ヤフーページにアクセスしてHTMLのソースを取得

#! /usr/bin/python

import mechanize
web = mechanize.Browser()
hh  = web.open( "http://yahoo.co.jp" )
print hh.read()

perl で Mechanize を使い、wordpress へ記事を投稿するスクリプト

wordpressに対して自動で記事を投稿するスクリプト。perlからMechanizeを使いWEBアクセスをエミュレートして書き込むやり方。何度か実行する機会があったのでここにメモ。

#! /usr/bin/perl

use FindBin qw($Bin);
use WWW::Mechanize;
use utf8;


$URI = 'http://your_wordpress_url/wp-admin/';
$agent = 'WWW::Mechanize'->new();
$agent->get("$URI");

$agent->submit_form(
        form_name => 'loginform',
        fields => {
                log => 'YOUR ID',
                pwd => 'YOUR PASSWORD',
        },
);
eval {
  $URI = $URI."post-new.php";
  $agent->get("$URI");
  $data = $agent->content;
  $agent->submit_form(
        form_name => 'post',
        fields => {
          'post_title' => 'post title',
          'content'    => 'post contents',
        },
        button => 'publish'
  );
};
if ($@) {
  print "err\n";
  exit 1;
};

exit 0;

Script sample to posting to wordpress by perl and mechanize.