Python代码库OpenCV之13 古书一页切成两页

Python代码库OpenCV之13 古书一页切成两页

之前项目

首先将pdf切割成图片,然后再将每个图片切割成2页

  • Python代码库之PDF转换为图片poppler

照片与效果

代码

import os
import cv2
import shutil


out_path='./chunqiu_out'
in_path='./chunqiu_image'
filestart='59676d78-8952-47c7-9f38-ab8923379d31-'
pageNum=0
# 定义切割函数
def cut_img(outpath,pageNum,image_path):
    img=cv2.imread(image_path)
    
    x=747
    y=65
    h=850
    w=560
    cropImg = img[(y):(y + h), (x):(x + w)]
    cv2.imwrite(outpath+'/%d.png'%pageNum, cropImg)

    x=185
    y=65
    h=850
    w=560
    cropImg = img[(y):(y + h), (x):(x + w)]
    cv2.imwrite(outpath+'/%d.png'%(pageNum+1), cropImg)

# 创建文件夹
out_path='./chunqiu_out'
if os.path.exists(out_path): 
    shutil.rmtree(out_path, ignore_errors=True)
os.mkdir(out_path)
# 分割图片
for i in range(3,130):
    image_path=in_path+'/'+filestart+'%03d.png'%i
    print(image_path)
    cut_img(out_path,pageNum,image_path)    
    pageNum=pageNum+2
    

更多精彩代码请关注我的专栏

  • selenium & python 源码大全
  • reportlab教程和源码大全
  • python源码大全

你可能感兴趣的:(Python代码库OpenCV之13 古书一页切成两页)