使用python编辑jpg中的rgb值 [英] edit rgb values in a jpg with python

查看:393
本文介绍了使用python编辑jpg中的rgb值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python Imaging Library更改照片中的RGB值。我一直在使用函数Image.point,它做我想要的,除了我想能够在R G和B值上实现不同的功能。有人知道我怎么做吗?

I am trying to change the RGB values in a photo with the Python Imaging Library. I have been using the function Image.point and it does what I want except I want to be able to implement a different function on the R the G and the B values. Anyone know how I can do this?

谢谢!

推荐答案

除了PIL之外,你最好还是使用 numpy 来计算图像各个波段的数学。

You're better off using numpy in addition to PIL for doing math of the individual bands of an image.

作为一个人为的例子 not 意味着以任何方式表现良好:

As a contrived example that is not meant to look good in any way:

import Image
import numpy as np

im = Image.open('snapshot.jpg')

# In this case, it's a 3-band (red, green, blue) image
# so we'll unpack the bands into 3 separate 2D arrays.
r, g, b = np.array(im).T

# Let's make an alpha (transparency) band based on where blue is < 100
a = np.zeros_like(b)
a[b < 100] = 255

# Random math... This isn't meant to look good...
# Keep in mind that these are unsigned 8-bit integers, and will overflow.
# You may want to convert to floats for some calculations.
r = (b + g) * 5

# Put things back together and save the result...
im = Image.fromarray(np.dstack([item.T for item in (r,g,b,a)]))

im.save('output.png')

输入

输出

这篇关于使用python编辑jpg中的rgb值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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