File: graphics.py

package info (click to toggle)
ocrfeeder 0.6.6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,616 kB
  • ctags: 2,088
  • sloc: python: 16,603; makefile: 52
file content (174 lines) | stat: -rw-r--r-- 6,390 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-

###########################################################################
#    OCRFeeder - The complete OCR suite
#    Copyright (C) 2009 Joaquim Rocha
# 
#    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 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
###########################################################################

from lib import getStandardDeviation
from ocrfeeder.util.lib import debug
import Image
import gtk
import math

def getContainerRectangle(points_list):
    points_list = list(points_list)
    points_list.sort()
    leftmost_x = (points_list[0])[0]
    rightmost_x = (points_list[-1])[0]
    i = 0
    while i < len(points_list):
        current_point = points_list[i]
        points_list[i] = (current_point[1], current_point[0])
        i += 1
    points_list.sort()
    highest_y = (points_list[0])[0]
    lowest_y = (points_list[-1])[0]
    return leftmost_x, highest_y, rightmost_x, lowest_y

def getBoundsFromStartEndPoints(start_point, end_point):
    start_x, start_y = start_point
    end_x, end_y = end_point
    width = end_x - start_x
    height = end_y - start_y
    if width < 0:
        start_x += width
        width = abs(width)
    if height < 0:
        start_y += height
        height = abs(height)
    return start_x, start_y, width, height

def convertPixbufToImage(pixbuf):
    assert(pixbuf.get_colorspace() == gtk.gdk.COLORSPACE_RGB)
    dimensions = pixbuf.get_width(), pixbuf.get_height()
    stride = pixbuf.get_rowstride()
    pixels = pixbuf.get_pixels()
    mode = pixbuf.get_has_alpha() and "RGBA" or "RGB"
    return Image.frombuffer(mode, dimensions, pixels,
                            "raw", mode, stride, 1)
    width, height = pixbuf.get_width(), pixbuf.get_height()
    return Image.fromstring("RGBA", (width, height), pixbuf.get_pixels())

def rgbaToInteger(rgba):
    r, g, b, a = rgba
    return (r << 24) | (g << 16) | (b << 8) | a

def colorsContrast(color1, color2, tolerance = 120):
    return abs(color1 - color2) > tolerance

def getImageResolution(image_object):
    image_object
    resolution = (300, 300)
    if 'dpi' in image_object.info.keys():
        resolution = image_object.info['dpi']
    return resolution

def getImageResolutionFromPath(image_path):
    image = Image.open(image_path)
    return getImageResolution(image)

def getImagePrintSizeFromPath(image_path):
    image_object = Image.open(image_path)
    return getImagePrintSize(image_object)

def getImagePrintSize(image_object):
    resolution = getImageResolution(image_object)
    x_resolution, y_resolution = resolution
    width, height = image_object.size
    return ( float(width) / float(x_resolution), float(height) / float(y_resolution) )

def getTextSizeFromImage(image):
    width, height = image.size
    colors = image.getcolors(width * height)
    background_color = 255
    if colors:
        colors.sort()
        background_color = (list(colors[-1][1])).pop()
    text_sizes = [0]
    for i in xrange(getTextBeginHeight(image), height):
        current_line = image.crop((0, i - 1, width, i))
        current_line.convert('L')
        blank_line = True
        for i in range(0, current_line.size[0], 3):
            color = current_line.getpixel((i, 0))
            if colorsContrast(list(color).pop(), background_color):
                blank_line = False
                break
        if blank_line:
            if text_sizes[-1]:
                text_sizes.append(0)
        else:
            text_sizes[-1] += 1
    text_sizes.sort()
    text_sizes = [i for i in text_sizes if i != 0]
    text_size = 0
    if text_sizes:
        text_sizes_avg = sum(text_sizes) / len(text_sizes)
        for i in text_sizes:
            if i > text_sizes_avg:
                text_size = math.floor(i)
                break
        text_size = max(text_sizes)
    debug('Text Size: ', text_size)
    return text_size

def getTextBeginHeight(image):
    width, height = image.size
    for i in range(1, height, 1):
        current_line = image.crop((0, i - 1, width, i))
        colors = current_line.getcolors()
        foreground_color = [0, 0, 0]
        if colors:
            foreground_color = [0 for value in list((colors[0][1])[:-1])]
        if colors:
            for color in colors:
                if list(color[1])[:-1] == foreground_color:
                    return i
    return -1

def getHorizontalAngleForText(image):
    width, height = image.size
    longest_axis = math.sqrt(width ** 2 + height ** 2)
    collage = Image.new('RGBA', (longest_axis, longest_axis), 'white')
    test_image = image.copy().convert('RGBA')
    y_to_paste = (longest_axis - test_image.size[1]) / 2
    x_to_paste = (longest_axis - test_image.size[0]) / 2
    original_paste = collage.copy()
    original_paste.paste(test_image, (x_to_paste, y_to_paste), test_image)
    previous_text_begin = getTextBeginHeight(original_paste)
    paste_image = original_paste.copy()
    angle = 0
    while angle > -360:
        current_text_begin = getTextBeginHeight(paste_image)
        if previous_text_begin > current_text_begin or current_text_begin == -1:
            break
        previous_text_begin = getTextBeginHeight(paste_image)
        angle -=5
        paste_image = original_paste.rotate(angle)
        collage = Image.new('RGBA', paste_image.size, 'white')
        collage.paste(paste_image, (0,0), paste_image)
        paste_image = collage
    if angle:
        return angle + 5
    return angle

def getImageRotated(image, angle):
    transparent_bg_image = image.copy().convert('RGBA')
    transparent_bg_image = transparent_bg_image.rotate(angle, Image.BICUBIC, expand = True)
    collage = Image.new('RGBA', transparent_bg_image.size, 'white')
    collage.paste(transparent_bg_image, (0, 0), transparent_bg_image)
    return collage