如何按顺序将图像放在另一个图像上?
How to put images on another image in order?
在下面的示例中,白色背景上依次显示了三张图像。如何在 python 中使用 CV2 或 PIL 或任何工作代码实现此目的。
谢谢。
图片必须根据宽高比对齐。
输入 = 3 张带 BG 的图像,
输出=单张图片如上图
更新 !!!!
每次循环只有一张图片贴在BG上。
from PIL import Image
import cv2
import numpy as np
d=0
folder = 'save'
image_paths = []
for path, subdirs, files in os.walk(folder):
for filename in files:
f = os.path.join(path, filename)
if f.endswith(".jpg"):
image_paths.append(f)
if f.endswith(".png"):
image_paths.append(f)
if f.endswith(".JPG"):
image_paths.append(f)
if f.endswith(".PNG"):
image_paths.append(f)
if f.endswith(".jpeg"):
image_paths.append(f)
if f.endswith(".JPEG"):
image_paths.append(f)
for image in image_paths:
image = cv2.imread(image)
r = 720.0 / image.shape[1]
dim = (720, int(image.shape[0] * r))
resized = cv2.resize(image, dim)
#resized = resized[:,:,0]
h, w, z = resized.shape
back = cv2.imread('template.jpg')
yoff = round((1080-h)/4)
xoff = round((1920-w)/6)
d+=1
result = back.copy()
result[yoff:yoff+h, xoff:xoff+w] = resized
#result = np.stack((result)*3)
cv2.imwrite('saves/resized_centered_%d.jpg'%d, result)
所以输入中的多张图片被粘贴到背景中,但问题是,我想将三张图片粘贴到背景中,而不是按顺序粘贴一张图片。
注意:顶部的图片仅代表我的帮助!!!你可以告诉我除此之外的任何可能!!!
这行代码将图像移到左上角并正确放置,但同样我还需要另外两张图像放置在右上角和底部。
yoff = round((1080-h)/4)
xoff = round((1920-w)/6)
我假设有这样的模板:
“最终图像”的尺寸为 (1920, 1080)
(参见您对 xoff
和 yoff
的计算)。既然你写了,你想保持每个“单张图片”的纵横比,你需要检查这两种情况:Resize w.r.t。到单个图像的宽度,如果生成的高度太大,re-resize w.r.t。到单张图片的高度。
剩下的就是跟踪循环内每个最终图像的单张图像数量,并为三种情况中的每一种设置适当的 xoff
和 yoff
值。也许,查看此处的代码比长篇解释更有帮助:
import cv2
import numpy as np
import os
folder = 'path/to/your/images'
image_paths = []
for path, subdirs, files in os.walk(folder):
for filename in files:
f = os.path.join(path, filename)
if f.endswith((".jpg", ".png", ".JPG", ".PNG", ".jpeg", ".JPEG")):
image_paths.append(f)
d = 0 # Final image counter
e = 0 # Single image counter
back = np.ones((1080, 1920, 3), np.uint8) * 255 # Background
result = back.copy() # Final image
for i, image in enumerate(image_paths):
# Read image
image = cv2.imread(image)
h, w = image.shape[:2]
# First two single images: Enforce subimage with h_max = 480 and w_max = 900
if e <= 1:
r = 900.0 / w
dim = (900, int(h * r))
if dim[1] > 480:
r = 480.0 / h
dim = (int(w * r), 480)
resized = cv2.resize(image, dim)
hr, wr = resized.shape[:2]
x_off = 40
if e == 0:
y_off = 40
else:
y_off = 560
# Third single image: Enforce subimage with h_max = 1000 and w_max = 900
else:
r = 900.0 / w
dim = (900, int(h * r))
if dim[1] > 1000:
r = 1000.0 / h
dim = (int(w * r), 1000)
resized = cv2.resize(image, dim)
hr, wr = resized.shape[:2]
x_off, y_off = 980, 40
# Add single image to final image
result[y_off:y_off + hr, x_off:x_off + wr] = resized
# Increment single image counter
e += 1
# After three single images: Write final image; start new final image
if (e == 3) or (i == (len(image_paths) - 1)):
cv2.imwrite('resized_centered_%d.jpg' % d, result)
result = back.copy()
d += 1
e = 0
对于我的 Whosebug 存档中的一些随机图像,我得到以下输出:
如果您想在单张图片周围或图片之间设置不同大小的框或边距,只需调整代码中的相应值即可。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1.1
NumPy: 1.20.2
OpenCV: 4.5.1
----------------------------------------
在下面的示例中,白色背景上依次显示了三张图像。如何在 python 中使用 CV2 或 PIL 或任何工作代码实现此目的。
谢谢。
图片必须根据宽高比对齐。
输入 = 3 张带 BG 的图像,
输出=单张图片如上图
更新 !!!!
每次循环只有一张图片贴在BG上。
from PIL import Image
import cv2
import numpy as np
d=0
folder = 'save'
image_paths = []
for path, subdirs, files in os.walk(folder):
for filename in files:
f = os.path.join(path, filename)
if f.endswith(".jpg"):
image_paths.append(f)
if f.endswith(".png"):
image_paths.append(f)
if f.endswith(".JPG"):
image_paths.append(f)
if f.endswith(".PNG"):
image_paths.append(f)
if f.endswith(".jpeg"):
image_paths.append(f)
if f.endswith(".JPEG"):
image_paths.append(f)
for image in image_paths:
image = cv2.imread(image)
r = 720.0 / image.shape[1]
dim = (720, int(image.shape[0] * r))
resized = cv2.resize(image, dim)
#resized = resized[:,:,0]
h, w, z = resized.shape
back = cv2.imread('template.jpg')
yoff = round((1080-h)/4)
xoff = round((1920-w)/6)
d+=1
result = back.copy()
result[yoff:yoff+h, xoff:xoff+w] = resized
#result = np.stack((result)*3)
cv2.imwrite('saves/resized_centered_%d.jpg'%d, result)
所以输入中的多张图片被粘贴到背景中,但问题是,我想将三张图片粘贴到背景中,而不是按顺序粘贴一张图片。
注意:顶部的图片仅代表我的帮助!!!你可以告诉我除此之外的任何可能!!!
这行代码将图像移到左上角并正确放置,但同样我还需要另外两张图像放置在右上角和底部。
yoff = round((1080-h)/4)
xoff = round((1920-w)/6)
我假设有这样的模板:
“最终图像”的尺寸为 (1920, 1080)
(参见您对 xoff
和 yoff
的计算)。既然你写了,你想保持每个“单张图片”的纵横比,你需要检查这两种情况:Resize w.r.t。到单个图像的宽度,如果生成的高度太大,re-resize w.r.t。到单张图片的高度。
剩下的就是跟踪循环内每个最终图像的单张图像数量,并为三种情况中的每一种设置适当的 xoff
和 yoff
值。也许,查看此处的代码比长篇解释更有帮助:
import cv2
import numpy as np
import os
folder = 'path/to/your/images'
image_paths = []
for path, subdirs, files in os.walk(folder):
for filename in files:
f = os.path.join(path, filename)
if f.endswith((".jpg", ".png", ".JPG", ".PNG", ".jpeg", ".JPEG")):
image_paths.append(f)
d = 0 # Final image counter
e = 0 # Single image counter
back = np.ones((1080, 1920, 3), np.uint8) * 255 # Background
result = back.copy() # Final image
for i, image in enumerate(image_paths):
# Read image
image = cv2.imread(image)
h, w = image.shape[:2]
# First two single images: Enforce subimage with h_max = 480 and w_max = 900
if e <= 1:
r = 900.0 / w
dim = (900, int(h * r))
if dim[1] > 480:
r = 480.0 / h
dim = (int(w * r), 480)
resized = cv2.resize(image, dim)
hr, wr = resized.shape[:2]
x_off = 40
if e == 0:
y_off = 40
else:
y_off = 560
# Third single image: Enforce subimage with h_max = 1000 and w_max = 900
else:
r = 900.0 / w
dim = (900, int(h * r))
if dim[1] > 1000:
r = 1000.0 / h
dim = (int(w * r), 1000)
resized = cv2.resize(image, dim)
hr, wr = resized.shape[:2]
x_off, y_off = 980, 40
# Add single image to final image
result[y_off:y_off + hr, x_off:x_off + wr] = resized
# Increment single image counter
e += 1
# After three single images: Write final image; start new final image
if (e == 3) or (i == (len(image_paths) - 1)):
cv2.imwrite('resized_centered_%d.jpg' % d, result)
result = back.copy()
d += 1
e = 0
对于我的 Whosebug 存档中的一些随机图像,我得到以下输出:
如果您想在单张图片周围或图片之间设置不同大小的框或边距,只需调整代码中的相应值即可。
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1.1
NumPy: 1.20.2
OpenCV: 4.5.1
----------------------------------------