python-在固定点粘贴图片

from PIL import Image
from ...FaceRotate import FaceRotate
from ...Morph import Morph
import cv2
from numpy import *
def merge():
    source_path = '/root/Desktop/test.jpg'
    yuantu = cv2.imread(source_path)

    eye_path = '/root/Desktop/2111_A_real_A.png'
    eye_pic = Image.open(eye_path)
    eye_pic = eye_pic.resize((180, 180))

    mph = Morph()
    f = FaceRotate()
    Bpoint = f.get_points(yuantu)

    radius = 90
    x0 = int(Bpoint[100][0])
    y0 = int(Bpoint[100][1])

    x1 = x0 - radius
    y1 = y0 - radius
    x2 = x0 + radius
    y2 = y0 + radius
    height = y2 - y1
    width = x2 - x1

    crop_img = yuantu[y1:y1 + height, x1:x1 + width]
    cv2.imwrite('/root/Desktop/crop_img.jpg', crop_img)
    crop_img_ = Image.open('/root/Desktop/result.jpg')
    crop_img_new = Image.blend(crop_img_, eye_pic, 0.7)

    yuantu_ = Image.open(source_path)
    yuantu_.paste(crop_img_new, (x0, y0))
    yuantu_.show()
    yuantu_.save('/root/Desktop/result_new.jpg')

if __name__ == '__main__':
    merge()

### 实现图像的随机裁剪与重新拼接 为了实现图像的随机裁剪并将其重新拼接,在Python中有多种方法可以完成这一操作。这里介绍一种基于`OpenCV`和`NumPy`的方法来执行此任务。 #### 随机裁剪函数定义 首先,创建一个用于从原始图像中提取子图的功能模块。该功能会根据指定的高度宽度参数以及步长(即每次移动的距离),在原图内选取多个不重叠或部分重叠的小区域作为新的训练样本[^4]。 ```python import cv2 import numpy as np from random import randint def random_crop(image, crop_height, crop_width): max_x = image.shape[1] - crop_width max_y = image.shape[0] - crop_height if max_x <= 0 or max_y <= 0: raise ValueError('Image size is smaller than the specified crop dimensions.') x = randint(0, max_x) y = randint(0, max_y) cropped_image = image[y:y + crop_height, x:x + crop_width] return cropped_image ``` 上述代码实现了单次随机位置的选择,并从中截取固定大小的部分;当输入图片不足以满足所设条件时,则抛出异常提示错误信息。 #### 处理多张碎片化后的重组逻辑 接下来构建另一个辅助工具用来管理这些被切割出来的片段并将它们按照一定规律组合回去形成完整的图形。考虑到实际应用中的灵活性需求,下面给出的例子允许用户自定义最终输出画布的具体规格: ```python def reassemble_patches(patches_list, target_shape=(None, None)): rows = [] row_images = [] current_row_height = sum([p.shape[0] for p in patches_list]) total_cols = max([p.shape[1] for p in patches_list]) canvas_height = target_shape[0] if all(target_shape) else current_row_height canvas_width = target_shape[1] if all(target_shape) else total_cols * len(patches_list) empty_patch = np.zeros((canvas_height//len(patches_list), canvas_width)) index = 0 while True: try: patch = patches_list[index] if not row_images or sum([ri.shape[1] for ri in row_images]) + patch.shape[1] <= canvas_width: row_images.append(patch) elif any(row_images): combined_row = np.hstack(tuple(row_images)) padding_needed = canvas_width - combined_row.shape[1] padded_row = cv2.copyMakeBorder(combined_row, top=0,bottom=0,left=0,right=padding_needed, borderType=cv2.BORDER_CONSTANT,value=[0,0,0]) rows.append(padded_row) row_images.clear() index += 1 except IndexError: break last_combined_row = np.hstack(tuple(row_images)) if row_images else empty_patch remaining_space = canvas_height - last_combined_row.shape[0] final_canvas = cv2.vconcat(rows + [last_combined_row,cv2.copyMakeBorder(last_combined_row,top=remaining_space,bottom=0,left=0,right=0,borderType=cv2.BORDER_CONSTANT,value=[0,0,0])][:-(not bool(rows)+1)]) return final_canvas[:target_shape[0], :target_shape[1]] if all(target_shape) else final_canvas ``` 这个过程涉及到水平方向上的连接(`np.hstack`)和平铺式的垂直堆叠(`cv2.vconcat`)两个主要步骤。此外还加入了必要的边界填充以确保所有组件都能适配到预设的目标框架之内[^3]。 通过以上两段核心算法的支持,就可以方便地对任意给定的大尺寸影像文件实施多次不同方位/尺度下的局部采样动作,同时也能保证经过一系列变换之后的数据能够顺利恢复原型以便后续分析使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值