示例#1
0
def process_crop_job(job, create_message=True):
    """ This method does the actual cropping. It controls the data extraction
    and the creation of the sub-stack. It can be executed as Celery task.
    """
    try:
        # Create the sub-stack
        cropped_stack = extract_substack( job )

        # Create tho output image
        outputImage = ImageList()
        for img in cropped_stack:
            outputImage.append( img )

        # Save the resulting micro_stack to a temporary location
        no_error_occured = True
        error_message = ""
        # Only produce an image if parts of stacks are within the output
        if len( cropped_stack ) > 0:
            outputImage.writeImages( job.output_path )
            # Add some meta data to the image
            addMetaData( job.output_path, job, cropped_stack )
        else:
            no_error_occured = False
            error_message = "A region outside the stack has been selected. " \
                    "Therefore, no image was produced."
    except (IOError, OSError, ValueError), e:
        no_error_occured = False
        error_message = str(e)
        # Delete the file if parts of it have been written already
        if os.path.exists( job.output_path ):
            os.remove( job.output_path )
示例#2
0
def process_crop_job(job, create_message=True):
    """ This method does the actual cropping. It controls the data extraction
    and the creation of the sub-stack. It can be executed as Celery task.
    """
    try:
        # Create the sub-stack
        cropped_stack = extract_substack(job)

        # Create tho output image
        outputImage = ImageList()
        for img in cropped_stack:
            outputImage.append( img )

        # Save the resulting micro_stack to a temporary location
        no_error_occured = True
        error_message = ""
        # Only produce an image if parts of stacks are within the output
        if len( cropped_stack ) > 0:
            outputImage.writeImages( job.output_path.encode('ascii', 'ignore') )
            # Add some meta data to the image
            addMetaData( job.output_path, job, cropped_stack )
        else:
            no_error_occured = False
            error_message = "A region outside the stack has been selected. " \
                    "Therefore, no image was produced."
    except (IOError, OSError, ValueError) as e:
        no_error_occured = False
        error_message = str(e)
        # Delete the file if parts of it have been written already
        if os.path.exists( job.output_path ):
            os.remove( job.output_path )

    if create_message:
        # Create a notification message
        bb_text = "( %s, %s, %s ) -> ( %s, %s, %s )" % (job.x_min, job.y_min, \
                job.z_min, job.x_max, job.y_max, job.z_max)

        msg = Message()
        msg.user = User.objects.get(pk=int(job.user.id))
        msg.read = False
        if no_error_occured:
            file_name = os.path.basename( job.output_path )
            url = os.path.join( settings.CATMAID_URL, "crop/download/" + file_name + "/")
            msg.title = "Microstack finished"
            msg.text = "The requested microstack %s is finished. You can " \
                    "download it from this location: <a href='%s'>%s</a>" % \
                    (bb_text, url, url)
            msg.action = url
        else:
            msg.title = "Microstack could not be created"
            msg.text = "The requested microstack %s could not be created due " \
                    "to an error while saving the result (%s)." % \
                    (bb_text, error_message)
            msg.action = ""
        msg.save()

    return job.output_path if no_error_occured else error_message
示例#3
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"
示例#4
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 )
示例#5
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"
示例#6
0
    def resize0(srcFile="", destFile="", w=200, h=200):
        imgs = ImageList()
        imgs.readImages(srcFile)
        imgs.scaleImages("%dx>" % w)

        imgs.writeImages(destFile)
        return "True"
