Python小作业

使用多张图片混合生成一张图片

题目要求:

学习并使用数组算术和逻辑运算, 使用下述编程结构进行编程:
import numpy as np
from PIL import  Image
……#读入imb和ima或者更多图像
ima = np.array(im)
imb = np.array(im1)

#ima和imb进行数组运算(+-*/)得到新数组, 新数组保存到imc中
imc = …
#将imc转换为图像对象并显示
imc = imc.astype(np.uint8)
imgnew = Image.fromarray(imc)
imgnew .show()

结果:展示一张你生成的图片,提交你的程序代码。

19信管二dkh

import numpy as np
from PIL import  Image
#读入imb和ima或者更多图像
img1 = Image.open( "D:/1.jpg")#路径自己修改
img1 = img1.convert('RGBA')#打开图片格式为RGBA
img2 = Image.open( "D:/3.jpg")
img2 = img2.convert('RGBA')
r, g, b, alpha = img2.split()#将图片按不同色域区分
alpha = alpha.point(lambda i: i>0 and 204)
img = Image.composite(img2, img1, alpha)#合并两张图片
img.show()
img.save("D:/blend2.png")#在D盘下保存图片
Image.open("D:/blend2.png")
#转载
#原博客:https://blog.csdn.net/guduruyu/article/details/71439733

参考答案

import numpy as np
from PIL import  Image
ima = np.array(Image.open('D:/1.jpg'))
imb = np.array(Image.open('D:/2.jpg'))
ima=ima[0:300,0:300]
imb=imb[0:300,0:300]
#ima和imb进行数组运算(+-*/)得到新数组, 新数组保存到imc中
imc = ima/2+imb/2
ima.astype('uint8')
imb.astype('uint8')
#将imc转换为图像对象并显示
imc = imc.astype(np.uint8)
imgnew = Image.fromarray(imc)
imgnew .show()
imgnew.save("D:/blend3.png")
posted @ 2020-12-14 21:23  小码A  阅读(143)  评论(0)    收藏  举报