Home | Trees | Indices | Help |
|
---|
|
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 # Copyright (C) 2011 ~ 2012 Deepin, Inc. 5 # 2011 ~ 2012 Wang Yong 6 # 7 # Author: Wang Yong <lazycat.manatee@gmail.com> 8 # Maintainer: Wang Yong <lazycat.manatee@gmail.com> 9 # 10 # This program is free software: you can redistribute it and/or modify 11 # it under the terms of the GNU General Public License as published by 12 # the Free Software Foundation, either version 3 of the License, or 13 # any later version. 14 # 15 # This program is distributed in the hope that it will be useful, 16 # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 # GNU General Public License for more details. 19 # 20 # You should have received a copy of the GNU General Public License 21 # along with this program. If not, see <http://www.gnu.org/licenses/>. 22 23 from PIL import Image 24 from constant import SHADE_SIZE 25 from draw import draw_pixbuf, draw_vlinear, draw_hlinear 26 from utils import propagate_expose, color_hex_to_cairo, find_similar_color 27 import gtk 28 import scipy 29 import scipy.cluster 30 import scipy.misc 31 import urllib 32 33 __all__ = ["get_dominant_color"] 3436 ''' 37 Parse image and return dominant color in image. 38 39 @param image_path: Image path to parse. 40 @return: Return dominant color, format as hexadecimal number. 41 ''' 42 # print 'reading image' 43 im = Image.open(image_path) 44 im = im.resize((150, 150)) # optional, to reduce time 45 ar = scipy.misc.fromimage(im) 46 shape = ar.shape 47 ar = ar.reshape(scipy.product(shape[:2]), shape[2]) 48 49 # print 'finding clusters' 50 NUM_CLUSTERS = 5 51 codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) 52 # print 'cluster centres:\n', codes 53 54 vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes 55 counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences 56 57 index_max = scipy.argmax(counts) # find most frequent 58 peak = codes[index_max] 59 colour = ''.join(chr(c) for c in peak).encode('hex') 60 # print 'most frequent is %s (#%s)' % (peak, colour) 61 62 return "#%s" % (colour[0:6])6365 ''' 66 Widget to test function get_dominant_color. 67 ''' 68138 139 if __name__ == '__main__': 140 window = gtk.Window() 141 window.set_title("从文件管理器拖动图片测试主色") 142 window.connect("destroy", lambda w: gtk.main_quit()) 143 window.maximize() 144 145 test = ColorTestWidget() 146 test.set_pixbuf("/data/Picture/壁纸/1713311.jpg") 147 window.add(test) 148 149 window.show_all() 150 151 gtk.main() 15270 ''' 71 Initialize ColorTestWidget class. 72 ''' 73 gtk.DrawingArea.__init__(self) 74 self.add_events(gtk.gdk.ALL_EVENTS_MASK) 75 self.connect("expose-event", self.color_test_widget_expose) 76 self.pixbuf = None 77 self.background_color = "#000000" 78 79 self.drag_dest_set( 80 gtk.DEST_DEFAULT_MOTION | 81 gtk.DEST_DEFAULT_HIGHLIGHT | 82 gtk.DEST_DEFAULT_DROP, 83 [("text/uri-list", 0, 1)], 84 gtk.gdk.ACTION_COPY) 85 86 self.connect("drag-data-received", self.color_test_widget_drag)87 9092 self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(filename, 800, 800) 93 print filename 94 self.background_color = get_dominant_color(filename) 95 self.queue_draw()9698 cr = widget.window.cairo_create() 99 rect = widget.allocation 100 101 # Draw pixbuf. 102 draw_pixbuf(cr, self.pixbuf, 0, 0) 103 104 # Draw mask. 105 draw_vlinear( 106 cr, rect.x, rect.y + self.pixbuf.get_height() - SHADE_SIZE, rect.width, SHADE_SIZE, 107 [(0, (self.background_color, 0)), 108 (1, (self.background_color, 1))]) 109 110 draw_hlinear( 111 cr, rect.x + self.pixbuf.get_width() - SHADE_SIZE, rect.y, SHADE_SIZE, rect.height, 112 [(0, (self.background_color, 0)), 113 (1, (self.background_color, 1))]) 114 115 # Draw background. 116 (similar_color_name, similar_color_value) = find_similar_color(self.background_color) 117 print (similar_color_name, self.background_color, similar_color_value) 118 cr.set_source_rgb(*color_hex_to_cairo(similar_color_value)) 119 cr.rectangle(rect.x + self.pixbuf.get_width(), rect.y, 120 rect.width - self.pixbuf.get_width(), rect.height) 121 cr.fill() 122 cr.set_source_rgb(*color_hex_to_cairo(self.background_color)) 123 cr.rectangle(rect.x, rect.y + self.pixbuf.get_height(), 124 rect.width, rect.height - self.pixbuf.get_height()) 125 cr.fill() 126 127 # cr.set_source_rgb(*color_hex_to_cairo(self.background_color)) 128 # cr.rectangle(rect.x + self.pixbuf.get_width(), rect.y, 129 # rect.width - self.pixbuf.get_width(), rect.height) 130 # cr.rectangle(rect.x, rect.y + self.pixbuf.get_height(), 131 # rect.width, rect.height - self.pixbuf.get_height()) 132 133 # cr.fill() 134 135 propagate_expose(widget, event) 136 137 return True
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Wed Aug 8 13:17:38 2012 | http://epydoc.sourceforge.net |