Exemple #1
0
def GetImageSize(file):
    import img
    try:
        rdr = img.reader(None, file)
    except img.error:
        return 0, 0
    else:
        return rdr.width, rdr.height
Exemple #2
0
 def opendoc(self, *args):
     pathname = EasyDialogs.AskFileForOpen()  # Any file type
     if not pathname:
         return
     bar = EasyDialogs.ProgressBar('Reading and converting...')
     try:
         rdr = img.reader(imgformat.macrgb16, pathname)
     except img.error, arg:
         EasyDialogs.Message(repr(arg))
         return
 def opendoc(self, *args):
     pathname = EasyDialogs.AskFileForOpen() # Any file type
     if not pathname:
         return
     bar = EasyDialogs.ProgressBar('Reading and converting...')
     try:
         rdr = img.reader(imgformat.macrgb16, pathname)
     except img.error, arg:
         EasyDialogs.Message(repr(arg))
         return
Exemple #4
0
    def _loadimagefromfile(self, image, scale=None):
        if not image:
            return
        format = PIXELFORMAT
        try:
            rdr = img.reader(format, image)
            bits = rdr.read()
        except (img.error, IOError, MemoryError):
            return

        pixmap = mac_image.mkpixmap(rdr.width, rdr.height, format, bits)
        return (rdr.width, rdr.height, (pixmap, bits))
Exemple #5
0
 def new(self, url):
     if self.dict.has_key(url):
         self.dict[url][0] = self.dict[url][0] + 1
         return self.dict[url][1]
     fname = MMurl.urlretrieve(url)[0]
     image = img.reader(PIXELFORMAT, fname)
     data = image.read()
     pixmap = mac_image.mkpixmap(image.width, image.height, PIXELFORMAT,
                                 data)
     handle = Res.Resource(url)
     self.dict[url] = [1, handle, pixmap, data, image.width, image.height]
     return handle
Exemple #6
0
def resolveImage(widget, src, noload=0):
    import MMurl, X, img, imgformat, windowinterface
    visual = widget.visual
    if visual.c_class == X.TrueColor and visual.depth == 8:
        format = windowinterface.toplevel._imgformat
    else:
        format = imgformat.xcolormap
    dict = image_cache.get((src, format))
    if dict is not None:
        return dict
    if noload:
        return
    try:
        filename, info = MMurl.urlretrieve(src)
    except IOError:
        return
    try:
        reader = img.reader(format, filename)
    except:
        return
    is_transparent = 0
    if hasattr(reader, 'transparent') and \
       hasattr(reader, 'colormap'):
        is_transparent = 1
        reader.colormap[
            reader.
            transparent] = windowinterface.toplevel._colormap.QueryColor(
                widget.background)[1:4]
    if format is imgformat.xcolormap:
        colors = map(None, reader.colormap)
    else:
        colors = range(256)
        colors = map(None, colors, colors, colors)
    dict = {
        'width': reader.width,
        'height': reader.height,
        'image_data': reader.read(),
        'colors': colors
    }
    if not is_transparent:
        # only cache non-transparent images since the
        # background can be different next time we use
        # the image
        image_cache[(src, format)] = dict
    return dict
Exemple #7
0
    def __namecallback(self, widget, index, call_data):
        import img
        self.__index = index
        if self.__gc is None:
            self.__gc = self.__draw.CreateGC(
                {'foreground': self.__window.background})
        self.__gc.FillRectangle(0, 0, WIDTH, HEIGHT)
        filename = self.__descriptions[index][1]
        self.__text.value = self.__descriptions[index][0] or ''
        if filename:

            try:
                rdr = img.reader(toplevel._imgformat, filename)
            except:
                # don't try to display non-existing image...
                return
            xim = toplevel._visual.CreateImage(
                toplevel._visual.depth, X.ZPixmap, 0, rdr.read(), rdr.width,
                rdr.height, toplevel._imgformat.descr['align'], 0)
            xim.byte_order = toplevel._byteorder
            self.__gc.PutImage(xim, 0, 0, (WIDTH - rdr.width) / 2,
                               (HEIGHT - rdr.height) / 2, rdr.width,
                               rdr.height)
