示例#1
0
    def resize0( srcFile="", destFile="", w=200,h=200 ):
        imgs = ImageList()
        imgs.readImages( srcFile )
        imgs.scaleImages("%dx>"%w)

        imgs.writeImages(destFile)
        return "True"
示例#2
0
    def resize0(srcFile="", destFile="", w=200, h=200):
        imgs = ImageList()
        imgs.readImages(srcFile)
        imgs.scaleImages("%dx>" % w)

        imgs.writeImages(destFile)
        return "True"
示例#3
0
 def process_gif(self, source, target, size):
     from pgmagick import ImageList, Geometry
     list = ImageList()
     list.readImages(source)
     list.scaleImages(Geometry(size['size'][0], size['size'][1]))
     list.writeImages(target.name)
     pass
示例#4
0
    def resize5( srcFile="", destFile="", w=200,h=200 ):
        imgs = ImageList()
        outImgs = ImageList()
        imgs.readImages( srcFile )
        gifFrameLen = len( imgs )
        #取得gif的第0帧
        img = imgs.__getitem__( 0 )
        #sw源图宽度
        sw = img.columns()
        sh = img.rows()
        #要缩略的宽度
        rw = w
        #要缩略的高度
        rh = h

        #源图的宽高比
        sratio = float(sw)/float(sh)
        #目标图的宽高比
        rratio = float(rw)/float(rh)

        if ( sw>w ):
            imgs.scaleImages( "%dx"%w)
        #
        #??长大高小的图片处理问题:1600x94 转换为160x298按照宽度等比缩放
        #??长大高小的图片处理问题:1600x94 转换为522x294
        #若源图的宽高比大于目标图的宽高比时,则按照高进行缩放后再裁剪宽度
        else:
            if ( sratio > rratio ):
                hscale = float(rh)/float(sh)
                w = int(sw*hscale)
                h = int(sh*hscale)
                #print (sw,sh,w,h,rw,rh,hscale)
                #就高缩放
                imgs.scaleImages( "%dx"%(w) )

            #若源图的宽高比小于目标图的宽高比时,则按照宽进行缩放后再裁剪高度
            else:
                wscale = float(rw)/float(sw)

                w = int(sw*wscale)
                h = int(sh*wscale)
                #print (sw,sh,w,h,rw,rh,wscale)
                #就宽缩放
                imgs.scaleImages("%dx%d"%(w,h))
                #缩放完后遍历裁剪
            for i in range( gifFrameLen ):
                tmpImg = imgs.__getitem__( i )
                tmpImg.crop(Geometry( rw,rh,0,0 ) )
                tmpImg.profile("*", Blob())
                outImgs.append( tmpImg )
                #(102, 900, 160, 1411, 160, 298)
                #print( sw,sh,w,h,rw,rh)

        if ( len( outImgs ) > 0 ):
            outImgs.writeImages(destFile)
        else:
            imgs.writeImages(destFile)
        return "True"
