Beispiel #1
0
    def select_subimage(self, x0, y0, x1, y1):
        """
        cut subimage from original image
        """
        # draw selected rectangle on the image
        imgS = image(self.work_dir + 'input_0.png')
        imgS.draw_line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)], 
                       color="red")
        imgS.draw_line([(x0+1, y0+1), (x1-1, y0+1), (x1-1, y1-1), (x0+1, y1-1), 
                       (x0+1, y0+1)], color="white")
        imgS.save(self.work_dir + 'input_0s.png')
        # crop the image
        # try cropping from the original input image (if different from input_0)
        im0 = image(self.work_dir + 'input_0.orig.png')
        dx0 = im0.size[0]
        img = image(self.work_dir + 'input_0.png')
        dx = img.size[0]
        if (dx != dx0) :
            z = float(dx0)/float(dx)
            im0.crop((int(x0*z), int(y0*z), int(x1*z), int(y1*z)))
            # resize if cropped image is too big
            if self.input_max_pixels and prod(im0.size) > self.input_max_pixels:
                im0.resize(self.input_max_pixels, method="antialias")
            img = im0
        else :
            img.crop((x0, y0, x1, y1))
	# save result
        img.save(self.work_dir + 'input_0.sel.png')
        return
Beispiel #2
0
 def select_subimage(self, x0, y0, x1, y1):
     """
     cut subimage from original image
     """
     # draw selected rectangle on the image
     imgS = image(self.work_dir + 'input_0.png')
     imgS.draw_line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
                    color="red")
     imgS.draw_line([(x0 + 1, y0 + 1), (x1 - 1, y0 + 1), (x1 - 1, y1 - 1),
                     (x0 + 1, y1 - 1), (x0 + 1, y0 + 1)],
                    color="white")
     imgS.save(self.work_dir + 'input_0s.png')
     # crop the image
     # try cropping from the original input image
     # (if different from input_1)
     im0 = image(self.work_dir + 'input_0.orig.png')
     dx0 = im0.size[0]
     img = image(self.work_dir + 'input_0.png')
     dx = img.size[0]
     if (dx != dx0):
         z = float(dx0) / float(dx)
         im0.crop((int(x0 * z), int(y0 * z), int(x1 * z), int(y1 * z)))
         # resize if cropped image is too big
         if (self.input_max_pixels
                 and prod(im0.size) > self.input_max_pixels):
             im0.resize(self.input_max_pixels, method="antialias")
         img = im0
     else:
         img.crop((x0, y0, x1, y1))
     # save result
     img.save(self.work_dir + 'input_0.sel.png')
     return
Beispiel #3
0
    def rectangle(self, action=None, x=None, y=None, x0=None, y0=None):
        """
        select a rectangle in the image
        """
        if action == 'run':
            # use the whole image
            img = image(self.work_dir + 'input_0.png')
            img.save(self.work_dir + 'input' + self.input_ext)
            img.save(self.work_dir + 'input.png')
            # go to the wait page, with the key
            http.redir_303(self.base_url + "wait?key=%s" % self.key)
            return
        else:
            # use a part of the image
            if x0 == None:
                # first corner selection
                x = int(x)
                y = int(y)
                # draw a cross at the first corner
                img = image(self.work_dir + 'input_0.png')
                img.draw_cross((x, y), size=4, color="white")
                img.draw_cross((x, y), size=2, color="red")
                img.save(self.work_dir + 'input.png')
                return self.tmpl_out("params.html", x0=x, y0=y)
            else:
                # second corner selection
                x0 = int(x0)
                y0 = int(y0)
                x1 = int(x)
                y1 = int(y)
                # reorder the corners
                (x0, x1) = (min(x0, x1), max(x0, x1))
                (y0, y1) = (min(y0, y1), max(y0, y1))
                assert (x1 - x0) > 0
                assert (y1 - y0) > 0
                # draw selected rectangle on the image
                imgS = image(self.work_dir + 'input_0.png')
                imgS.draw_line([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)], color="red")
                imgS.draw_line([(x0+1, y0+1), (x1-1, y0+1), (x1-1, y1-1), (x0+1, y1-1), (x0+1, y0+1)], color="white")
                imgS.save(self.work_dir + 'input_0s.png')
                # crop the image
		# try cropping from the original input image (if different from input_0)
		im0 = image(self.work_dir + 'input_0.orig.png')
		(dx0, dy0) = im0.size
                img = image(self.work_dir + 'input_0.png')
                (dx, dy) = img.size
		if (dx != dx0) :
                    z=float(dx0)/float(dx)
                    im0.crop((int(x0*z), int(y0*z), int(x1*z), int(y1*z)))
                    # resize if cropped image is too big
                    if self.input_max_pixels and prod(im0.size) > (self.input_max_pixels):
                        im0.resize(self.input_max_pixels, method="antialias")
                    img=im0
                    # im0.save(self.work_dir + 'input.png')
                    # img = image(self.work_dir + 'input.png')
		else :
                    img.crop((x0, y0, x1, y1))
                # zoom the cropped area
                (dx, dy) = img.size
                if (dx < 400) and (dy < 400) :
                    if dx > dy :
                        dy = int(float(dy) / float(dx) * 400)
                        dx = 400
                    else :
                        dx = int(float(dx) / float(dy) * 400)
                        dy = 400
                    img.resize((dx, dy), method="bilinear")
                img.save(self.work_dir + 'input' + self.input_ext)
                img.save(self.work_dir + 'input.png')
                # go to the wait page, with the key
                http.redir_303(self.base_url + "wait?key=%s" % self.key)
            return
