python - 使用 PIL 进行 RGB 处理 - 如何获取单个图像并生成具有不同 RGB 值的图像?
问题描述
我想拍一张照片,然后把它变成我添加到列表中的 9 张不同的图像。我想拍摄这张单张图像,然后将 R 值更改为一个较大的值,然后将 R 值更改为一半,最后将 R 值更改为一个较小的值。
我正在努力让它发挥作用。我的逻辑循环似乎不起作用,我的图像并没有被修改。任何帮助表示赞赏。
from PIL import Image
# Set the file and open it
file = "Final/charlie.png"
#pic = Image.open(file)
#Convert to RGB, so we dont have to deal with the alpha channel
#pic = pic.convert('RGB')
images = []
count = 0
#Image processing for lage change
def image_processing0(a):
c = int(a / 10)
return c
#Create PixelMap
count = 0
while count <3:
pic = Image.open(file)
pic = pic.convert('RGB')
for x in range(pic.size[0]):
for y in range(pic.size[1]):
r,g,b = pic.getpixel((x,y))
#Check the count and use logic to appy the processing to the corect channel
if count == 1:
image_processing0(r)
pic.putpixel((x,y),(r,g,b))
elif count == 2:
image_processing0(g)
pic.putpixel((x,y),(r,g,b))
else:
image_processing0(b)
pic.putpixel((x,y),(r,g,b))
images.append(pic)
count+=1
解决方案
您忘记分配 to 的返回image_processing0
值r, g, b
。
改变这个:
if count == 1:
image_processing0(r)
pic.putpixel((x,y),(r,g,b))
elif count == 2:
image_processing0(g)
pic.putpixel((x,y),(r,g,b))
else:
image_processing0(b)
pic.putpixel((x,y),(r,g,b))
到:
if count == 1:
r = image_processing0(r)
pic.putpixel((x,y),(r,g,b))
elif count == 2:
g = image_processing0(g)
pic.putpixel((x,y),(r,g,b))
else:
b = image_processing0(b)
pic.putpixel((x,y),(r,g,b))
推荐阅读
- java - 使用 hadoop 纱线运行 distcp java 作业
- apache - Apache2 使用虚拟主机将 https://example.com 重定向到 https://www.example.com
- sql - SQL Conditional Where 语句在不同代码中具有 AND 逻辑,在相同代码中具有 OR 逻辑
- reactjs - react hooks回调函数需要点击两次
- pyspark - 两个数据帧的 Pyspark 联合
- ansible - 删除所有旧文件,但保留最新的 3 个相同文件名和不同路径的文件
- c++ - 代码存储器和数据存储器中存储的内容
- javascript - 如何通过按下 django 中的按钮来获取 div 的值
- r - 如何制作一个函数来计算一列中“1”的流行率只包含“0”和“1”并制作图表?
- c++ - 创建自己的(管道)范围 ::views 和 ::actions 的规则是什么?