示例#5
0
    def resize5(srcFile="", destFile="", w=200, h=200):
        imgs = ImageList()
        outImgs = ImageList()
        imgs.readImages(srcFile)
        gifFrameLen = len(imgs)
        #取得gif的第0帧
        img = imgs.__getitem__(0)
        #sw源图宽度
        sw = img.columns()
        sh = img.rows()
        #要缩略的宽度
        rw = w
        #要缩略的高度
        rh = h

        #源图的宽高比
        sratio = float(sw) / float(sh)
        #目标图的宽高比
        rratio = float(rw) / float(rh)

        if (sw > w):
            imgs.scaleImages("%dx" % w)
        #
        #??长大高小的图片处理问题:1600x94 转换为160x298按照宽度等比缩放
        #??长大高小的图片处理问题:1600x94 转换为522x294
        #若源图的宽高比大于目标图的宽高比时,则按照高进行缩放后再裁剪宽度
        else:
            if (sratio > rratio):
                hscale = float(rh) / float(sh)
                w = int(sw * hscale)
                h = int(sh * hscale)
                #print (sw,sh,w,h,rw,rh,hscale)
                #就高缩放
                imgs.scaleImages("%dx" % (w))

            #若源图的宽高比小于目标图的宽高比时,则按照宽进行缩放后再裁剪高度
            else:
                wscale = float(rw) / float(sw)

                w = int(sw * wscale)
                h = int(sh * wscale)
                #print (sw,sh,w,h,rw,rh,wscale)
                #就宽缩放
                imgs.scaleImages("%dx%d" % (w, h))
                #缩放完后遍历裁剪
            for i in range(gifFrameLen):
                tmpImg = imgs.__getitem__(i)
                tmpImg.crop(Geometry(rw, rh, 0, 0))
                tmpImg.profile("*", Blob())
                outImgs.append(tmpImg)
                #(102, 900, 160, 1411, 160, 298)
                #print( sw,sh,w,h,rw,rh)

        if (len(outImgs) > 0):
            outImgs.writeImages(destFile)
        else:
            imgs.writeImages(destFile)
        return "True"
示例#6
0
 def resize2( srcFile="", destFile="", w=200, h=200, Ignore=False ):
     imgs = ImageList()
     imgs.readImages( srcFile )
     if ( h != -1 ):
         imgs.scaleImages("%dx%d!" % (w,h))
     else:
         imgs.scaleImages("%dx!" % w )
     imgs.writeImages(destFile)
     return "True"
示例#7
0
 def resize2(srcFile="", destFile="", w=200, h=200, Ignore=False):
     imgs = ImageList()
     imgs.readImages(srcFile)
     if (h != -1):
         imgs.scaleImages("%dx%d!" % (w, h))
     else:
         imgs.scaleImages("%dx!" % w)
     imgs.writeImages(destFile)
     return "True"
示例#8
0
def addMetaData(path, job, result):
    """ Use this method to add meta data to the image. Due to a bug in
    exiv2, its python wrapper pyexiv2 is of no use to us. This bug
    (http://dev.exiv2.org/issues/762) hinders us to work on multi-page
    TIFF files. Instead, we use a separate tool called exiftool to write
    meta data. Currently, there seems no better solution than this. If the
    tool is not found, no meta data is produced and no error is raised.
    """
    # Add resolution information in pixel per nanometer. The stack info
    # available is nm/px and refers to a zoom-level of zero.
    res_x_scaled = job.ref_stack.resolution.x * 2**job.zoom_level
    res_y_scaled = job.ref_stack.resolution.y * 2**job.zoom_level
    res_x_nm_px = 1.0 / res_x_scaled
    res_y_nm_px = 1.0 / res_y_scaled
    res_args = "-EXIF:XResolution={0} -EXIF:YResolution={1} -EXIF:" \
            "ResolutionUnit=None".format( str(res_x_nm_px), str(res_y_nm_px) )

    # ImageJ specific meta data to allow easy embedding of units and
    # display options.
    n_images = len(result)
    ij_version = "1.45p"
    unit = "nm"
    newline = "\n"

    # sample with (the actual is a line break instead of a .):
    # ImageJ=1.45p.images={0}.channels=1.slices=2.hyperstack=true.mode=color.unit=micron.finterval=1.spacing=1.5.loop=false.min=0.0.max=4095.0.
    ij_data = "ImageJ={1}{0}unit={2}{0}".format(newline, ij_version, unit)
    if n_images > 1:
        n_channels = len(job.stacks)
        if n_images % n_channels != 0:
            raise ValueError( "Meta data creation: the number of images " \
                    "modulo the channel count is not zero" )
        n_slices = n_images / n_channels
        ij_data += "images={1}{0}channels={2}{0}slices={3}{0}hyperstack=true{0}mode=color{0}".format(
            newline, str(n_images), str(n_channels), str(n_slices))
    ij_args = "-EXIF:ImageDescription=\"{0}\"".format(ij_data)

    # Information about the software used
    sw_args = "-EXIF:Software=\"Created with CATMAID and GraphicsMagic, " \
            "processed with exiftool.\""
    # Build up the final tag changing arguments for each slice
    tag_args = "{0} {1} {2}".format(res_args, ij_args, sw_args)
    per_slice_tag_args = []
    for i in range(0, n_images):
        # the string EXIF gets replaced for every image with IFD<N>
        slice_args = tag_args.replace("EXIF", "IFD" + str(i))
        per_slice_tag_args.append(slice_args)
    final_tag_args = " ".join(per_slice_tag_args)
    # Create the final call and execute
    call = "exiftool -overwrite_original {0} {1}".format(final_tag_args, path)
    os.system(call)

    # Re-save the image with GraphicsMagick, otherwise ImageJ won't read the
    # images directly.
    images = ImageList()
    images.readImages(path)
    images.writeImages(path)