示例#7
0
def GenerateSHPpreview(mapObject):
	# generates gif preview of shp files for every mapObject in list of objects
	currentDirectory = os.getcwd()
	for item in mapObject:
		path = os.getcwd() + os.sep + 'openraData/data/maps/' + str(item.id) + os.sep
		Dir = os.listdir(path + 'content/')
		if os.path.isdir(path+'content/png/'):
			shutil.rmtree(path+'content/png/')
		for fn in Dir:
			if fn.endswith('.shp'):
				os.mkdir(path + 'content/png/')
				os.chdir(path + 'content/png/')
				command = 'mono --debug %sOpenRA.Utility.exe %s --png %s %s' % (settings.OPENRA_PATH, item.game_mod, path+'content/'+fn, '../../../../palettes/0/RA1/temperat.pal')
				proc = Popen(command.split(), stdout=PIPE).communicate()
				pngsdir = os.listdir(path + 'content/png/')
				imglist = []
				for pngfn in pngsdir:
					if pngfn.endswith('.png'):
						imglist.append(pngfn)
				imglist.sort()
				imgs = ImageList()
				for img in imglist:
					imgs.append(Image(path+'content/png/'+img))
				imgs.animationDelayImages(50)
				imgs.writeImages(path+'content/'+fn+'.gif')
				os.chdir(currentDirectory)
				shutil.rmtree(path+'content/png/')
	return True
示例#8
0
    def resize0( srcFile="", destFile="", w=200,h=200 ):
        imgs = ImageList()
        imgs.readImages( srcFile )
        imgs.scaleImages("%dx>"%w)

        imgs.writeImages(destFile)
        return "True"
示例#9
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
示例#10
0
def save_files(png_list, save_name):
    print("Saving files to gif:%s" % save_name)
    imgs = ImageList()
    for file_name in png_list:
        im = PGImage(file_name)
        imgs.animationDelayImages(5)
        imgs.append(im)

    imgs.writeImages(save_name)
示例#11
0
def process_crop_job(job, create_message=True):
    """ This method does the actual cropping. It controls the data extraction
    and the creation of the sub-stack. It can be executed as Celery task.
    """
    try:
        # Create the sub-stack
        cropped_stack = extract_substack(job)

        # Create tho output image
        outputImage = ImageList()
        for img in cropped_stack:
            outputImage.append(img)

        # Save the resulting micro_stack to a temporary location
        no_error_occured = True
        error_message = ""
        # Only produce an image if parts of stacks are within the output
        if len(cropped_stack) > 0:
            outputImage.writeImages(job.output_path)
            # Add some meta data to the image
            addMetaData(job.output_path, job, cropped_stack)
        else:
            no_error_occured = False
            error_message = "A region outside the stack has been selected. " \
                    "Therefore, no image was produced."
    except (IOError, OSError), e:
        no_error_occured = False
        error_message = str(e)
        # Delete the file if parts of it have been written already
        if os.path.exists(job.output_path):
            os.remove(job.output_path)
示例#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
def process_crop_job(job, create_message=True):
    """ This method does the actual cropping. It controls the data extraction
    and the creation of the sub-stack. It can be executed as Celery task.
    """
    try:
        # Create the sub-stack
        cropped_stack = extract_substack(job)

        # Create tho output image
        outputImage = ImageList()
        for img in cropped_stack:
            outputImage.append(img)

        # Save the resulting micro_stack to a temporary location
        no_error_occured = True
        error_message = ""
        # Only produce an image if parts of stacks are within the output
        if len(cropped_stack) > 0:
            outputImage.writeImages(job.output_path.encode('ascii', 'ignore'))
            # Add some meta data to the image
            addMetaData(job.output_path, job, cropped_stack)
        else:
            no_error_occured = False
            error_message = "A region outside the stack has been selected. " \
                    "Therefore, no image was produced."
    except (IOError, OSError, ValueError) as e:
        no_error_occured = False
        error_message = str(e)
        # Delete the file if parts of it have been written already
        if os.path.exists(job.output_path):
            os.remove(job.output_path)

    if create_message:
        # Create a notification message
        bb_text = "( %s, %s, %s ) -> ( %s, %s, %s )" % (job.x_min, job.y_min, \
                job.z_min, job.x_max, job.y_max, job.z_max)

        msg = Message()
        msg.user = User.objects.get(pk=int(job.user.id))
        msg.read = False
        if no_error_occured:
            file_name = os.path.basename(job.output_path)
            url = os.path.join(settings.CATMAID_URL,
                               "crop/download/" + file_name + "/")
            msg.title = "Microstack finished"
            msg.text = "The requested microstack %s is finished. You can " \
                    "download it from this location: <a href='%s'>%s</a>" % \
                    (bb_text, url, url)
            msg.action = url
        else:
            msg.title = "Microstack could not be created"
            msg.text = "The requested microstack %s could not be created due " \
                    "to an error while saving the result (%s)." % \
                    (bb_text, error_message)
            msg.action = ""
        msg.save()

    return job.output_path if no_error_occured else error_message