Beispiel #4
0
    def input_upload(self, **kwargs):
        """
        use the uploaded input images
        """
        self.new_key()
        self.init_cfg()

        ra = self.upload_given_file('a', kwargs['file_a'])
        rb = self.upload_given_file('b', kwargs['file_b'])
        rt = self.upload_given_file('t.tiff', kwargs['file_t'])

        if not ra:
            return self.error(errcode='badparams',
                         errmsg='(MISSING FIRST IMAGE)')
        if not rb:
            return self.error(errcode='badparams',
                         errmsg='(MISSING SECOND IMAGE)')

        try:
            ima = image(self.work_dir + 'a')
            imb = image(self.work_dir + 'b')
        except IOError:
            return self.error(errcode='badparams',
                     errmsg='(INPUT IMAGE FORMAT ERROR)')

        msg = "no message"


        if self.input_max_pixels and prod(ima.size) > (self.input_max_pixels):
            ima.resize(self.input_max_pixels)
            imb.resize(self.input_max_pixels)
            msg = """The image has been resized
                  for a reduced computation time."""

        if ima.size[0] != imb.size[0] or ima.size[1] != imb.size[1]:
            ss = "%dx%d - %dx%d" % (ima.size[0], ima.size[1],
                           imb.size[0], imb.size[1])
            return self.error(errcode='badparams',
             errmsg='(INPUT IMAGES MUST HAVE THE SAME SIZE %s)'%ss)

        if len(ima.im.getbands()) != len(imb.im.getbands()):
            return self.error(errcode='badparams',
             errmsg='(DO NOT MIX COLOR AND GRAY)')
        if rt:
            sizetruth = subprocess.Popen([self.bin_dir + 'imprintf',
                  '%w %h %c' , self.work_dir + 't.tiff'],
                  stdout=subprocess.PIPE).communicate()[0]
            ts = [int(f) for f in sizetruth.split()]
            if len(ts) != 3 or ts[2] != 2:
                return self.error(errcode='badparam',
                      errmsg='(CAN NOT READ GROUND TRUTH FILE)')
            if ts[0] != ima.size[0] or ts[1] != ima.size[1]:
                return self.error(errcode='badparams',
                  errmsg='(GROUND TRUTH IMAGE HAS DIFFERENT SIZE)')

        ima.save(self.work_dir + 'a.png')
        imb.save(self.work_dir + 'b.png')

        self.log("input uploaded")
        self.cfg['meta']['original'] = True
        self.cfg['meta']['height'] = image(self.work_dir + '/a.png').size[1]
        self.cfg['meta']['hastruth'] = rt

        self.cfg.save()
        # jump to the params page
        return self.params(msg=msg, key=self.key)
Beispiel #5
0
    def input_upload(self, **kwargs):
        """
        use the uploaded input images
        """
        self.new_key()
        self.init_cfg()

        ra = self.upload_given_file('a', kwargs['file_a'])
        rb = self.upload_given_file('b', kwargs['file_b'])
        rt = self.upload_given_file('t.tiff', kwargs['file_t'])

        if not ra:
            return self.error(errcode='badparams',
                              errmsg='(MISSING FIRST IMAGE)')
        if not rb:
            return self.error(errcode='badparams',
                              errmsg='(MISSING SECOND IMAGE)')

        try:
            ima = image(self.work_dir + 'a')
            imb = image(self.work_dir + 'b')
        except IOError:
            return self.error(errcode='badparams',
                              errmsg='(INPUT IMAGE FORMAT ERROR)')

        msg = "no message"

        if self.input_max_pixels and prod(ima.size) > (self.input_max_pixels):
            ima.resize(self.input_max_pixels)
            imb.resize(self.input_max_pixels)
            msg = """The image has been resized
                  for a reduced computation time."""

        if ima.size[0] != imb.size[0] or ima.size[1] != imb.size[1]:
            ss = "%dx%d - %dx%d" % (ima.size[0], ima.size[1], imb.size[0],
                                    imb.size[1])
            return self.error(
                errcode='badparams',
                errmsg='(INPUT IMAGES MUST HAVE THE SAME SIZE %s)' % ss)

        if len(ima.im.getbands()) != len(imb.im.getbands()):
            return self.error(errcode='badparams',
                              errmsg='(DO NOT MIX COLOR AND GRAY)')
        if rt:
            sizetruth = subprocess.Popen(
                [
                    self.bin_dir + 'imprintf', '%w %h %c',
                    self.work_dir + 't.tiff'
                ],
                stdout=subprocess.PIPE).communicate()[0]
            ts = [int(f) for f in sizetruth.split()]
            if len(ts) != 3 or ts[2] != 2:
                return self.error(errcode='badparam',
                                  errmsg='(CAN NOT READ GROUND TRUTH FILE)')
            if ts[0] != ima.size[0] or ts[1] != ima.size[1]:
                return self.error(
                    errcode='badparams',
                    errmsg='(GROUND TRUTH IMAGE HAS DIFFERENT SIZE)')

        ima.save(self.work_dir + 'a.png')
        imb.save(self.work_dir + 'b.png')

        self.log("input uploaded")
        self.cfg['meta']['original'] = True
        self.cfg['meta']['height'] = image(self.work_dir + '/a.png').size[1]
        self.cfg['meta']['hastruth'] = rt

        self.cfg.save()
        # jump to the params page
        return self.params(msg=msg, key=self.key)