ぱたへね

はてなダイアリーはrustの色分けができないのでこっちに来た

Gauche-CVで画像のコピーアンドペースト

Gauche-CLを使って画像の一部をコピーアンドペーストする方法です。
元ネタはProgramming Computer Vision With Pythonから。
Gauche-CVはaharisuのごみ箱で公開されています。

Python PIL でコピーアンドペースト

画像の真ん中で切り取るとちょうど首の所が切れて恐ろしいことになったので、端の方を切り取って回転させています。

# -*- coding: utf-8 -*-
__author__ = 'Natsutani'

from PIL import Image

def make_thumbnail(infile, outfile):
    try:
        im = Image.open(infile)
        box = (10,300,120,450)
        # コピー
        region = im.crop(box)
        # 回転
        region = region.transpose(Image.ROTATE_180)
        # ペースト
        im.paste(region,box)
        im.save(outfile)
    except Exception as ex:
        print(repr(ex))
        print('copy and paste failed %s' % infile)

def main():
    make_thumbnail('twittan.jpg', 'twittan_cc.jpg')

if __name__ == "__main__":
    main()

Gauche-CV でコピーアンドペースト

同じ処理をGauche-CVで。

(use cv)

(let* ((src (cv-load-image "twittan.jpg"))
       (dst (cv-clone-image src))
       (region_x 10)
       (region_y 300)
       (region_h 150)
       (region_w 110)
       (region (make-image region_w region_h  (ref src 'depth) (ref src 'n-channels)))
       (box (make-cv-rect region_x region_y region_w region_h)))
  ; 領域をコピーして反転
  (slot-set! src 'roi box)
  (cv-copy src region)
  (cv-flip region region -1)
  ; コピー先にroiを設定し、コピー後元に戻す。
  (let ((roi-save (slot-ref dst 'roi)))
    (slot-set! dst 'roi box)
    (cv-copy region dst)
    (slot-set! dst 'roi roi-save))
  
  (cv-save-image "twittan_cc.jpg" dst))

処理結果