Python?opencv應用實現(xiàn)圖片切分操作示例
說明
之前下載來zip包的漫畫,里面的圖片都是兩張一起的:

但是某些漫畫查看軟件不支持自動分屏,看起來會比較不舒服,所以只能自己動手來切分。
操作說明
Python有不少的庫支持圖片操作,其中比較著名的一個是OpenCV。
OpenCV是一個跨平臺的計算機視覺庫,Python下有它的接口實現(xiàn)。
Python默認不帶OpenCV,所以需要先用pip下載:

OpenCV功能強大,這里用來做圖片的切分其實是牛刀小試。
關于OpenCV的功能,這里不多介紹,有興趣的可以找其它的資料。
為了在代碼中使用OpenCV,首先需要import相關的庫:
import cv2 # Should be install independently.
然后是讀取圖片:
img1 = cv2.imread(filename)
然后做切割:
# shape[0]:height shape[1]:width shape[2]:channel
# img[y0:y1, x0:x1] 0=(left up) 1=(right low)
slice1 = img[0:height, width/2:width]這里實際上就是指定圖片框體,需要的兩個值是左上角和右下角坐標,只是對應的方式有些詭異,不知道為什么要這樣對應。
然后是回寫圖片:
cv2.imwrite(getname(index1), slice1, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])
此外,為了保證圖片不會太大,還可以做些壓縮:
img = cv2.resize(img1, (0, 0), fx=compressratio, fy=compressratio, interpolation=cv2.INTER_NEAREST)
以上就是涉及到圖片的基本代碼。
代碼
下面是全部的代碼,將它保存到py文件中,然后與圖片放到同一個目錄,雙擊py文件就可以執(zhí)行,并進行圖片切分:
#!/usr/bin/env python
# ---------------------------------------------------------------------------------
# coding=utf-8
# @File : sliceImage.py
# @Author : Jiangwei
# @Date : 2020/4/18
# @Desc : Slice images.
# @History :
# Date Author Description
# 20200418 Jiangwei Created.
# @Warning:
# Tested in Python 2.7.
# ---------------------------------------------------------------------------------
import os
import sys
import cv2 # Should be install independently.
todir = "tmp"
exts = ['.jpg', '.JPG', '.png', '.PNG']
compressratio = 0.75
def listimage(adir):
'''
adir : The directory name.
'''
list = []
for i in os.listdir(adir):
if os.path.splitext(i)[1] in exts:
list.append(os.path.join(adir, i))
return list
def getname(index):
page = "Image%03d.png" % index
return os.getcwd() + "\\" + todir + "\\" + page
def doslice(filename, index1, index2):
img1 = cv2.imread(filename)
img = cv2.resize(img1, (0, 0), fx=compressratio, fy=compressratio, interpolation=cv2.INTER_NEAREST)
height,width = img.shape[0:2]
# shape[0]:height shape[1]:width shape[2]:channel
# img[y0:y1, x0:x1] 0=(left up) 1=(right low)
slice1 = img[0:height, width/2:width]
cv2.imwrite(getname(index1), slice1, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])
print getname(index1)
slice2 = img[0:height, 0:width/2]
cv2.imwrite(getname(index2), slice2, [int(cv2.IMWRITE_PNG_COMPRESSION), 1])
print getname(index2)
return
if __name__ == "__main__":
'''
Slice images.
'''
# Temperature directory for sliceped images.
if not os.path.exists(todir):
os.mkdir(todir)
# Transverse all files and do the slice.
imagelist = listimage (os.getcwd())
index = 1
for i in imagelist:
print "Processing %s" % i
doslice(i, index, index + 1)
index += 2切分之后的文件會放到新創(chuàng)建的tmp目錄下。
切換效果
下面是切換之后的效果:

代碼寫得不怎么樣,不過能夠用......
以上就是Python opencv應用實現(xiàn)圖片切分操作示例的詳細內(nèi)容,更多關于Python opencv圖片切分的資料請關注腳本之家其它相關文章!
相關文章
Python異步編程中asyncio.gather的并發(fā)控制詳解
在Python異步編程生態(tài)中,asyncio.gather是并發(fā)任務調(diào)度的核心工具,本文將通過實際場景和代碼示例,展示如何結合信號量機制實現(xiàn)精準并發(fā)控制,希望對大家有所幫助2025-03-03
解決多個@Scheduled定時任務執(zhí)行時個別不執(zhí)行問題
這篇文章主要介紹了解決多個@Scheduled定時任務執(zhí)行時個別不執(zhí)行問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
python?中的np.zeros()和np.ones()函數(shù)詳解
這篇文章主要介紹了python?中的np.zeros()和np.ones()函數(shù),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04

