python Pillow 把一个图片复制3次

1. 先看效果

输入图片

输出图片

输入图片

输出图片

代码

来源: kimi , https://kimi.moonshot.cn/chat/cr6ug39mqu053qm0sfsg

python3 复制代码
from PIL import Image
import os


# 对于一个  1365*2048 的图片,进行复制3次
def duplicate_and_combine_horizontally(image_path):
    # 打开原始图片
    original_image = Image.open(image_path)

    # 获取原始图片的尺寸
    width, height = original_image.size

    # 创建一个新图片,宽度是原始宽度的3倍,高度不变
    new_image_width = 3 * width
    new_image = Image.new('RGB', (new_image_width, height))

    # 将原始图片粘贴到新图片上,复制3次
    new_image.paste(original_image, (0, 0))
    new_image.paste(original_image, (width, 0))
    new_image.paste(original_image, (2 * width, 0))

    # 从输入文件名构造输出文件名
    base_name = os.path.splitext(os.path.basename(image_path))[0]  # 去除文件扩展名
    output_filename = f"{base_name}_combined.jpg"

    # 保存新图片
    new_image.save(output_filename)  # 保存图片到文件
    print(f"Image saved as {output_filename}")


# 使用函数,传入图片路径
duplicate_and_combine_horizontally('../imgs/2.jpg')
duplicate_and_combine_horizontally('y.jpg')
相关推荐
麦麦鸡腿堡几秒前
Java_MySQL介绍
java·开发语言·mysql
于是我说几秒前
一份Python 面试常见问题清单 覆盖从初级到高级
开发语言·python·面试
shoubepatien几秒前
JavaWeb_Web基础
java·开发语言·前端·数据库·intellij-idea
BoBoZz19几秒前
RotationAroundLine 模型的旋转
python·vtk·图形渲染·图形处理
Kurbaneli4 分钟前
Python金融数据分析革命:Mootdx让通达信数据获取变得如此简单
python
吧啦蹦吧14 分钟前
`org.springframework.util.ClassUtils#forName
开发语言·python
CC.GG15 分钟前
【C++】红黑树
java·开发语言·c++
倔强的小石头_16 分钟前
Python 从入门到实战(十):Pandas 数据处理(高效搞定表格数据的 “瑞士军刀”)
人工智能·python·pandas
学IT的周星星26 分钟前
java常见面试题
java·开发语言
Together_CZ26 分钟前
DarkIR: Robust Low-Light Image Restoration——鲁棒的低光照图像复原
python·image·robust·darkir·鲁棒的低光照图像复原·low-light·restoration