昨日及び今朝の追記で GdkPixbuf の扱い方は解った。
今度は保存、無意味に勉強してもツマランので Gedit Plugin にする。
メニューで選択すると GtkFileChooserDialog を出す。
画像を選択して OK すると 300*300 以下の縮小画像を画像位置に自動生成する。
それをリンク画像にし元画像へのリンク文字列を相対パスでカーソル位置に流し込む。
つまり blog 機能のようなモンを作って自分が楽したいので。
仕様として JPEG, PNG 限定でいいだろう。
圧縮レベルは Gimp デフォルトに合わせ JPEG:85, PNG:9 固定。
ダイアログを出して縮小サイズ指定や圧縮率変更も可能だけどパス。
保存されていない場合は相対パスが得られないから弾く。
メニューの挿入位置は「ファイル(f)」メニューに入れたほうが迷わないと思う。
こんなもんかな。
gdk_pixbuf_save 関数があるはずだがドコにも見つからなかった。
しかたがないので Pixbuf オブジェクトの savev メソッドを使う、多分大丈夫。
scale_simple メソッドは GdkPixbuf.Pixbuf 内にあった。
GTK2 の時と使い方はあまり変わらないようだ。
savev メソッドの第三及び第四引数は何故かリストにする必要があった。
文字列にすると WARNING が出るし適用されないしで困ったよ。
os.path.relpath を初めて使ったけどコレって基準はディレクトリ名なんだね。
アスペクト比の計算は以前作ったというか動画プレイヤーで散々やったしコピペでいい。
何か作りつづけていれば後々で何かに流用できる。
後はとにかく GTK+3.0 の仕様に合わせてひたすら書く。
半分以上が dir() でメソッドや列挙体名を探しまくる時間だったのは秘密だよ。
結構外国からのアクセスがあるので相変わらず全部英語でコメント。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #-*- coding:utf-8 -*- # Gedit a_href_picture plugin version 3.0.0 # Copyright © 2011 sasakima-nao <m6579ne998z@gmail.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. from gi.repository import GObject, Gedit, Gtk, GdkPixbuf import os ui_str = """<ui> <menubar name="MenuBar"> <menu name="FileMenu" action="File"> <placeholder name="FileOps_1"> <menuitem name="AHrefPicture" action="AHrefPicture"/> </placeholder> </menu> </menubar> </ui> """ class AHrefPicturePlugin(GObject.Object, Gedit.WindowActivatable): """ Create small image and link insert (e.g. <a href="img/pic1.jpg"><img src="img/pic1-300x225.jpg" alt="img/pic1.jpg" /></a>) This Plugin Gedit 3 only """ __gtype_name__ = "AHrefPicturePlugin" window = GObject. property ( type = Gedit.Window) def __init__( self ): GObject.Object.__init__( self ) def do_activate( self ): manager = self .window.get_ui_manager() self ._action_group = Gtk.ActionGroup( "AHrefPicturePluginActions" ) # GtkActionEntry # name, stock_id, label, accelerator, tooltip, callback actions = [( "AHrefPicture" , None , "a href Picture" , None , "a href Picture" , self .on_a_href_activate)] self ._action_group.add_actions(actions) manager.insert_action_group( self ._action_group, - 1 ) self ._ui_id = manager.add_ui_from_string(ui_str) def do_deactivate( self ): manager = self .window.get_ui_manager() manager.remove_ui( self ._ui_id) manager.remove_action_group( self ._action_group) manager.ensure_update() def do_update_state( self ): self ._action_group.set_sensitive( self .window.get_active_document() ! = None ) def on_a_href_activate( self , action, data = None ): doc = self .window.get_active_document() viewpath = doc.get_uri_for_display() if viewpath[ 0 ] ! = "/" : self .messagebox( "Please save the file" ) return dlg = Gtk.FileChooserDialog( "Select Picture" , self .window, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK) ) dlg.set_current_folder(os.path.dirname(viewpath)) r = dlg.run() if r = = Gtk.ResponseType.OK: try : filepath = dlg.get_filename() pixbuf = GdkPixbuf.Pixbuf.new_from_file(filepath) except : dlg.destroy() return # create under 300*300 px image d_width = 300 d_height = 300 p_width = pixbuf.get_width() p_height = pixbuf.get_height() width = 0 height = 0 if (d_width * p_height) > (d_height * p_width): width = p_width * d_height / p_height height = d_height else : width = d_width height = p_height * d_width / p_width smallpix = GdkPixbuf.Pixbuf.scale_simple(pixbuf, width, height, GdkPixbuf.InterpType.BILINEAR) # jpeg or png name, ext = os.path.splitext(filepath) ext = ext.lower() if ext = = ".jpg" or ext = = ".jpeg" : smallpath = "{0}-{1}x{2}.jpg" . format (name, width, height) smallpix.savev(smallpath, "jpeg" , [ "quality" ], [ "85" ]) elif ext = = ".png" : smallpath = "{0}-{1}x{2}.png" . format (name, width, height) smallpix.savev(smallpath, "png" , [ "compression" ], [ "9" ]) else : self .messagebox( "Extension not found" ) return # create image link text dpath = os.path.dirname(viewpath) path1 = os.path.relpath(filepath, dpath) path2 = os.path.relpath(smallpath, dpath) result = '<a href="{0}"><img src="{1}" alt="{0}" /></a>' . format (path1, path2) # insert text view = self .window.get_active_view() buf = view.get_buffer() buf.insert_at_cursor(result) dlg.destroy() def messagebox( self , text): dlg = Gtk.MessageDialog( self .window, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.OK, text) r = dlg.run() dlg.destroy() |
という感じに仕上がった。
Plugin は本サイトで公開しているのでコッチはコードの参考に。