在 OpenCV 和/或 PIL 中叠加两个图像时出错
Error when overlaying two images in OpenCV and or PIL
我尝试在 openCV 和 PIL 中叠加 openCV 中的两个图像,但无济于事。我正在使用 np.zeros
的 1000x1000x3
数组作为背景(又名黑色背景)和 this 我的显示器的随机图像,但我真的无法让它工作一些我不知道的原因。
仅尝试使用 OpenCV:(result(如果你注意,你会在中间看到几条奇怪的线和点))
base_temp = np.zeros((1000,1000,3))
foreground_temp = cv2.imread('exampleImageThatILinkedAbove.png')
base_temp[offset_y:offset_y+foreground_temp.shape[0], offset_x:offset_x+foreground_temp.shape[1]] = foreground_temp
尝试使用 PIL:(结果与 OpenCV 版本完全相同)
base_temp = cv2.convertScaleAbs(self.base) #Convert to uint8 for cvtColor
base_temp = cv2.cvtColor(base_temp, cv2.COLOR_BGR2RGB) #PIL uses RGB and OpenCV uses BGR
base_temp = Image.fromarray(base_temp) #Convert to PIL Image
foreground_temp = cv2.cvtColor(cv2.convertScaleAbs(self.last_img), cv2.COLOR_BGR2RGB)
foreground_temp = Image.fromarray(foreground_temp)
base_temp.paste(foreground_temp, offset)
我正在 python3.5 和 Windows 10 上使用 OpenCV3.4,如果有帮助的话。
我 喜欢 避免任何需要保存 cv2 图像然后将它们重新加载到另一个模块中以进行转换的解决方案,但如果这是不可避免的,那也没关系。任何帮助将不胜感激!
如果您检查 base_temp
的类型,您会看到它是 float64
,当您尝试将其另存为需要无符号 8 位的 JPEG 时,这会给您带来问题值。
因此解决方案是创建具有正确类型的 base_temp
图像:
base_temp = np.zeros((1000,1000,3), dtype=np.uint8)
完整的代码和结果如下所示:
import cv2
import numpy as np
from PIL import Image
# Make black background - not square, so it shows up problems with swapped dimensions
base_temp=np.zeros((768,1024,3),dtype=np.uint8)
foreground_temp=cv2.imread('monitor.png')
# Paste with different x and y offsets so it is clear when indices are swapped
offset_y=80
offset_x=40
base_temp[offset_y:offset_y+foreground_temp.shape[0], offset_x:offset_x+foreground_temp.shape[1]] = foreground_temp
Image.fromarray(base_temp).save('result.png')
我尝试在 openCV 和 PIL 中叠加 openCV 中的两个图像,但无济于事。我正在使用 np.zeros
的 1000x1000x3
数组作为背景(又名黑色背景)和 this 我的显示器的随机图像,但我真的无法让它工作一些我不知道的原因。
仅尝试使用 OpenCV:(result(如果你注意,你会在中间看到几条奇怪的线和点))
base_temp = np.zeros((1000,1000,3))
foreground_temp = cv2.imread('exampleImageThatILinkedAbove.png')
base_temp[offset_y:offset_y+foreground_temp.shape[0], offset_x:offset_x+foreground_temp.shape[1]] = foreground_temp
尝试使用 PIL:(结果与 OpenCV 版本完全相同)
base_temp = cv2.convertScaleAbs(self.base) #Convert to uint8 for cvtColor
base_temp = cv2.cvtColor(base_temp, cv2.COLOR_BGR2RGB) #PIL uses RGB and OpenCV uses BGR
base_temp = Image.fromarray(base_temp) #Convert to PIL Image
foreground_temp = cv2.cvtColor(cv2.convertScaleAbs(self.last_img), cv2.COLOR_BGR2RGB)
foreground_temp = Image.fromarray(foreground_temp)
base_temp.paste(foreground_temp, offset)
我正在 python3.5 和 Windows 10 上使用 OpenCV3.4,如果有帮助的话。
我 喜欢 避免任何需要保存 cv2 图像然后将它们重新加载到另一个模块中以进行转换的解决方案,但如果这是不可避免的,那也没关系。任何帮助将不胜感激!
如果您检查 base_temp
的类型,您会看到它是 float64
,当您尝试将其另存为需要无符号 8 位的 JPEG 时,这会给您带来问题值。
因此解决方案是创建具有正确类型的 base_temp
图像:
base_temp = np.zeros((1000,1000,3), dtype=np.uint8)
完整的代码和结果如下所示:
import cv2
import numpy as np
from PIL import Image
# Make black background - not square, so it shows up problems with swapped dimensions
base_temp=np.zeros((768,1024,3),dtype=np.uint8)
foreground_temp=cv2.imread('monitor.png')
# Paste with different x and y offsets so it is clear when indices are swapped
offset_y=80
offset_x=40
base_temp[offset_y:offset_y+foreground_temp.shape[0], offset_x:offset_x+foreground_temp.shape[1]] = foreground_temp
Image.fromarray(base_temp).save('result.png')