示例#9
0
def addMetaData( path, job, result ):
    """ Use this method to add meta data to the image. Due to a bug in
    exiv2, its python wrapper pyexiv2 is of no use to us. This bug
    (http://dev.exiv2.org/issues/762) hinders us to work on multi-page
    TIFF files. Instead, we use a separate tool called exiftool to write
    meta data. Currently, there seems no better solution than this. If the
    tool is not found, no meta data is produced and no error is raised.
    """
    # Add resolution information in pixel per nanometer. The stack info
    # available is nm/px and refers to a zoom-level of zero.
    res_x_scaled = job.ref_stack.resolution.x * 2**job.zoom_level
    res_y_scaled = job.ref_stack.resolution.y * 2**job.zoom_level
    res_x_nm_px = 1.0 / res_x_scaled
    res_y_nm_px = 1.0 / res_y_scaled
    res_args = "-EXIF:XResolution={0} -EXIF:YResolution={1} -EXIF:" \
            "ResolutionUnit=None".format( str(res_x_nm_px), str(res_y_nm_px) )

    # ImageJ specific meta data to allow easy embedding of units and
    # display options.
    n_images = len( result )
    ij_version= "1.45p"
    unit = "nm"
    newline = "\n"

    # sample with (the actual is a line break instead of a .):
    # ImageJ=1.45p.images={0}.channels=1.slices=2.hyperstack=true.mode=color.unit=micron.finterval=1.spacing=1.5.loop=false.min=0.0.max=4095.0.
    ij_data = "ImageJ={1}{0}unit={2}{0}".format( newline, ij_version, unit)
    if n_images > 1:
        n_channels = len(job.stacks)
        if n_images % n_channels != 0:
            raise ValueError( "Meta data creation: the number of images " \
                    "modulo the channel count is not zero" )
        n_slices = n_images / n_channels
        ij_data += "images={1}{0}channels={2}{0}slices={3}{0}hyperstack=true{0}mode=color{0}".format( newline, str(n_images), str(n_channels), str(n_slices) )
    ij_args = "-EXIF:ImageDescription=\"{0}\"".format( ij_data )

    # Information about the software used
    sw_args = "-EXIF:Software=\"Created with CATMAID and GraphicsMagic, " \
            "processed with exiftool.\""
    # Build up the final tag changing arguments for each slice
    tag_args = "{0} {1} {2}".format( res_args, ij_args, sw_args )
    per_slice_tag_args = []
    for i in range(0, n_images):
        # the string EXIF gets replaced for every image with IFD<N>
        slice_args = tag_args.replace( "EXIF", "IFD" + str(i) )
        per_slice_tag_args.append( slice_args  )
    final_tag_args = " ".join( per_slice_tag_args )
    # Create the final call and execute
    call = "exiftool -overwrite_original {0} {1}".format( final_tag_args, path )
    os.system( call )

    # Re-save the image with GraphicsMagick, otherwise ImageJ won't read the
    # images directly.
    images = ImageList()
    images.readImages( path )
    images.writeImages( path )
