<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def derive(self, srcfile=None, request=None, outfile=None):
 """Do sequence of manipulations for IIIF to derive output image. Named argments: srcfile -- source image file request -- IIIFRequest object with parsed parameters outfile -- output image file. If set the the output file will be written to that file, otherwise a new temporary file will be created and outfile set to its location. See order in spec: http://www-sul.stanford.edu/iiif/image-api/#order Region THEN Size THEN Rotation THEN Quality THEN Format Typical use: m = IIIFManipulator() try: m.derive(srcfile='a.jpg',request=r) # .. serve m.outfile except IIIFError as e: # .. finally: m.cleanup() #removes temp m.outfile """

        # set if specified
        if (srcfile is not None):
            self.srcfile = srcfile
        if (request is not None):
            self.request = request
        if (outfile is not None):
            self.outfile = outfile
        if (self.outfile is not None):
            # create path to output dir if necessary
            dir = os.path.dirname(self.outfile)
            if (not os.path.exists(dir)):
                os.makedirs(dir)
        #
        self.do_first()
        (x, y, w, h) = self.region_to_apply()
        self.do_region(x, y, w, h)
        (w, h) = self.size_to_apply()
        self.do_size(w, h)
        (mirror, rot) = self.rotation_to_apply(no_mirror=True)
        self.do_rotation(mirror, rot)
        (quality) = self.quality_to_apply()
        self.do_quality(quality)
        self.do_format(self.request.format)
        self.do_last()
        return(self.outfile, self.mime_type)