- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Python 将多个图像对角合并为一个图像。我查了很多问题,但没有找到与我的需求类似的东西。
我现在能做的就是简单地合并文件:
from PIL import Image
import numpy as np
img = Image.open("1.png")
background = Image.open("2.png")
background.paste(img, (0, 0), img)
background.save('result.png',"PNG")
以下是要测试的图片:
我需要将图片对角排列以适合最终的 900 x 1200 像素大小的白色背景图片。也许它们需要缩小一点尺寸并合身?至少这是我在 Photoshop 中手动完成的过程(耗时)。
有时需要 2 张图片,有时可能是 4 或 5 张。
最佳答案
这应该可以完成工作:
from PIL import Image
images = ['1.png', '2.png', '3.png']
# shift between images
offset = (200, 100)
target_size = (900, 1200)
images = [Image.open(fn) for fn in images]
no_img = len(images)
image_size = [s+no_img*o for s, o in zip(images[0].size, offset)]
#create empty background
combined_image = Image.new('RGBA', image_size)
# paste each image at a slightly shifted position, start at top right
for idx, image in enumerate(images):
combined_image.paste(image, ((no_img - idx - 1) * offset[0], idx * offset[1]), image)
# crop to non-empty area
combined_image = combined_image.crop(combined_image.getbbox())
# resizing and padding such that it fits 900 x 1200 px
scale = min(target_size[0] / combined_image.size[0], target_size[1] / combined_image.size[1])
combined_image = combined_image.resize((int(combined_image.size[0] * scale), int(combined_image.size[1] * scale)), Image.BICUBIC)
img_w, img_h = combined_image.size
finale_output = Image.new('RGB', target_size, (255, 255, 255))
offset = ((target_size[0] - img_w) // 2, (target_size[1] - img_h) // 2)
finale_output.paste(combined_image, offset, combined_image)
# display
finale_output.show()
编辑:我添加了用于调整大小和填充的代码,以便输出完全符合您想要的大小(同时保持纵横比)。
关于python - 如何使用Python将多张图片对角线合并为一张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038056/
我有一个 6x6 数组,并且希望始终获取接下来的四个值。举个例子: 0----- -1---- --2--- ---3-- ----4- 所以我想得到所有对角线的 (0+1+2+3) 和 (1+2+3
我想遍历一个已转换为一维的二维方阵。 问题是我想遍历它,就像我在对角条中遍历原始 2D 一样。 该数组是对角数组,我最初使用一维的 malloc 创建它,以避免分配太多内存。 数组的大小: int T
“给定一个 2D 字符数组和一个字符串。 查找特定字符串是否出现在矩阵的对角线上。 private static boolean diagonalContains(char[][] grid,Stri
我有一个由 nxn 矩阵组成的 njxnj 矩阵。我想提取 nxn 矩阵的对角 j block 。即我想提取对角线(对于 n = 2,j = 4): 最有效的方法是什么? 最佳答案 要为元素建立索引,
这是一个 self 回答的问题。给定两个数据框, x 0 1 0 1 2 1 3 4 y 0 1 2 0 5 6 7 1 8 9 X 2 Y Z 0 x 和
我试图让我的程序打印出不等于幻方规则的行、列或对角线,例如,如果矩阵是 1 9 5 2 4 3 6 8 7 第 1 行 [2, 4, 3] 不起作用 第 2 行 [6, 8, 7] 不起作用 第 0
所以我有一个像这样的 4x4 矩阵 |0 1 2 3 -+------- 0|0 1 3 6 1|2 4 7 a 2|5 8 b d 3|9 c e f 并且我是按照其中的十六进制字符指定的顺序遍历
什么是获取正方形DataFrame的对角线的有效方法。我希望结果是一个 Series 和一个 MultiIndex 有两个级别,第一个是 DataFrame 的索引,第二个级别是DataFrame 的
问题:我正在尝试在 SwiftUI 中以矩形呈现对角线线性渐变。 我实现了一个标准的多点线性渐变,它在呈现为正方形时效果很好,但是当我将框架更改为矩形时,它有一些奇怪的行为,看起来更水平,或者有一些奇
我目前正在尝试找到一种在 C# for Excel 中使用 VSTO 的方法,以使用 C# 代码在单元格中绘制对角线。但我在网上找不到任何人甚至试图这样做。 有谁知道如何实现这一目标? 谢谢 (对不起
我需要删除图像中的线条,这最终是一个表格。我找到了一种删除水平线和垂直线的方法: convert 1.jpg -type Grayscale -negate -define morphology:co
我有一个如下所示的矩阵: ` matrix = [ ['P', 'o', 'P', 'o', 'P'], ['m', 'i', 'c', 's', 'r'], ['g', 'a', 'T', 'A',
如何在Python中按如下方式堆叠矩阵,使得父矩阵的元素在子矩阵的相同 block 对角点处形成 block 对角线。例子:我有四个矩阵 AA,AB,BA,BB 我想制作如附图所示的矩阵。 最佳答案
我在做一些统计。 我有数据框: tag a b c d e f a 5 2 3 2 0 1 b 2 4 3 2 0 1
我最近做了一个 Connect4 游戏,当我的 Connect4 向右对角线连接时,我的 Connect4 没有赢得游戏。并且它只适用于某些组合,当它连接到左边的对角线时。坐标:- 左上角:(0,0)
在 numpy 中有什么方法可以获取对角数组的引用吗?我希望我的数组对角线除以某个因子谢谢 最佳答案 如果 X 是你的数组,c 是因子, X[np.diag_indices_from(X)] /= c
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我有一个形状为 (m*n, m*n) 的张量,我想提取一个大小为 (n, m*n) 的张量,其中包含对角线上大小为 n*n 的 m 个块。例如: >>> a tensor([[1, 2, 0, 0],
我目前正在使用 matplotlib/pyplot 绘制 3d 对象,如下所示: fig = plt.figure().gca(projection='3d') plot = fig.plot_sur
好的,让我们考虑一个 64 位的数字,它的位组成一个 8x8 的表。 例如 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1
我是一名优秀的程序员,十分优秀!