How can i iterate over image pixels in a faster manner in python?

后端 未结 2 1298
误落风尘
误落风尘 2020-12-20 03:16

I want to modify a grayscale image in a manner so that I can change the pixel values to black for the top half of the image. I can certainly do this by iterating over in the

2条回答
  •  时光取名叫无心
    2020-12-20 03:58

    This is a very good candidate for multiprocessing the image/s. If you split the image into blocks of pixels you can very easily process the image in parallel, that is if it is sufficiently large or you are doing this on a lot of images.

    1. Break the image up into chunks defined as tuples ( top left X, top left Y, width, height )
    2. Pass tuples and image handle to various threads, hopefully in a thread pool.
    3. Wait for threads to finish and then continue using your image.

    This, depending on the image size and your pick of the number of threads and block size, can speed up your process linearly up to a point of course.

提交回复
热议问题