Exemple #8
0
def mkimg(file, format):
    try:
        rdr = img.reader(format, file)
    except:
        print 'error opening image file', file
        return

    # figure out the name of the format
    for fmt in dir(imgformat):
        if getattr(imgformat, fmt) is rdr.format:
            break
    else:
        print 'internal error: unknown format'
        sys.exit(1)

    descr = rdr.format.descr
    byteorder = 0  # whether we deal with byte order
    if descr['size'] > 8:
        byteorder = 1
        # must deal with little-endian / big-endian difference
        comp = descr['comp']
        for c in comp:
            if c[0] % 8 != 0 or c[1] != 8:
                print 'format must use whole-byte pixels/colors'
                sys.exit(1)
        size = descr['size']
        align = descr['align']
        for altname in dir(imgformat):
            altfmt = getattr(imgformat, altname)
            if type(altfmt) is not type(rdr.format):
                continue
            descr1 = altfmt.descr
            if descr1['size'] != size or \
               descr1['align'] != align or \
               len(descr1['comp']) != len(comp):
                continue
            comp1 = descr1['comp']
            for i in range(len(comp)):
                if comp1[i][0] != size - comp[i][1] - comp[i][0]:
                    break
            else:
                # success
                break
        else:
            print 'cannot find format for other byte order'
            sys.exit(1)

    f = open(os.path.splitext(os.path.basename(file))[0] + '.py', 'w')
    f.write('''\
__version__ = "$''' 'Id' '''$"
''')
    if byteorder:
        f.write('''\
import struct

_bigendian = struct.pack('i', 1)[0] == '\\0'
''')
    f.write('''
class reader:
    def __init__(self):
            self.width = %(width)d
            self.height = %(height)d
            import imgformat
''' % {
        'width': rdr.width,
        'height': rdr.height
    })
    if byteorder:
        f.write('''\
        if _bigendian == %(bigendian)d:
                format = imgformat.%(format)s
        else:
                format = imgformat.%(altformat)s
''' % {
            'bigendian': bigendian,
            'format': fmt,
            'altformat': altname
        })
    else:
        f.write('''\
        format = imgformat.%s
''' % fmt)
    f.write('''\
            self.format = format
            self.format_choices = (format,)
''')
    if hasattr(rdr, 'colormap'):
        colormaple = []
        colormapbe = []
        colormap = rdr.colormap.getmapdata()
        f.write('\t\timport imgcolormap\n')
        f.write('\t\tself.colormap = imgcolormap.colormap(')
        pprint.pprint(colormap, f)
        f.write(')\n')
    if hasattr(rdr, 'transparent'):
        f.write('\t\tself.transparent = %d\n' % rdr.transparent)
    if hasattr(rdr, 'top'):
        f.write('\t\tself.top = %d\n' % rdr.top)
    if hasattr(rdr, 'left'):
        f.write('\t\tself.left = %d\n' % rdr.left)
    if hasattr(rdr, 'aspect'):
        f.write('\t\tself.aspect = %d\n' % rdr.aspect)
    if hasattr(rdr, 'name'):
        f.write('\t\tself.name = ')
        writedata(f.write, rdr.name)
        f.write('\n')
    f.write('\n\tdef read(self):\n')
    f.write('\t\treturn ')
    writedata(f.write, rdr.read())
    f.write('\n')
Exemple #9
0
import sys, Xt, Xm, X, img, imgformat, imgconvert
Exemple #10
0
import sys
Exemple #11
0
"""imgconvert - A very simple image converter."""
import img
import sys
import os
# Set trace, so we can see what happens
img.settrace(1)
if os.name == 'mac':
	import macfs
	fss, ok = macfs.StandardGetFile()
	if not ok: sys.exit(0)
	infile = fss.as_pathname()
	fss, ok = macfs.StandardPutFile('Output?')
	if not ok: sys.exit(0)
	outfile = fss.as_pathname()
