做展示型网站便宜吗,广告设计公司开店策划,sql网站源码,同一源代码再建设一个网站python 版本 3.x首先安装 PIL由于PIL仅支持到Python 2.7#xff0c;加上年久失修#xff0c;于是一群志愿者在PIL的基础上创建了兼容的版本#xff0c;名字叫Pillow#xff0c;支持最新Python 3.x#xff0c;又加入了许多新特性#xff0c;因此#xff0c;我们可以直接安…python 版本 3.x首先安装 PIL由于PIL仅支持到Python 2.7加上年久失修于是一群志愿者在PIL的基础上创建了兼容的版本名字叫Pillow支持最新Python 3.x又加入了许多新特性因此我们可以直接安装使用Pillow。所以 安装pip install pillow获取像素点import numpy as npfrom PIL import Imageimg Image.open(./b.png).convert(RGBA)a_img np.asarray(img)获取的图片像素为 一个二维数组相当于是二维左边系 x ,y 然后里面存了一个元组 值分别为 r g b a分别计算改变了像素值之后就需要将数据写入到图片了这个时候就需要 matplotlibimport matplotlib.pyplot as pltplt.figure(beauty) # 开启图层名称为 beautyplt.imshow(a_img) # 二维数组的数据plt.axis(off)#plt.show()plt.savefig(./result.png)下面给出一个完整的 demo需要将两张图片合并计算并输出结果将上面两个图片合并from PIL import Imageimport numpy as npimport matplotlib.pyplot as pltdef modeSuperposition(basePixel,mixPixel,alpha):basePixel int(basePixel)mixPixel int(mixPixel);res0if basePixel 128 :res int(mixPixel) * int(basePixel) / 128;else:res 255 - (255 - mixPixel)*(255 - basePixel) / 128;a alpha / 255;if a 1:a 1res (1-a)*basePixel a*rest int(res)-256if t 0:return int(res)if res 255:return 255return 0def mergePoint(x,y):p1 img1[x][y]p2 img2[x][y]p1[1] modeSuperposition(p1[0],p2[0],p2[3])p1[2] modeSuperposition(p1[1],p2[1],p2[3])p1[3] modeSuperposition(p1[2],p2[2],p2[3])imgA Image.open(./b.png)img1np.array(imgA.convert(RGBA)) #打开图像并转化为数字矩img2np.array(Image.open(./light.png).convert(RGBA))i len(img1);j len(img1[0]);for k in range(0,len(img2)):for n in range(0,len(img2[0])):if k i and n j:mergePoint(k,n)#img Image.new(RGBA,imgA.size)###创建一个5*5的图片plt.figure(beauty) # 开启图层名称为 beautyplt.imshow(img1) # 二维数组的数据plt.axis(off)#plt.show()plt.savefig(./result.png)结果如下以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持我们。本文标题: python使用PIL和matplotlib获取图片像素点并合并解析本文地址: http://www.cppcns.com/jiaoben/python/273238.html