Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)))
like image 532
JanneI Avatar asked Dec 05 '25 20:12

JanneI


1 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))
like image 200
Alexander Avatar answered Dec 08 '25 10:12

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!