示例#10
0
 def get_image(self, source):
     blob = Blob()
     blob.update(source.read())
     if source.name.lower().endswith('.gif'):
         image = ImageList()
         image.readImages(blob)
         image.coalesceImags()
     else:
         image = Image(blob)
     return image
示例#11
0
    def resize4(srcFile="", destFile="", w=200, h=200):
        imgs = ImageList()
        imgs.readImages(srcFile)
        imgs.scaleImages("%dx%d<" % (int(w), int(h)))
        """
        gifFrame0 = imgs.__getitem__(0)

        sw = gifFrame0.columns()
        sh = gifFrame0.rows()
        #小于指定的宽高时才拉伸,notice是等比缩放,目标尺寸不成比例时不会达到目标尺寸
        if ( sw < w or sh < h ):
            imgs.scaleImages("%dx%d" % (int(w),int(h)))
        """
        imgs.writeImages(destFile)
        return "True"
示例#12
0
    def resize4( srcFile="", destFile="", w=200,h=200 ):
        imgs = ImageList()
        imgs.readImages( srcFile )
        imgs.scaleImages("%dx%d<" % (int(w),int(h)))
        """
        gifFrame0 = imgs.__getitem__(0)

        sw = gifFrame0.columns()
        sh = gifFrame0.rows()
        #小于指定的宽高时才拉伸,notice是等比缩放,目标尺寸不成比例时不会达到目标尺寸
        if ( sw < w or sh < h ):
            imgs.scaleImages("%dx%d" % (int(w),int(h)))
        """
        imgs.writeImages(destFile)
        return "True"
示例#13
0
from pgmagick import Image, ImageList, Geometry, Color

imgs = ImageList()
for color in ("red", "blue", "green", "black", "yellow"):
    imgs.append(Image(Geometry(200, 200), Color(color)))
imgs.animationDelayImages(100)
imgs.scaleImages(Geometry(100, 100))
print len(imgs)
imgs.writeImages("output.gif")

imgs = ImageList()
imgs.readImages("output.gif")
for img in imgs:
    print img
示例#14
0
 def resize3( srcFile="", destFile="", w=200, h=200, Ignore=False ):
     imgs = ImageList()
     imgs.readImages(srcFile)
     imgs.scaleImages("%dx%d>"%(w,h))
     imgs.writeImages(destFile)
     return "True"
示例#15
0
 def resize3(srcFile="", destFile="", w=200, h=200, Ignore=False):
     imgs = ImageList()
     imgs.readImages(srcFile)
     imgs.scaleImages("%dx%d>" % (w, h))
     imgs.writeImages(destFile)
     return "True"
示例#16
0
srcGifFrame2 = "./testGif/rBABE1DX6Q2R4VdiABCfwP0OA-o539.gif[2]"

#imgFrame0 = Image( srcGifFrame0 )
#imgFrame1 = Image( srcGifFrame1 )
#imgFrame2 = Image( srcGifFrame2 )

#imgFrame2.write('aaaa.gif')
imgs = ImageList(  )
outImage = ImageList()
#imgs.append(imgFrame0)
#imgs.append(imgFrame1)
#imgs.append(imgFrame2)
#imgs.animationDelayImages(100)
a = ImageType()

imgs.readImages(srcGifFrame0)
imgs.scaleImages( "550x550")
img = imgs.__getitem__( 0 )
print img.columns(),img.rows()

print len( imgs )
#imgs.animationDelayImages(100)
imgs.writeImages( './test.gif' )

