'Copy images using the PIL.
The method tries to read and write the files with the PIL,
converting the format and resizing the image if necessary/possible.'

def copy_image_files_pil(self):

    ensuredir(path.join(self.outdir, self.imagedir))
    for src in status_iterator(self.images, 'copying    images...    ', 'brown', len(self.images), self.app.verbosity):
        dest = self.images[src]
        try:
            img = Image.open(path.join(self.srcdir, src))
        except IOError:
            if (not self.is_vector_graphics(src)):
                logger.warning('cannot    read    image    file    %r:    copying    it    instead', path.join(self.srcdir, src))
            try:
                copyfile(path.join(self.srcdir, src), path.join(self.outdir, self.imagedir, dest))
            except (IOError, OSError) as err:
                logger.warning('cannot    copy    image    file    %r:    %s', path.join(self.srcdir, src), err)
            continue
        if self.config.epub_fix_images:
            if (img.mode in ('P',)):
                img = img.convert()
        if (self.config.epub_max_image_width > 0):
            (width, height) = img.size
            nw = self.config.epub_max_image_width
            if (width > nw):
                nh = ((height * nw) / width)
                img = img.resize((nw, nh), Image.BICUBIC)
        try:
            img.save(path.join(self.outdir, self.imagedir, dest))
        except (IOError, OSError) as err:
            logger.warning('cannot    write    image    file    %r:    %s', path.join(self.srcdir, src), err)