示例#14
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)
示例#15
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
示例#16
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"
示例#17
0
 def GenerateSHPpreview(self):
     Dir = os.listdir(self.map_full_path_directory+'content/')
     for fn in Dir:
         if fn.endswith('.shp'):
             os.mkdir(self.map_full_path_directory+'content/png/')
             os.chdir(self.map_full_path_directory+'content/png/')
             command = 'mono --debug %sOpenRA.Utility.exe %s --png %s %s' % (settings.OPENRA_PATH, self.MapMod, self.map_full_path_directory+'content/'+fn, '../../../../palettes/0/RA1/temperat.pal')
             proc = Popen(command.split(), stdout=PIPE).communicate()
             self.flushLog(proc)
             pngsdir = os.listdir(self.map_full_path_directory+'content/png/')
             imglist = []
             for pngfn in pngsdir:
                 if pngfn.endswith('.png'):
                     imglist.append(pngfn)
             imglist.sort()
             imgs = ImageList()
             for img in imglist:
                 imgs.append(Image(self.map_full_path_directory+'content/png/'+img))
             imgs.animationDelayImages(50)
             imgs.writeImages(self.map_full_path_directory+'content/'+fn+'.gif')
             os.chdir(self.currentDirectory)
             shutil.rmtree(self.map_full_path_directory+'content/png/')
示例#18
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"
示例#19
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"
示例#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"
示例#21
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"
示例#22
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"
 def test_noarg(self):
     imgs = ImageList()
     self.assertEqual(type(imgs), ImageList)
