python - 如何合并 100 张图像以获得巨大的图像?
问题描述
我在合并 100 张图片时遇到了一些问题,因此他们构建了一个 10 x 10 的大图片,如上所示:我试过这样:
from PIL import Image
image1 = Image.open("4XP6edit.png")
image2 = Image.open('4MBSedit.png')
(width, height) = image1.size
result_width = 10*width
result_height = 10*height
result = Image.new('RGB', (result_width, result_height))
result.paste(im=image1, box=(0, 0))
result.paste(im=image2, box=(width, 0))
result.paste(im=image2, box=(2*width, 0))
# and so on until 10*width, 0 and than:
result.paste(im=image10, box=(0, height))
result.paste(im=image10, box=(0, 2*height))
这很好用,但我希望它在 for 循环或某事中自动创建。但我的建议不起作用..有人能找到我的错误吗?
length = len(protein_list)
k=0; m=0; j=0
for i in range(length):
image= Image.open(str(protein_list[i] + 'edit.png')
(width, height) = image.size
result_width = 10*width
result_height = 10*height
result = Image.new('RGB', (result_width, result_height))
if (k == 10): k = 0
if (j >= 0 and j <= 9): m = 0
if (j >= 10 and j <= 19): m = 1
if (j >= 20 and j <= 29): m = 2
if (j >= 30 and j <= 39): m = 3
if (j >= 40 and j <= 49): m = 4
if (j >= 50 and j <= 59): m = 5
if (j >= 60 and j <= 69): m = 6
if (j >= 70 and j <= 79): m = 7
if (j >= 80 and j <= 89): m = 8
if (j >= 90 and j <= 99): m = 9
result.paste(im=image, box=(k*width, m*height))
k= k+1
j=j+1
result
如果部分真的很丑,我知道这一点,但我是编程新手,所以请原谅。有人可以帮我解决这个问题吗?它显示了一张巨大的 10x10 黑色图片,其中显示了一张 proteinedit.png 的图片。它看起来像这样
解决方案
它看起来像这样,因为您在循环的每次迭代中都定义了结果图像。如果所有图像的大小相同,则可以执行以下操作:
length = len(protein_list)
k=0; m=0; j=0
result = None
for i in range(length):
image= Image.open(str(protein_list[i] + 'edit.png')
if result is None:
(width, height) = image.size
result_width = 10*width
result_height = 10*height
result = Image.new('RGB', (result_width, result_height))
if (k == 10): k = 0
...
result
其余的代码可以使用很多改进,所以如果可行,我会将该代码带到 Code Review SO。
推荐阅读
- sql - SP执行时间极慢
- angular - 具有反应形式的角管道:仅适用于模糊
- image - SVG 无法在 IE / Edge 中正确显示
- windows - 如何从 Windows 命令提示符启动运行窗口(Windows 键 + R)?我还想定义 WHAT 程序 Run 应该在同一命令中打开
- c# - 动态加载 DropDownList
- c# - 通过 azure 函数以同步方式同时处理多个文件
- scheme - 方案中的嵌套条件和未指定的返回值
- python - np.uint8() 使法线向量计算错误
- python - Seaborn pairplot,每个三角形都有不同的数据
- regex - 使用正则表达式的 Github 用户名约定