使用Python中的PIL或OpenCV将图像以不透明性更改的两个给定坐标粘贴到另一个图像 [英] Paste an image to another image at two given co-ordinates with altered opacity using PIL or OpenCV in Python

查看:113
本文介绍了使用Python中的PIL或OpenCV将图像以不透明性更改的两个给定坐标粘贴到另一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张具有给定点的图像,每张图像一个点,需要对齐,以便结果图像是两个图像的总和,而图像2则以40%的不透明度粘贴到图像1上.我已经接受了这个

图片2:

最终结果(所需输出):

为此,我尝试了PIL的 img.paste()函数,并替换了cv2中的numpy图片数组中的值,两者都给出了远非期望的结果.

解决方案

我使用 ImageMagick 制作了两个输入图像,如下所示:

  magick -size 300x400 xc:``rgb(1,204,255)''-填充红色-绘制点280,250";1.pngmagick-尺寸250x80 xc:"rgb(150,203,0)"-填充红色-绘制点12,25";2.png 

然后运行以下代码:

 #!/usr/bin/env python3"将一个图像粘贴到另一个图像上,以使每个图像中的给定点重合."从PIL导入图像#打开图像并确保RGBim1 = Image.open('1.png').convert('RGB')im2 = Image.open('2.png').convert('RGB')每个图像中点的#x,y坐标p1x,p1y = 280,250p2x,p2y = 12,25#算出我们需要在新图像的公共点左,右,上,下多少像素的空间pL =最大值(p1x,p2x)pR =最大值(im1.width-p1x,im2.width-p2x)pT =最大值(p1y,p2y)pB = max(im1.height-p1y,im2.height-p2y)#创建纯白色背景bg = Image.new('RGB',(pL + pR,pT + pB),'white')bg.save('DEBUG-bg.png')#将im1粘贴到背景上bg.paste(im1,(pL-p1x,pT-p1y))bg.save('DEBUG-bg + im1.png')#为im2制作40%不透明的蒙版alpha = Image.new('L',(im2.width,im2.height),int(40 * 255/100))alpha.save('DEBUG-alpha.png')#将im2粘贴到带有Alpha的背景上bg.paste(im2,(pL-p2x,pT-p2y),alpha)bg.save('result.png') 

结果是这样的:

使用以"DEBUG-xxx.png" 开头的名称保存图像的行仅是为了易于调试,可以将其删除.我可以轻松查看所有内容以查看代码的状态,也可以通过删除"DEBUG * png" 轻松删除所有内容.

I have two images with given points, one point each image, that need to be aligned so that the result image is a summation of both images, while image 2 is pasted on image 1 with 40% opacity. I have taken this question into consideration but our case does not exactly match as the image co-ordinate is supplied by user and images can have wide range of sizes.

Image 1:

Image2:

Final result(desired output):

For this I have tried img.paste() function of PIL and replacing values in numpy array of images in cv2, both giving results that are far from desired.

解决方案

I made two input images with ImageMagick like this:

magick -size 300x400 xc:"rgb(1,204,255)" -fill red -draw "point 280,250" 1.png
magick -size 250x80  xc:"rgb(150,203,0)" -fill red -draw "point 12,25"   2.png

Then ran the following code:

#!/usr/bin/env python3
"""
Paste one image on top of another such that given points in each are coincident.
"""

from PIL import Image

# Open images and ensure RGB
im1 = Image.open('1.png').convert('RGB')
im2 = Image.open('2.png').convert('RGB')

# x,y coordinates of point in each image
p1x, p1y = 280, 250
p2x, p2y = 12, 25

# Work out how many pixels of space we need left, right, above, below common point in new image
pL = max(p1x, p2x)
pR = max(im1.width-p1x,  im2.width-p2x)
pT = max(p1y, p2y)
pB = max(im1.height-p1y, im2.height-p2y)

# Create background in solid white
bg = Image.new('RGB', (pL+pR, pT+pB),'white')
bg.save('DEBUG-bg.png')

# Paste im1 onto background
bg.paste(im1, (pL-p1x, pT-p1y))
bg.save('DEBUG-bg+im1.png')

# Make 40% opacity mask for im2
alpha = Image.new('L', (im2.width,im2.height), int(40*255/100))
alpha.save('DEBUG-alpha.png')

# Paste im2 over background with alpha
bg.paste(im2, (pL-p2x, pT-p2y), alpha)
bg.save('result.png')

The result is this:

The lines that save images with names starting "DEBUG-xxx.png" are just for easy debugging and can be removed. I can easily view them all to see what is going on with the code and I can easily delete them all by removing "DEBUG*png".

这篇关于使用Python中的PIL或OpenCV将图像以不透明性更改的两个给定坐标粘贴到另一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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