"""
from pgmagick import Image, ImageList, Geometry, Color



imgs = ImageList()
示例#17
0
    def resize6( srcFile="", destFile="", w=200,h=200 ):
        imgs = ImageList()

        imgs.readImages(srcFile)
        gifFrameLen = len(imgs)

        #若只有一帧直接调用静态方法输出
        if ( gifFrameLen == 1 ):
            return staticPhoto.resize6(srcFile,destFile,w,h)

        outImg = ImageList()

        #取得第0帧
        gifFrame0 = imgs.__getitem__(0)

        #sw源图宽度
        sw = gifFrame0.columns()
        sh = gifFrame0.rows()
        #如果宽高比大小于倍则转成静态图片
        if ( int(sw/sh) > 5 ):
            return staticPhoto.resize1(srcFile,destFile,w,h)

        #要缩略的宽度
        rw = w
        #要缩略的高度
        rh = h

        #源图的宽高比
        sratio = float(sw)/float(sh)
        #目标图的宽高比
        rratio = float(rw)/float(rh)

        #若源图的宽高比大于目标图的宽高比时,则按照高进行缩放后再裁剪宽度
        if ( sratio > rratio ):
            hscale = float(rh)/float(sh)
            w = int(sw*hscale)
            h = int(sh*hscale)
            #print (sw,sh,w,h,rw,rh,hscale)
            #就高缩放
            imgs.scaleImages("%dx%d"%(w,h))

            #计算裁剪宽的部分的横坐标,超出的宽的部分进行裁剪
            tmpRowsPos = int((sw*hscale - rw)/2)
            #imgs.coalesceImags(Geometry( rw,rh,tmpRowsPos,0 ))
            for i in range( gifFrameLen ):
                tmpImg = imgs.__getitem__(i)
                #print w,h,sw,sh,rw,rh,tmpRowsPos,i,gifFrameLen,tmpImg.columns(),tmpImg.rows()
                #594 260 320 140 132 260 231 0 145 320 140

                if ( sw == tmpImg.columns() and sh == tmpImg.rows() and tmpRowsPos > 0 ):
                    return staticPhoto.resize1(srcFile,destFile,rw,rh)
                tmpImg.crop(Geometry( rw,rh,tmpRowsPos,0 ) )
                tmpImg.profile("*", Blob())
                outImg.append(tmpImg)
            outImg.writeImages(destFile)
            return "True"
        #若源图的宽高比小于目标图的宽高比时,则按照宽进行缩放后再裁剪高度
        else:
            wscale = float(rw)/float(sw)
            w = int(sw*wscale)
            h = int(sh*wscale)
            #print (sw,sh,w,h,rw,rh,wscale)
            #就宽缩放
            imgs.scaleImages("%dx%d"%(w,h))
            tmpColsPos = int((sh*wscale-rh)/2 )

            for i in range(gifFrameLen):
                if ( i == 0 ):
                    tmpImg = gifFrame0
                else:
                    tmpImg = imgs.__getitem__(i)
                if ( sw == tmpImg.columns() and sh == tmpImg.rows() and tmpColsPos > 0 ):
                    return staticPhoto.resize1(srcFile,destFile,rw,rh)
                tmpImg.crop( Geometry( rw,rh,0,tmpColsPos ) )
                tmpImg.profile("*", Blob())
                outImg.append(tmpImg)
            outImg.writeImages(destFile)
            return "True"
        return "True"
示例#18
0
from pgmagick import Image, ImageList, Geometry, Color

imgs = ImageList()
for color in ('red', 'blue', 'green', 'black', 'yellow'):
    imgs.append(Image(Geometry(200, 200), Color(color)))
imgs.animationDelayImages(100)
imgs.scaleImages(Geometry(100, 100))
print len(imgs)
imgs.writeImages('output.gif')

imgs = ImageList()
imgs.readImages('output.gif')
for img in imgs:
    print img
示例#19
0
srcGifFrame2 = "./testGif/rBABE1DX6Q2R4VdiABCfwP0OA-o539.gif[2]"

#imgFrame0 = Image( srcGifFrame0 )
#imgFrame1 = Image( srcGifFrame1 )
#imgFrame2 = Image( srcGifFrame2 )

#imgFrame2.write('aaaa.gif')
imgs = ImageList()
outImage = ImageList()
#imgs.append(imgFrame0)
#imgs.append(imgFrame1)
#imgs.append(imgFrame2)
#imgs.animationDelayImages(100)
a = ImageType()

imgs.readImages(srcGifFrame0)
imgs.scaleImages("550x550")
img = imgs.__getitem__(0)
print img.columns(), img.rows()

print len(imgs)
#imgs.animationDelayImages(100)
imgs.writeImages('./test.gif')
"""
from pgmagick import Image, ImageList, Geometry, Color



