Ask Your Question

Revision history [back]

Python error: imwrite() takes at most 3 arguments (4 given)

I'm facing with problem in code:

import cv2
from pathlib import Path
from lib.cli import DirectoryProcessor
from plugins.PluginLoader import PluginLoader`
class ExtractTrainingData(DirectoryProcessor):
    def create_parser(self, subparser, command, description):
        self.parser = subparser.add_parser(
            command,
            help="Extract the faces from a pictures.",
            description=description,
            epilog="Questions and feedback: \
            https://github.com/deepfakes/faceswap-playground"
        )
    def process(self):
        extractor_name = "Align" # TODO Pass as argument
        extractor = PluginLoader.get_extractor(extractor_name)()
        try:
            for filename in self.read_directory():
                image = cv2.imread(filename)
                for idx, face in self.get_faces(image):
                    resized_image = extractor.extract(image, face, 256)
                    output_file = self.output_dir / Path(filename).stem
                    cv2.imwrite(str(output_file) + str(idx) + Path(filename).suffix, resized_image, [int(cv2.IMWRITE_JPEG_QUALITY), 100], interpolation=cv2.INTER_NEAREST)
        except Exception as e:
            print('Failed to extract from image: {}. Reason: {}'.format(filename, e))

I want to add parameter interpolation=cv2.INTER_NEAREST to function cv2.imwrite, but in output i'm getting Failed to extract from image: /home/Jane/Desktop/50.jpg. Reason: imwrite() takes at most 3 arguments (4 given) Any tips how to fix it? I've tried add interpolation via int

cv2.imwrite(str(output_file) + str(idx) + Path(filename).suffix, resized_image, [int(cv2.IMWRITE_JPEG_QUALITY), 100], [int(interpolation), CV_INTER_LINEAR])

but this doesn't helped me. :( According to the documentation, i can only pass three parameters. But i've see ppl doing it via 2 steps. Any ways how to fix it? Thanks guys! :)