主にプログラミングに関して。Python, .NET Framework(C#), JavaScript, その他いくらか。
記事にあるサンプルやコードは要検証。使用に際しては責任を負いかねます

スポンサーサイト

                
tags:
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

カラーデザイン補助ツールの改良

                
tags:
より多くの人に向けたGUIデザインのために(基本的なこととツール製作)

 上記の記事で作成したツールを改良した。ただしWindows環境のみでの動作に。
・スクリーンキャプチャに使用するモジュールをPyGTK付属の機能からImageGrabへ
PyGTKだとキャプチャ画像が不安定なため
PILのImageオブジェクトからcairoのsurfaceオブジェクトに変換しても、処理負荷はたいしたことにならない
ImageGrabではキャプチャに要する時間がキャプチャサイズに依存せず、且つわりと速い

#-*- coding: utf-8 -*-

import datetime
import time

import cairo
import gtk
import gobject
import ImageGrab


root = gtk.gdk.get_default_root_window()
XML = """<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="title" translatable="yes">Makestomy</property>
<property name="default_width">440</property>
<property name="default_height">250</property>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="width_request">150</property>
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkHBox" id="hbox2">
<property name="visible">True</property>
<child>
<object class="GtkSpinButton" id="spinbutton1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="adjustment">adjustment1</property>
<property name="climb_rate">1</property>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbutton2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="adjustment">adjustment2</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">13</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkHBox" id="hbox3">
<property name="visible">True</property>
<child>
<object class="GtkSpinButton" id="spinbutton3">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="adjustment">adjustment3</property>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbutton4">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="adjustment">adjustment4</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">13</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">none</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="left_padding">12</property>
<child>
<object class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkRadioButton" id="radiobutton1">
<property name="label" translatable="yes">C</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobutton2">
<property name="label" translatable="yes">P</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">radiobutton1</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobutton3">
<property name="label" translatable="yes">D</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<property name="group">radiobutton1</property>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes">&lt;b&gt;Type&lt;/b&gt;</property>
<property name="use_markup">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkHBox" id="hbox4">
<property name="visible">True</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Play</property>
<property name="width_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Pause</property>
<property name="width_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">Save
Scene</property>
<property name="width_request">50</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">13</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkDrawingArea" id="drawingarea1">
<property name="visible">True</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkAdjustment" id="adjustment1">
<property name="value">1</property>
<property name="upper">32000</property>
<property name="step_increment">1.0099999997764826</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="adjustment2">
<property name="upper">32000</property>
<property name="step_increment">5</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="adjustment3">
<property name="value">10</property>
<property name="upper">32000</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="adjustment4">
<property name="value">10</property>
<property name="upper">32000</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
</interface>
"""

class ShapedWindow:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_string(XML)
self.window = self.builder.get_object("window1")
self.image = self.builder.get_object("drawingarea1")

self.x = self.builder.get_object("spinbutton1")
self.x.set_adjustment(gtk.Adjustment(value=0, lower=0, upper=32000, step_incr=5, page_incr=0, page_size=0))
self.y = self.builder.get_object("spinbutton2")
self.y.set_adjustment(gtk.Adjustment(value=0, lower=0, upper=32000, step_incr=5, page_incr=0, page_size=0))
self.width = self.builder.get_object("spinbutton3")
self.width.set_adjustment(gtk.Adjustment(value=300, lower=10, upper=32000, step_incr=5, page_incr=0, page_size=0))
self.height = self.builder.get_object("spinbutton4")
self.height.set_adjustment(gtk.Adjustment(value=300, lower=10, upper=32000, step_incr=5, page_incr=0, page_size=0))

self.button_c = self.builder.get_object("radiobutton1")
self.button_c.connect("clicked", self.color_change)
self.button_p = self.builder.get_object("radiobutton2")
self.button_p.connect("clicked", self.color_change)
self.button_d = self.builder.get_object("radiobutton3")
self.button_d.connect("clicked", self.color_change)

self.button_play = self.builder.get_object("button1")
self.button_play.connect("clicked", self.play)
self.button_pause = self.builder.get_object("button2")
self.button_pause.connect("clicked", self.pause)
self.button_save = self.builder.get_object("button3")
self.button_save.connect("clicked", self.save)

self.window.connect("destroy", self.close_application)

self.timer = gobject.timeout_add(1000, self.capture)
self.window.show_all()



def capture(self):
t = time.time()
x = self.x.get_value_as_int()
y = self.y.get_value_as_int()
width = self.width.get_value_as_int()
height = self.height.get_value_as_int()
self.image.set_size_request(width, height)
t_cap_start = time.time()
self.screen_cap = ImageGrab.grab((x, y, x + width, y + height))
t_cap = time.time() - t_cap_start
self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, self.screen_cap.size[0], self.screen_cap.size[1])
ia = self.surface.get_data()
label = [r for r in self.button_c.get_group() if r.get_active()][0].get_label()
if label == "C":
ia[0: :4], ia[1: :4], ia[2: :4] =\
self.screen_cap.tostring()[2: :3], self.screen_cap.tostring()[1: :3], self.screen_cap.tostring()[0: :3]
elif label == "P":
ia[0: :4], ia[1: :4] =\
self.screen_cap.tostring()[2: :3], self.screen_cap.tostring()[1: :3]
elif label == "D":
ia[0: :4], ia[2: :4] =\
self.screen_cap.tostring()[2: :3], self.screen_cap.tostring()[0: :3]
cr = self.image.window.cairo_create()
cr.set_source_surface(self.surface, 0, 0)
cr.paint()
print "capture %s[s], whole process %s[s]" %(t_cap, time.time() - t)

return True

def pause(self, widget):
gobject.source_remove(self.timer)
return True

def play(self, widget):
self.timer = gobject.timeout_add(1000, self.capture)
return True

def save(self, widget):
filename = "cap_%s.png" %(datetime.datetime.now().strftime("%Y%m%dT%H%M%S"))
self.surface.write_to_png(filename)
return True

def color_change(self, widget):
self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, self.screen_cap.size[0], self.screen_cap.size[1])
ia = self.surface.get_data()
label = [r for r in self.button_c.get_group() if r.get_active()][0].get_label()
if label == "C":
ia[0: :4], ia[1: :4], ia[2: :4] =\
self.screen_cap.tostring()[2: :3], self.screen_cap.tostring()[1: :3], self.screen_cap.tostring()[0: :3]
elif label == "P":
ia[0: :4], ia[1: :4] =\
self.screen_cap.tostring()[2: :3], self.screen_cap.tostring()[1: :3]
elif label == "D":
ia[0: :4], ia[2: :4] =\
self.screen_cap.tostring()[2: :3], self.screen_cap.tostring()[0: :3]
cr = self.image.window.cairo_create()
cr.set_source_surface(self.surface, 0, 0)
cr.paint()

return True

def close_application(self, widget):
gtk.main_quit()
return False

if __name__ == '__main__':
win = ShapedWindow()
gtk.main()

            

コメントの投稿

非公開コメント

プロフィール

hMatoba

Author:hMatoba
Github

最新記事
リンク
作ったものなど
月別アーカイブ
カテゴリ
タグリスト

検索フォーム
Amazon
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。