imgs = ImageList()

for color in ('red', 'blue', 'green', 'black', 'yellow'):
示例#20
0
    def resize6(srcFile="", destFile="", w=200, h=200):
        imgs = ImageList()

        imgs.readImages(srcFile)
        gifFrameLen = len(imgs)

        #若只有一帧直接调用静态方法输出
        if (gifFrameLen == 1):
            return staticPhoto.resize6(srcFile, destFile, w, h)

        outImg = ImageList()

        #取得第0帧
        gifFrame0 = imgs.__getitem__(0)

        #sw源图宽度
        sw = gifFrame0.columns()
        sh = gifFrame0.rows()
        #如果宽高比大小于倍则转成静态图片
        if (int(sw / sh) > 5):
            return staticPhoto.resize1(srcFile, destFile, w, h)

        #要缩略的宽度
        rw = w
        #要缩略的高度
        rh = h

        #源图的宽高比
        sratio = float(sw) / float(sh)
        #目标图的宽高比
        rratio = float(rw) / float(rh)

        #若源图的宽高比大于目标图的宽高比时,则按照高进行缩放后再裁剪宽度
        if (sratio > rratio):
            hscale = float(rh) / float(sh)
            w = int(sw * hscale)
            h = int(sh * hscale)
            #print (sw,sh,w,h,rw,rh,hscale)
            #就高缩放
            imgs.scaleImages("%dx%d" % (w, h))

            #计算裁剪宽的部分的横坐标,超出的宽的部分进行裁剪
            tmpRowsPos = int((sw * hscale - rw) / 2)
            #imgs.coalesceImags(Geometry( rw,rh,tmpRowsPos,0 ))
            for i in range(gifFrameLen):
                tmpImg = imgs.__getitem__(i)
                #print w,h,sw,sh,rw,rh,tmpRowsPos,i,gifFrameLen,tmpImg.columns(),tmpImg.rows()
                #594 260 320 140 132 260 231 0 145 320 140

                if (sw == tmpImg.columns() and sh == tmpImg.rows()
                        and tmpRowsPos > 0):
                    return staticPhoto.resize1(srcFile, destFile, rw, rh)
                tmpImg.crop(Geometry(rw, rh, tmpRowsPos, 0))
                tmpImg.profile("*", Blob())
                outImg.append(tmpImg)
            outImg.writeImages(destFile)
            return "True"
        #若源图的宽高比小于目标图的宽高比时,则按照宽进行缩放后再裁剪高度
        else:
            wscale = float(rw) / float(sw)
            w = int(sw * wscale)
            h = int(sh * wscale)
            #print (sw,sh,w,h,rw,rh,wscale)
            #就宽缩放
            imgs.scaleImages("%dx%d" % (w, h))
            tmpColsPos = int((sh * wscale - rh) / 2)

            for i in range(gifFrameLen):
                if (i == 0):
                    tmpImg = gifFrame0
                else:
                    tmpImg = imgs.__getitem__(i)
                if (sw == tmpImg.columns() and sh == tmpImg.rows()
                        and tmpColsPos > 0):
                    return staticPhoto.resize1(srcFile, destFile, rw, rh)
                tmpImg.crop(Geometry(rw, rh, 0, tmpColsPos))
                tmpImg.profile("*", Blob())
                outImg.append(tmpImg)
            outImg.writeImages(destFile)
            return "True"
        return "True"