Ask Your Question

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

asked 2018-02-06 06:31:41 -0500

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(
            help="Extract the faces from a pictures.",
            epilog="Questions and feedback: \
    def process(self):
        extractor_name = "Align" # TODO Pass as argument
        extractor = PluginLoader.get_extractor(extractor_name)()
            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! :)

edit retag flag offensive close merge delete


"I want to add parameter interpolation=cv2.INTER_NEAREST to function cv2.imwrite" -- why do you think, there is any interpolation involved here ?

berak gravatar imageberak ( 2018-02-06 06:47:36 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-02-06 07:59:18 -0500

berak gravatar image

have a look at the docs , and trust your python, -- imwrite() takes a filename, an image and an (optional) array of compression flags, that's it. there is no interpolation here at all

edit flag offensive delete link more


I've readed docs a lot, but my level is not that high that i can use it. Can you please write how i can add interpolation=CV_INTER_LINEAR to my code or this is impossible? Big thanks in advance mate!

JaneFarrow gravatar imageJaneFarrow ( 2018-02-06 08:17:13 -0500 )edit

you can't it's impossible. and again, why do you think it is useful/nessecary ? and where did you find any hints, it should be so ?

interpolation is only used, when you change the size of the image, and imwrite() does not do this.

berak gravatar imageberak ( 2018-02-06 08:20:54 -0500 )edit

I need it to improve resized image quality.

original string was cv2.imwrite(str(output_file) + str(idx) + Path(filename).suffix, resized_image) I've added [int(cv2.IMWRITE_JPEG_QUALITY), 100]to improve IMWRITE_JPEG_QUALITY to 100%. And now i want also improve quality of interpolation. By default interpolation it's a INTER_LINEAR But i want switch it to CV_INTER_CUBIC.

JaneFarrow gravatar imageJaneFarrow ( 2018-02-06 08:27:21 -0500 )edit

the legit compression flags are here .

and again interpolation is only relevant with resize and such, while imwrite() does not do any of it.

berak gravatar imageberak ( 2018-02-06 08:44:51 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-02-06 06:31:41 -0500

Seen: 491 times

Last updated: Feb 06 '18