python图片切割与合并

  • 1.tif图片切割为jpg
  • 2.图片合并

1.tif图片切割为jpg

  • 将图片切割为1024大小的小图片
  • 大小不够1024的,填充为黑色
    import os
    import numpy as np
    import cv2 as cv
    import PIL.Image as Imagedef cutting(img_path, data_path,image_name, clip_path):""":param img_path: 图片文件夹:param data_path: 数据文件夹:param image_name: 切割原始图片名:param clip_path: 切割图片保存路径:return:"""img_size = 1024n, m, max_n = 0, 0, 0img = cv.imread(os.path.join(img_path, image_name), 1)clip_img_path = os.path.join(data_path, 'clip', clip_path)if not os.path.exists(clip_img_path):os.makedirs(clip_img_path)h, w = img.shape[0], img.shape[1]# 图片切割for i in range(0, h, img_size):img_h = (h % img_size) if (i + img_size) > h else 1024for j in range(0, w, img_size):max_n = n if max_n <= n else max_nend_i, end_j = min(h, i + img_size), min(w, j + img_size)cropped = img[i:end_i, j:end_j]img_w = (w % img_size) if (j + img_size) > w else 1024img_orig = Image.fromarray(cv.cvtColor(cropped, cv.COLOR_BGR2RGB)) # bgr 转 rgbto_image = Image.new('RGB', (img_size, img_size))  # 创建1024大小图片to_image.paste(img_orig, (0, 0))if img_w != 1024:  # 宽度填充img_w_new = np.zeros(shape=((img_size - img_w), img_h, 3), dtype=np.uint8)img_w_new = Image.fromarray(img_w_new)to_image.paste(img_w_new, (img_w, 0))if img_h != 1024:  # 宽度填充 and 高度填充img_h_new = np.zeros(shape=(img_size, (img_size - img_h), 3), dtype=np.uint8)img_h_new = Image.fromarray(img_h_new)to_image.paste(img_h_new, (0, img_h))to_image.save(clip_img_path + '/' + str(m) + '_' + str(n) + ".jpg")elif img_w == 1024 and img_h != 1024:  # 只填充高度img_h_new = np.zeros(shape=(img_size, (img_size - img_h), 3), dtype=np.uint8)img_h_new = Image.fromarray(img_h_new)to_image.paste(img_h_new, (0, img_h))to_image.save(clip_img_path + '/' + str(m) + '_' + str(n) + ".jpg")else:  # 不填充cv.imwrite(clip_img_path + '/' + str(m) + '_' + str(n) + ".jpg", cropped)n += 1n = 0m += 1if __name__ == '__main__':root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))data_path = os.path.join(root, 'data')img_path = os.path.join(data_path, 'bhfx1')cutting(img_path, data_path, "fw1_2021.tif", "clip_2021")

2.图片合并

  • 合并的图片大小一致

    import os
    import PIL.Image as Imagedef image_compose(IMAGE_SIZE=1024, IMAGE_ROW=24, IMAGE_COLUMN=19):to_image = Image.new('RGB', (IMAGE_SIZE*(IMAGE_COLUMN+1), IMAGE_SIZE*(IMAGE_ROW+1)))  # 创建新图片大小for y in range(0, IMAGE_ROW + 1):  # 循环遍历,把每张图片按顺序粘贴到对应位置上for x in range(0, IMAGE_COLUMN + 1):file_name = str(y) + '_' + str(x) + ".jpg"img_name = os.path.join(clip_img_path, file_name) # 图片名from_image = Image.open(img_name) # 打开合并的图片to_image.paste(from_image, ((x) * IMAGE_SIZE, (y) * IMAGE_SIZE))  # 粘贴image到im的position(左上角)位置。w,hto_image.save(IMAGE_SAVE_PATH) # 保存图片if __name__ == '__main__':root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))data_path = os.path.join(root, 'data')clip_img_path = os.path.join(data_path, 'clip', 'clip_2020')IMAGE_SAVE_PATH = os.path.join(data_path, 'img_merge', 'clip_2020.jpg')image_compose()
    

