Exemplo n.º 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 )
Exemplo n.º 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)
            # 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)
Exemplo n.º 3
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
Exemplo n.º 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"
Exemplo n.º 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"
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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)
Exemplo n.º 9
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/')
Exemplo n.º 10
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"
Exemplo n.º 11
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)
Exemplo n.º 12
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
Exemplo n.º 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
Exemplo n.º 14
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"