经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
Python?opencv应用实现图片切分操作示例
来源:jb51  时间:2022/6/6 15:45:25  对本文有异议

说明

之前下载来zip包的漫画,里面的图片都是两张一起的:

但是某些漫画查看软件不支持自动分屏,看起来会比较不舒服,所以只能自己动手来切分。

操作说明

Python有不少的库支持图片操作,其中比较著名的一个是OpenCV。

OpenCV是一个跨平台的计算机视觉库,Python下有它的接口实现。

Python默认不带OpenCV,所以需要先用pip下载:

OpenCV功能强大,这里用来做图片的切分其实是牛刀小试。

关于OpenCV的功能,这里不多介绍,有兴趣的可以找其它的资料。

为了在代码中使用OpenCV,首先需要import相关的库:

  1. import cv2 # Should be install independently.

然后是读取图片:

  1. img1 = cv2.imread(filename)

然后做切割:

  1. # shape[0]:height shape[1]:width shape[2]:channel
  2. # img[y0:y1, x0:x1] 0=(left up) 1=(right low)
  3. slice1 = img[0:height, width/2:width]

这里实际上就是指定图片框体,需要的两个值是左上角和右下角坐标,只是对应的方式有些诡异,不知道为什么要这样对应。

然后是回写图片:

  1. cv2.imwrite(getname(index1), slice1, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])

此外,为了保证图片不会太大,还可以做些压缩:

  1. img = cv2.resize(img1, (0, 0), fx=compressratio, fy=compressratio, interpolation=cv2.INTER_NEAREST)

以上就是涉及到图片的基本代码。

代码

下面是全部的代码,将它保存到py文件中,然后与图片放到同一个目录,双击py文件就可以执行,并进行图片切分:

  1. #!/usr/bin/env python
  2. # ---------------------------------------------------------------------------------
  3. # coding=utf-8
  4. # @File : sliceImage.py
  5. # @Author : Jiangwei
  6. # @Date : 2020/4/18
  7. # @Desc : Slice images.
  8. # @History :
  9. # Date Author Description
  10. # 20200418 Jiangwei Created.
  11. # @Warning:
  12. # Tested in Python 2.7.
  13. # ---------------------------------------------------------------------------------
  14. import os
  15. import sys
  16. import cv2 # Should be install independently.
  17. todir = "tmp"
  18. exts = ['.jpg', '.JPG', '.png', '.PNG']
  19. compressratio = 0.75
  20. def listimage(adir):
  21. '''
  22. adir : The directory name.
  23. '''
  24. list = []
  25. for i in os.listdir(adir):
  26. if os.path.splitext(i)[1] in exts:
  27. list.append(os.path.join(adir, i))
  28. return list
  29. def getname(index):
  30. page = "Image%03d.png" % index
  31. return os.getcwd() + "\\" + todir + "\\" + page
  32. def doslice(filename, index1, index2):
  33. img1 = cv2.imread(filename)
  34. img = cv2.resize(img1, (0, 0), fx=compressratio, fy=compressratio, interpolation=cv2.INTER_NEAREST)
  35. height,width = img.shape[0:2]
  36. # shape[0]:height shape[1]:width shape[2]:channel
  37. # img[y0:y1, x0:x1] 0=(left up) 1=(right low)
  38. slice1 = img[0:height, width/2:width]
  39. cv2.imwrite(getname(index1), slice1, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])
  40. print getname(index1)
  41. slice2 = img[0:height, 0:width/2]
  42. cv2.imwrite(getname(index2), slice2, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])
  43. print getname(index2)
  44. return
  45. if __name__ == "__main__":
  46. '''
  47. Slice images.
  48. '''
  49. # Temperature directory for sliceped images.
  50. if not os.path.exists(todir):
  51. os.mkdir(todir)
  52. # Transverse all files and do the slice.
  53. imagelist = listimage (os.getcwd())
  54. index = 1
  55. for i in imagelist:
  56. print "Processing %s" % i
  57. doslice(i, index, index + 1)
  58. index += 2

切分之后的文件会放到新创建的tmp目录下。

切换效果

下面是切换之后的效果:

代码写得不怎么样,不过能够用......

以上就是Python opencv应用实现图片切分操作示例的详细内容,更多关于Python opencv图片切分的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号