简体   繁体   English

为什么不能将Image.putpixel()视为更改大量像素颜色的好选择?

[英]why is Image.putpixel() not considered a good option for changing extensive pixel colors?

The documentation of Image.putpixel() states:- Image.putpixel()的文档指出:

Modifies the pixel at the given position. 在给定位置修改像素。 The color is given as a single numerical value for single-band images, and a tuple for multi-band images. 对于单波段图像,颜色作为单个数值给出,对于多波段图像,颜色作为元组给出。

Note that this method is relatively slow. 请注意,此方法相对较慢。 For more extensive changes, use paste() or the ImageDraw module instead. 要进行更广泛的更改,请改用paste()ImageDraw模块。

Clearly, the documentation states that This method is relatively slow , and if the number of changes in the image are large one should consider other methods. 显然,文档指出此方法相对较慢 ,如果图像中的更改数量很大,则应考虑其他方法。

But there doesn't exists a clear reason, why? 但是没有明确的原因,为什么?

I tried to do a source code analysis of the putpixel() method, but was unable to deduce anything. 我试图对putpixel()方法进行源代码分析,但无法推断出任何内容。

SOURCE OF putpixel() :- 来源putpixel() :-

def putpixel(self, xy, value):

    if self.readonly:
        self._copy()
    self.load()

    if self.pyaccess:
        return self.pyaccess.putpixel(xy, value)

    if self.mode == "P" and isinstance(value, (list, tuple)) and len(value) in [3, 4]:
        # RGB or RGBA value for a P image
        value = self.palette.getcolor(value)
    return self.im.putpixel(xy, value)

PS:- I was wondering whether Image.getpixel() (opposite method of putpixel() ) suffers the same drawbacks too? PS: -我想知道是否Image.getpixel()相对的方法putpixel()遭受同样的缺点吗? As it has not been mentioned in its documentation. 由于它的文档中没有提到。

If you want six apples, it's way faster to go to the store once and buy six apples, than to go to the store six times and buy a single apple each time. 如果您要六个苹果,一次去商店购买六个苹果比一次去商店六次并每次购买一个苹果要快得多。 The same is true for setting pixels. 设置像素也是如此。

Look at what the putpixel method is doing: 看一下putpixel方法在做什么:

  1. It does various checks like whether the image is read-only, whether the implementation uses pyaccess, and which color mode the image uses 它会进行各种检查,例如图像是否为只读图像,实现是否使用pyaccess以及图像使用哪种颜色模式。
  2. It may check and verify the type and dimension of the pixel data 它可以检查并验证像素数据的类型和尺寸
  3. self.im.putpixel undoubtedly does the same to the coordinates, and multiples to find the offset self.im.putpixel毫无疑问对坐标做同样的self.im.putpixel ,并且self.im.putpixel倍数来找到偏移量
  4. Finally it can do the actual operation: setting four bytes worth of pixel data. 最后,它可以执行实际的操作:设置四个字节的像素数据。

That's dozen of operations, property accesses and method calls for a very small operation. 那是许多操作,属性访问和方法调用,它们仅需很小的操作。 If you call putpixel multiple times, it repeats all those operations every single time. 如果多次调用putpixel ,它将每次重复所有这些操作。

If you ask it to do multiple pixels at the same time, it can skip steps 1-3 for the next pixels because it'll be the same. 如果您要求它同时做多个像素,则可以跳过下一个像素的步骤1-3,因为它是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM