Ask Your Question
0

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

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

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! :)

edit retag flag offensive close merge delete

Comments

"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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

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

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

Comments

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 -0600 )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 -0600 )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 https://docs.opencv.org/2.4/modules/i... But i want switch it to CV_INTER_CUBIC.

JaneFarrow gravatar imageJaneFarrow ( 2018-02-06 08:27:21 -0600 )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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,522 times

Last updated: Feb 06 '18