为什么不将Image.putpixel()视为更改大量像素颜色的好选择? [英] why is Image.putpixel() not considered a good option for changing extensive pixel colors?

查看:430
本文介绍了为什么不将Image.putpixel()视为更改大量像素颜色的好选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

请注意,此方法相对较慢.要进行更广泛的更改,请改用paste()ImageDraw模块.

Note that this method is relatively slow. For more extensive changes, use paste() or the ImageDraw module instead.

很明显,文档指出此方法相对,如果图像中的更改数量很大,则应考虑其他方法.

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?

我试图对putpixel()方法进行源代码分析,但无法推断出任何内容.

I tried to do a source code analysis of the putpixel() method, but was unable to deduce anything.

putpixel()的来源:-

SOURCE OF 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)

P.S.:-我想知道Image.getpixel()(与putpixel()相反的方法)是否也遭受相同的缺点吗?正如其文档中未提到的那样.

P.S.:- I was wondering whether Image.getpixel() (opposite method of putpixel()) suffers the same drawbacks too? 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.

看看putpixel方法在做什么:

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

这需要大量的操作,属性访问和方法调用.如果您多次调用putpixel,它将每次重复执行所有这些操作.

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.

如果您要求它同时做多个像素,则可以跳过下一个像素的步骤1-3,因为它是相同的.

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.

这篇关于为什么不将Image.putpixel()视为更改大量像素颜色的好选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