Python-PIL并行(平行映射)

2024-09-26 18:18:26 发布

您现在位置:Python中文网/ 问答频道 /正文

{a pulle{a color}将一个新的像素映射成一个复杂的像素(让它把一个新的像素映射成一个新的像素,并把它作为一个新的颜色映射,使之与一个新的rgb值兼容):

import random


def rand_rgb(*_) -> tuple:
    return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)

我想做的是使用一些for(最好)高级并行性,比如multiprocessing

^{pr2}$

如果pixelaccess是一个“普通”的Python矩阵,意味着一个列表列表,那么这种类型的东西就可以工作了,图像中的每一行像素都将被单独处理,但是这并不完全有效,因为据我所见,pixel access object不是列表列表。在

我该如何进行并行化呢?在


Tags: import列表forreturn颜色defrandomrgb
1条回答
网友
1楼 · 发布于 2024-09-26 18:18:26

您可以始终使用numpy:

%matplotlib inline 
import matplotlib.pyplot as plt
import numpy as np

with PIL.Image.open(imgname) as im:
    width, height = im.size
    npim = np.array(im.getdata()).reshape(width, height, 3)

    pixelaccess = multiprocessing.Pool().map(row_rand_rgb, npim)

    new_im = np.array(pixelaccess, dtype=np.float32).reshape(width, height, 3)

plt.imshow(new_im)

这是我得到的1050x700输入图像:

enter image description here

相关问题 更多 >

    热门问题