How to pass additional arguments to a function when using ThreadPoolExecutor?

Question:

I would like to read several png images by utilizing the ThreadPoolExecutor and cv2.imread.

Problem is that I don’t know where to place cv2.IMREAD_UNCHANGED tag/argument to preserve alpha channel (transparency).

The following code works but alpha channel is lost. Where should I place the cv2.IMREAD_UNCHANGED argument?

import cv2
import concurrent.futures
images=["pic1.png", "pic2.png", "pic3.png"]
images_list=[]
with concurrent.futures.ThreadPoolExecutor() as executor:
    images_list=list(executor.map(cv2.imread,images))

For example, the following return an error:
SystemError: <built-in function imread> returned NULL without setting an error

import cv2
import concurrent.futures
images=["pic1.png", "pic2.png", "pic3.png"]
images_list=[]
with concurrent.futures.ThreadPoolExecutor() as executor:
    images_list=list(executor.map(cv2.imread(images,cv2.IMREAD_UNCHANGED)))
Asked By: JanneI

||

Answers:

Use a lambda that accepts one argument img and pass the argument to the imread function along with the cv2.IMREAD_UNCHANGED.


import cv2
import concurrent.futures
images=["pic1.png", "pic2.png", "pic3.png"]
images_list=[]
with concurrent.futures.ThreadPoolExecutor() as executor:
    images_list=list(executor.map(lambda img: cv2.imread(img, cv2.IMREAD_UNCHANGED),images))
Answered By: Alexander