我正在尝试合并4个图像,图像1在左上角,图像2在右上角,图像3在左下角和图像4在右下角。但是,我的图像是不同的大小,不知道如何调整图像的大小相同。我是相当新的Python,这是我第一次使用PIL。真的很感谢任何帮助目前为止我有这个(打开图像后)
img1 = img1.resize(img2.size) img1 = img1.resize(img3.size) img1 = img1.resize(img4.size)
lsmepo6l1#
这就满足你的基本要求了。操作步骤:
代码:
import cv2 from PIL import Image from skimage import io IMAGE_WIDTH = 1920 IMAGE_HEIGHT = 1080 def create_collage(images): images = [io.imread(img) for img in images] images = [cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT)) for image in images] if len(images) > 2: half = len(images) // 2 h1 = cv2.hconcat(images[:half]) h2 = cv2.hconcat(images[half:]) concat_images = cv2.vconcat([h1, h2]) else: concat_images = cv2.hconcat(images) image = Image.fromarray(concat_images) # Image path image_name = "result.jpg" image = image.convert("RGB") image.save(f"{image_name}") return image_name images=["image1.png","image2.png","image3.png","image4.png"] # image1 on top left, image2 on top right, image3 on bottom left,image4 on bottom right create_collage(images)
创建高级学院使你可以这样看待:https://codereview.stackexchange.com/questions/275727/python-3-script-to-make-photo-collages
1条答案
按热度按时间lsmepo6l1#
这就满足你的基本要求了。
操作步骤:
代码:
创建高级学院使你可以这样看待:https://codereview.stackexchange.com/questions/275727/python-3-script-to-make-photo-collages