python图片切割与合并相关推荐

  1. 【Python+图片切割+图片合并】基于Python的图片批量切割与合并(保持原像素不变,不会出现像素大小不匹配、填充黑边的问题)

    基于Python的图片批量切割与合并(保持原像素不变,不会出现像素大小不匹配.填充黑边的问题) 前言 效果图 1.批量读取文件 2.清空目标目录方法(配合切割图片方法使用) 3.批量切割图片(像素不足 ...

  2. python实现图片切割和合并

    在进行滑块验证码图像还原的过程中,需要用到图片切割成小的图像块,然后根据给定的排序对小的图像块进行重新组合. 目录 一.将原图进行切割 二.切割之后的图片 三.还原之后的图片 未切割前的图片 一.将原 ...

  3. python图片切割以及识别图片中的文字

    今天记录在爬取图片网站时,需要按如下需求展示图片和答案: 本次爬取数据量不大,爬取内容也都集中在一个页面,网站也没有异步加载或反爬措施,但是遇到了三个难点: 难点一:图片链接是lazyload,且全部 ...

  4. python图片切割导入excel_python处理excel中的图片-裁剪

    分享最近学习python处理excel中的图片一段代码,我是做无线通信工作的(无线网络优化,俗称"网优"),我以实际工作中用到的案例进行分享.写的不好请看官多多担待,作为初学者我会 ...

  5. python图片处理之图片切割

    python图片切割在很多项目中都会用到,比如验证码的识别.目标检测.定点切割等,本文给大家带来python的两种切割方式: from PIL import Image""&quo ...

  6. python读取图像并相加_python使用PIL和matplotlib获取图片像素点并合并解析

    python 版本 3.x 首先安装 PIL 由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又 ...

  7. python智能图片识别系统(图片切割、图片识别、区别标识)

    目录 技术介绍 运行效果 关键代码 写在最后 技术介绍 你好! python flask图片识别系统使用到的技术有:图片背景切割.图片格式转换(pdf转png).图片模板匹配.图片区别标识. 运行效果 ...

  8. python 图片合并_Python 图片合并pdf

    1.缘起 最近需要将记的笔记整理成一个 pdf 进行保存,所以就研究了一下如何利用 Python 代码将拍下来的照片整个合并成一个 pdf. 2.过程 拿到一个需求最重要的就是将大块任务拆分成一个个小 ...

  9. C#图片处理类(颜色透明化,图片切割,图片合并,图片旋转等)(转)

                              目录 1.背景透明化 2.指定颜色透明化 3.指定颜色替换成另一种颜色 4.图片按比例缩放 5.图片旋转 6.图片更改透明度 7.图片添加文字 8. ...

最新文章

  1. 如何让我们的vmware虚拟机上网!!
  2. Angular multiple binding debug
  3. 【转】最牛B的编码套路
  4. 怎样学c++程序语言,如何学好 C++——学习门槛最高的编程语言
  5. Android CI with jenkins in ubuntu
  6. 二维码生成易语言代码
  7. office计算机高级应用,全国高等学校计算机水平考试Ⅱ级《Office2010高级应用》考试大纲(试行)...
  8. 微信公众号网页授权-java开发
  9. 《kiss the rain》与追求完美的矛盾个体
  10. 史上最全 MCC(移动国家码)和 MNC(移动网络码)
  11. 【原创】十年可以做什么?
  12. 题目:大写年月日改写成阿拉伯数字
  13. 支付机构客户备付金存管办法
  14. 采集人物经历来佐证子平术
  15. 计算机设备替换方案,电脑升级cpu的方案
  16. Javaweb1:HTML、各种标签
  17. afrog 进阶篇(实战)
  18. C/C++编程:tan、atan、sin、cos等三角函数用法的代码演示及结果,注意角度和弧度的转换!
  19. 博途PLC 中位值滤波算法(FC功能块)
  20. java开发入门思维导图,java秒杀系统面试题

热门文章

  1. yolov5检测限定长宽比检测范围的目标
  2. django mysql sql语句_Django中使用mysql数据库并使用原生sql语句操作
  3. python实现图像像素修改脚本
  4. 码农如何克服“职业病”
  5. 硅云服务器怎么建网站,硅云怎么样,硅云香港云服务器怎么样
  6. 海豚湾--纪录日本人如何杀戮海豚的
  7. 使用易路代理yiluproxy时,出现不是自己选定的城市该怎么办?
  8. Sai 2 打开文件显示没有注册类的解决方法
  9. 区块链为什么叫区块链
  10. iMac 2019一体机开箱体验,其内存是什么牌子,怎样换iMac内存?