示例#24
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
示例#25
0
    def simulate(self, policy, iterations, output, figure_template, html, gif,
                 anim_delay):

        raw_policy = deepcopy(policy)

        # Policy dictionary
        policy = {
            tuple([pol['state'][agent][1]
                   for agent in self.agents]): pol['action']
            for pol in policy
        }

        print ''
        print 'Goal State Set:'
        print self.name_goal

        print ''
        print 'Initial State:'
        agent_locs = OrderedDict([(agent, None) for agent in self.agents])
        for agent in self.agents:
            agent_locs[agent] = np.random.choice(self.locs)
        print agent_locs.values()
        rwd = 0

        print ''
        print 'Steps'
        history = {}
        for i in range(iterations):

            # Types of agent on each location
            agent_classes_on_loc = OrderedDict([(loc, [])
                                                for loc in self.locs])
            for agent in self.agents:
                agent_classes_on_loc[agent_locs[agent]].append(
                    self.agents_types[agent])

            # State idx
            state_idx = self.s.index(
                tuple([self.locs[s] for s in agent_locs.values()]))

            # Computing rewards
            rwd = 0
            for loc, types in self.name_goal.items():
                if not (set(types) - set(agent_classes_on_loc[loc])):
                    rwd += 1

            # Printing history
            history[i] = {
                'state_idx': state_idx,
                'agent_locs': copy.deepcopy(agent_locs),
                'rwd': rwd
            }

            # Updating agent location, given policy
            for agent in self.agents:

                # Locations adjacent to the agent location
                adj_locs = self.name_loc_roads[agent_locs[agent]]

                # Baseline probability of going to any adjacent location (error)
                pdf = np.zeros(len(self.locs), dtype=np.float32)
                for adj_loc in adj_locs:
                    pdf[self.locs[adj_loc]] = self.error / (len(adj_locs) - 1)

                # Agent intended next state
                sn = self.locs[policy[tuple(agent_locs.values())][agent][-1]]

                # Success probability
                pdf[sn] = 1.0 - self.error

                # Transition
                agent_locs[agent] = np.random.choice(self.locs, p=pdf)

        # Printing history
        self.plot(raw_policy,
                  output + figure_template,
                  states=[hist['state_idx'] for hist in history.values()])

        # Printing history
        for h, hist in history.items():
            print h, hist

        # Creating animated GIF
        imgs = ImageList()
        for h, hist in history.items():
            imgs.append(Image(output + figure_template % hist['state_idx']))
        imgs.animationDelayImages(anim_delay)
        imgs.writeImages(output + gif)

        # Generating HTML report
        doc, tag, text, line = Doc().ttl()
        with tag('html'):
            with tag('body'):
                with tag('p', id='main'):
                    with tag('h1'):
                        text('Simulation Results')
                    line('h2', 'Animation')
                    with tag('div', id='frame'):
                        doc.stag('img', src=gif)
                    for h, hist in history.items():
                        with tag('p', id='%d' % h):
                            line('h2', 'Iteration %d' % h)
                            doc.stag('br')
                            text('State ID: %d' % hist['state_idx'])
                            doc.stag('br')
                            text('Reward: %d' % hist['rwd'])
                            doc.stag('br')
                            for agent, loc in hist['agent_locs'].items():
                                text('Agent %s at location %s.' % (agent, loc))
                                doc.stag('br')
                            with tag('div', id='frame'):
                                doc.stag('img',
                                         src=figure_template %
                                         hist['state_idx'])

        # Storing HTML file
        result = indent(doc.getvalue())
        with open(output + html, 'w') as f:
            f.write(result)
示例#26
0
-rw-r--r-- 1 root root   49983 12月 29 21:04 ../testPIC/srcPic/srcPic.bak/rBABE1DXrCqwaqv0AADDPz_aAUY584.gif
-rw-r--r-- 1 root root   35241 12月 29 21:04 ../testPIC/srcPic/srcPic.bak/rBABE1DXrEqhu0EcAACJqRzHzsk573.gif
-rw-r--r-- 1 root root   53148 12月 29 21:04 ../testPIC/srcPic/srcPic.bak/rBABE1DXrEqyEwS2AADPnJe2Too116.gif
"""

srcGifFrame0 = "./testGif/rBABE1DX6Q3CGkx-ADQOmxGk_JA500.gif"
#srcGifFrame0 = "./testGif/rBABE1DX6Q2R4VdiABCfwP0OA-o539.gif"
srcGifFrame1 = "./testGif/rBABE1DX6Q2R4VdiABCfwP0OA-o539.gif[1]"
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')
示例#27
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"
示例#28
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
示例#29
0
-rw-r--r-- 1 root root   49983 12月 29 21:04 ../testPIC/srcPic/srcPic.bak/rBABE1DXrCqwaqv0AADDPz_aAUY584.gif
-rw-r--r-- 1 root root   35241 12月 29 21:04 ../testPIC/srcPic/srcPic.bak/rBABE1DXrEqhu0EcAACJqRzHzsk573.gif
-rw-r--r-- 1 root root   53148 12月 29 21:04 ../testPIC/srcPic/srcPic.bak/rBABE1DXrEqyEwS2AADPnJe2Too116.gif
"""

srcGifFrame0 = "./testGif/rBABE1DX6Q3CGkx-ADQOmxGk_JA500.gif"
#srcGifFrame0 = "./testGif/rBABE1DX6Q2R4VdiABCfwP0OA-o539.gif"
srcGifFrame1 = "./testGif/rBABE1DX6Q2R4VdiABCfwP0OA-o539.gif[1]"
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' )