使用Python图像库模块反转图像

发布于 2024-09-01 22:10:17 字数 110 浏览 5 评论 0原文

我有兴趣学习如何使用 python 图像库模块反转(制作负片)图像。

但是,我无法使用 ImageOps 函数“反转”。我需要另一个解决方案,使用 RGB 值。我已经搜索并尝试过但无济于事。

I'm interested in learning how to invert (make a negative of) an image using the python image libary module.

I cannot however, use the ImageOps function 'invert.' I need another solution, using the RGB values. I've searched and tried to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩心态 2024-09-08 22:10:17

只需用 255(或最大值)减去每个 RGB 值即可获得新的 RGB 值。
这篇文章告诉你如何从图片中获取 RBG 值。

Just subtract each RGB value from 255 (or the max) to obtain the new RGB values.
this post tells you how to get the RBG values from a picture.

〃温暖了心ぐ 2024-09-08 22:10:17

一种明显的方法是使用 Image.getpixel 和 Image.putpixel,对于 RGB,每个应该是三个整数的元组。你可以得到(255-r, 255-g, 255-b),然后把它放回去。

或者使用 pix = Image.load(),这看起来更快。

或者,如果您查看 ImageOps.py,它会使用查找表(lut 列表)将图像映射到倒置图像。

或者,如果这不违反您的作业规则,您可以使用 Numpy。然后你可以使用更快的矩阵运算。

One obvious way is to use Image.getpixel and Image.putpixel, for RGB, each should be a tuple of three integers. You can get (255-r, 255-g, 255-b), then put it back.

Or use pix = Image.load(), which seems faster.

Or if you look into ImageOps.py, it's using a lookup table (lut list) to map an image to an inverted one.

Or if it's not against the rules of your assignment, you may use Numpy. Then you can use the faster matrix operation.

蔚蓝源自深海 2024-09-08 22:10:17

如果您正在使用 media 模块,那么您可以执行以下操作:

import media
def invert():
    filename = media.choose_file()    # opens a select file dialog
    pic = media.load_picture(filename)    # converts the picture file into a "picture" as recognized by the module.
    for pixel in pic:
        media.set_red(pixel, 255-media.get_red(pixel))    # the inverting algorithm as suggested by @Dingle
        media.set_green(pixel, 255-media.get_green(pixel))
        media.set_blue(pixel, 255-media.get_blue(pixel))
print 'Done!'

如果您使用 picture 模块,则过程类似,如下所示:

import picture
def invert():
    filename = picture.pick_a_file()    # opens a select file dialog
    pic = picture.make_picture(filename)    # converts the picture file into a "picture" as recognized by the module.
    for pixel in picture.get_pixels(pic):
        picture.set_red(pixel, 255-picture.get_red(pixel))    # the inverting algorithm as suggested by @Dingle
        picture.set_green(pixel, 255-picture.get_green(pixel))
        picture.set_blue(pixel, 255-picture.get_blue(pixel))
print 'Done!'

希望这会有所帮助

If you are working with the media module, then you could do this:

import media
def invert():
    filename = media.choose_file()    # opens a select file dialog
    pic = media.load_picture(filename)    # converts the picture file into a "picture" as recognized by the module.
    for pixel in pic:
        media.set_red(pixel, 255-media.get_red(pixel))    # the inverting algorithm as suggested by @Dingle
        media.set_green(pixel, 255-media.get_green(pixel))
        media.set_blue(pixel, 255-media.get_blue(pixel))
print 'Done!'

The process is similar if you are using the picture module, and looks like this:

import picture
def invert():
    filename = picture.pick_a_file()    # opens a select file dialog
    pic = picture.make_picture(filename)    # converts the picture file into a "picture" as recognized by the module.
    for pixel in picture.get_pixels(pic):
        picture.set_red(pixel, 255-picture.get_red(pixel))    # the inverting algorithm as suggested by @Dingle
        picture.set_green(pixel, 255-picture.get_green(pixel))
        picture.set_blue(pixel, 255-picture.get_blue(pixel))
print 'Done!'

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文