python - 我想通过在 python 中传递元组列表来创建图像
问题描述
import numpy as np
from PIL import Image
img=Image.open("Car.jpg")
array= np.array(img)
a = array
rgb=250,0,0
for i in range(768):
for j in range(500):
a[ i ] [ j ]=rgb
new_image=Image.fromarray(array)
new_image.save( "new.jpg" )
new_image.show()
我想传递像元组列表这样的 rgb 值:
rgb = [(255,0,0),
(198, 252, 247), (255, 255, 250) ,(254, 253, 248) ,(251, 252, 246), (247, 248, 240) ...
(100, 144, 247), (255, 200, 250), (254, 253,0)]
和 rgb 值随 i 和 j 变化
a [ i ] [ j ] = rgb
解决方案
您可以使用getdata()获得 RGB 。然后你可以自由地操纵每个像素。例如,您想要翻转图像。
from PIL import Image
# Resize for minimising pixels
width, height = 300, 200
src_image = ImageCall('cat.jpg').get_img()
src_image = src_image.resize((width, height), Image.ANTIALIAS)
# Get pixels in a list
pixels = list(src_image.getdata())
pixels = list(map(lambda i: i[::-1], pixels[:])) # Flipping
# pixels = [(r,g,b), (r,g,b), (r,g,b), (r,g,b), ..... n]
# Add new pixels to construct a new image
dst_image = Image.new('RGB', (width, height))
dst_image.putdata(pixels) # Place pixels in the new image.
dst_image.save('result.png') # Save the new image.
结果:
推荐阅读
- dynamics-crm - 如何根据文本字段填充查找字段 - Dynamics 365
- spring - java.lang.IllegalStateException:节点没有数据类型:org.hibernate.hql.internal.ast.tree.IdentNode
- arrays - Scala如何从数组转换为JSON格式
- php - 如何使用 codeigniter 查询获得每个产品的平均评分?
- django - django 根据日期列表过滤查询集并获取每天的计数
- javascript - 来自 compose 函数的 Javascript Reduce
- reactjs - React Native Square 进度条等价物
- flutter - 无法在颤动的列表视图中滚动交错的网格视图
- ios - UIAlertController + UIProgressView + macCatalyst
- bash - 在循环中更改文件路径输出,在 shell 脚本中