else:
	if len(sys.argv) <> 3:
	    print 'Usage:',sys.argv[0],'inimage outimage'
	    sys.exit(1)
	infile = sys.argv[1]
	outfile = sys.argv[2]
#
# Create a non-converting writer (we do conversion in the reader)
#
writer = img.writer(None, outfile)
#
# Create a reader that converts to the preferred output format
#
reader = img.reader(writer.format, infile)
#
# Set output file parameters
Exemple #12
0
def convertimagefile(u, srcurl, dstdir, file, node):
    # ignore suggested extension and make our own
    file = os.path.splitext(file)[0] + '.jpg'
    fullpath = os.path.join(dstdir, file)
    if identicalfiles(srcurl, fullpath):
        # src and dst files are the same, don't do anything
        u.close()
        if __debug__:
            print 'src and dst files are identical', fullpath
        return file
    import imgjpeg, imgconvert
    if u is not None:
        u.close()
    f, hdr = MMurl.urlretrieve(srcurl)
    wt = imgjpeg.writer(fullpath)
    wt.restart_interval = 1
    if node is not None:
        if type(node) == type({}):
            quality = node.get('project_quality')
        else:
            quality = node.GetAttrDef('project_quality', None)
        if quality:
            wt.quality = quality
    if hdr.subtype == 'jpeg':
        # special-case code for JPEG images so that we don't loose info
        rdr = imgjpeg.reader(f)
        wt.copy(rdr)
        if os.name == 'mac':
            import macfs
            import macostools
            fss = macfs.FSSpec(fullpath)
            fss.SetCreatorType('ogle', 'JPEG')
            macostools.touched(fss)
        return file
    if sys.platform == 'win32':
        import __main__
        import imgformat
        from win32ig import win32ig
        doc = __main__.toplevel.getActiveDocFrame()
        img = __main__.toplevel._image_handle(f, doc)
        if __main__.toplevel._image_depth(f, doc) != 24:
            win32ig.color_promote(img, 3)  # IG_PROMOTE_TO_24
        width, height = __main__.toplevel._image_size(f, doc)
        data = win32ig.read(img)
        srcfmt = imgformat.bmprgble_noalign
    else:
        import img
        rdr = img.reader(wt.format_choices[0], f)
        width = rdr.width
        height = rdr.height
        data = rdr.read()
        srcfmt = rdr.format
    imgconvert.setquality(0)
    wt = imgconvert.stackwriter(srcfmt, wt)
    wt.width = width
    wt.height = height
    wt.write(data)
    if os.name == 'mac':
        import macfs
        import macostools
        fss = macfs.FSSpec(fullpath)
        fss.SetCreatorType('ogle', 'JPEG')
        macostools.touched(fss)
    return file
Exemple #13
0
"""imgconvert - A very simple image converter."""
import img
import sys
import os
# Set trace, so we can see what happens
img.settrace(1)
if os.name == 'mac':
    import macfs
    fss, ok = macfs.StandardGetFile()
    if not ok: sys.exit(0)
    infile = fss.as_pathname()
    fss, ok = macfs.StandardPutFile('Output?')
    if not ok: sys.exit(0)
    outfile = fss.as_pathname()
else:
    if len(sys.argv) <> 3:
        print 'Usage:', sys.argv[0], 'inimage outimage'
        sys.exit(1)
    infile = sys.argv[1]
    outfile = sys.argv[2]
#
# Create a non-converting writer (we do conversion in the reader)
#
writer = img.writer(None, outfile)
#
# Create a reader that converts to the preferred output format
#
reader = img.reader(writer.format, infile)
#
# Set output file parameters
Exemple #14
0
"""imgbrowse - Display pictures using img"""
Exemple #15
0
"""imgbrowse - Display pictures using img"""