~rosco2/ubuntu/wily/gramps/bug-1492304

« back to all changes in this revision

Viewing changes to src/ImgManip.py

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2004-06-16 16:53:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040616165336-kjzczqef4gnxrn2b
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Gramps - a GTK+/GNOME based genealogy program
 
3
#
 
4
# Copyright (C) 2000-2003  Donald N. Allingham
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
 
 
21
import os
 
22
import const
 
23
import signal
 
24
 
 
25
#-------------------------------------------------------------------------
 
26
#
 
27
# Check for the python imaging library
 
28
#
 
29
#-------------------------------------------------------------------------
 
30
try:
 
31
    import PIL.Image
 
32
    import StringIO
 
33
    PIL.Image.init()
 
34
    no_pil = 0
 
35
except:
 
36
    import popen2
 
37
    import gtk
 
38
    no_pil = 1
 
39
 
 
40
class ImgManip:
 
41
    def __init__(self,source):
 
42
        self.src = source
 
43
 
 
44
    if no_pil:
 
45
 
 
46
        def size(self):
 
47
            img = gtk.gdk.pixbuf_new_from_file(self.src)
 
48
            return (img.get_width(),img.get_height())
 
49
 
 
50
        def fmt_thumbnail(self,dest,width,height,cnv):
 
51
            w = int(width)
 
52
            h = int(height)
 
53
            cmd = "%s -geometry %dx%d '%s' '%s:%s'" % (const.convert,w,h,self.src,cnv,dest)
 
54
            os.system(cmd)
 
55
 
 
56
        def fmt_convert(self,dest,cnv):
 
57
            cmd = "%s '%s' '%s:%s'" % (const.convert,self.src,cnv,dest)
 
58
            os.system(cmd)
 
59
 
 
60
        def fmt_data(self,cnv):
 
61
            cmd = "%s '%s' '%s:-'" % (const.convert,self.src,cnv)
 
62
            r,w = popen2.popen2(cmd)
 
63
            buf = r.read()
 
64
            r.close()
 
65
            w.close()
 
66
            return buf
 
67
 
 
68
        def fmt_scale_data(self,x,y,cnv):
 
69
            cmd = "%s -geometry %dx%d '%s' '%s:-'" % (const.convert,x,y,self.src,cnv)
 
70
            signal.signal (signal.SIGCHLD, signal.SIG_DFL)
 
71
            r,w = popen2.popen2(cmd)
 
72
            buf = r.read()
 
73
            r.close()
 
74
            w.close()
 
75
            return buf
 
76
 
 
77
    else:
 
78
 
 
79
        def size(self):
 
80
            return PIL.Image.open(self.src).size
 
81
 
 
82
        def fmt_thumbnail(self,dest,width,height,pil):
 
83
            im = PIL.Image.open(self.src)
 
84
            im.thumbnail((width,height))
 
85
            if im.mode != 'RGB':
 
86
                im.draft('RGB',im.size)
 
87
                im = im.convert("RGB")
 
88
            im.save(dest,pil.upper())
 
89
        
 
90
        def fmt_convert(self,dest,pil):
 
91
            im = PIL.Image.open(self.src)
 
92
            if im.mode != 'RGB':
 
93
                im.draft('RGB',im.size)
 
94
                im = im.convert("RGB")
 
95
            im.save(dest,pil.upper())
 
96
 
 
97
        def fmt_data(self,pil):
 
98
            g = StringIO.StringIO()
 
99
            im = PIL.Image.open(self.src)
 
100
            if im.mode != 'RGB':
 
101
                im.draft('RGB',im.size)
 
102
                im = im.convert("RGB")
 
103
            im.save(g,pil.upper())
 
104
            g.seek(0)
 
105
            buf = g.read()
 
106
            g.close()
 
107
            return buf
 
108
 
 
109
        def fmt_scale_data(self,x,y,pil):
 
110
            im = PIL.Image.open(self.src)
 
111
            im.thumbnail((x,y))
 
112
            if im.mode != 'RGB':
 
113
                im.draft('RGB',im.size)
 
114
                im = im.convert("RGB")
 
115
            return im.tostring(pil,"RGB")
 
116
 
 
117
    def jpg_thumbnail(self,dest,width,height):
 
118
        self.fmt_thumbnail(dest,width,height,"jpeg")
 
119
 
 
120
    def png_thumbnail(self,dest,width,height):
 
121
        self.fmt_thumbnail(dest,width,height,"png")
 
122
 
 
123
    def eps_thumbnail(self,dest,width,height):
 
124
        self.fmt_thumbnail(dest,width,height,"eps")
 
125
 
 
126
    def jpg_convert(self,dest):
 
127
        self.fmt_convert(dest,"jpeg")
 
128
 
 
129
    def png_convert(self,dest):
 
130
        self.fmt_convert(dest,"png")
 
131
 
 
132
    def eps_convert(self,dest):
 
133
        self.fmt_convert(dest,"eps")
 
134
 
 
135
    def jpg_data(self):
 
136
        return self.fmt_data("jpeg")
 
137
 
 
138
    def png_data(self):
 
139
        return self.fmt_data("png")
 
140
 
 
141
    def eps_data(self):
 
142
        return self.fmt_data("eps")
 
143
 
 
144
    def jpg_scale_data(self,x,y):
 
145
        return self.fmt_scale_data(x,y,"jpeg")
 
146
 
 
147
    def png_scale_data(self,x,y):
 
148
        return self.fmt_scale_data(x,y,"png")
 
149
 
 
150
    def eps_scale_data(self,x,y):
 
151
        return self.fmt_scale_data(x,y,"eps")
 
152
 
 
153
 
 
154
if __name__ == "__main__":
 
155
 
 
156
    import sys
 
157
    
 
158
    img = ImgManip(sys.argv[1])
 
159
    img.jpg_thumbnail("foo.jpg",50,50)
 
160
    img.png_thumbnail("foo.png",50,50)
 
161
    img.eps_thumbnail("foo.eps",50,50)
 
162
 
 
163
 
 
164
 
 
165
 
 
166