本文实例为大家分享了python实现书法碑帖图片分割的具体代码,供大家参考,具体内容如下
一、功能实现效果
1、选择要分割的碑帖图片
2、选择碑帖图像分割的行与列,本例的行为:5,列为:4。如何点击“确定行列”
3、输入对于碑帖的内容,点击“确定分割”按钮。
4、在输出文件夹生成了单字版图片,并对应内容命名。方便集字、创作与学习。
二、python代码实现
1、getimgdir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import wx import os from pil import image import numpy as np #import wx.grid #import row_col #row_col ####################################################################################### app = wx.app() #wx.app()行创建了一个应用程序对象。每个 wx 程序都需要一个 .app() 对象 frame = wx.frame(none, - 1 , '请选择待分割的图片文件' ) #wx.frame()方法返回一个可以包含小部件的新窗口 frame.setsize( 0 , 0 , 600 , 300 ) #函数设置位置和大小(x(左),y(顶部),宽度,高度) openfiledialog = wx.filedialog(frame, "open" , " ", " ", "all files (*.*)|*.*" , wx.fd_open | wx.fd_file_must_exist) openfiledialog.showmodal() #显示窗口 src = openfiledialog.getpath() #返回文件的完整路径(如果选择了一个) np.savez( 'dir.npz' , k_a = src) openfiledialog.destroy() |
2、row_col.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import wx import numpy as np import sys import time import os class myframe(wx.frame): clicknum = 0 def __init__( self ): #__init__(self) 是类的初始化方法,也称构造方法,是一种特殊的魔法方法。__init__(self)在实例化后,会自动调用,而不用手动调用,所以一般把属性设置在_init__()里。 super ().__init__(parent = none,title = "图像分割行数与列数" ,size = ( 500 , 730 )) # 初始化窗口信息 panel = wx.panel( self ) #框架的父窗口。对于顶级窗口,这个值是none 。#创建面板 #模块1 选择签约主体 self .center() text1 = wx.statictext(parent = panel, id = - 1 ,pos = ( 10 , 7 ),label = "图像分割行数:" ) list1 = [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ] self .combobox1 = wx.combobox(parent = panel, id = - 1 ,pos = ( 100 , 5 ),value = "5" ,choices = list1) #wx.combobox 默认它的文本框是可以修改的 text2 = wx.statictext(parent = panel, id = - 1 , pos = ( 250 , 7 ), label = "图像分割列数:" ) list2 = [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ] self .combobox2 = wx.combobox(parent = panel, id = - 1 , pos = ( 350 , 5 ), value = "4" ,choices = list2) # wx.combobox 默认它的文本框是可以修改的 datadir = np.load( 'dir.npz' ) imgdir = str (datadir[ 'k_a' ]) copybookimg = wx.bitmap(imgdir, wx.bitmap_type_any) img = wx.image(imgdir) w1,h1 = copybookimg.getsize() if h1> 400 : neww1 = ( 400 * w1) / h1 newh1 = 400 img2 = img.scale( int (neww1),newh1) img2 = wx.bitmap(img2) self .image = wx.staticbitmap(panel, - 1 , img2,pos = ( 10 , 90 )) st1 = wx.statictext(panel, - 1 , "字帖内容:" , pos = ( 10 , 505 )) self .txt1 = wx.textctrl(panel, - 1 , pos = ( 60 , 530 ), size = ( int ( 13.26 * 1 + 23.5 ), 140 ), style = wx.te_multiline) #提交模块 self .button = wx.button(panel, - 1 , "确定行列" , pos = ( 200 , 40 ), size = ( 60 , 30 )) # 在面板上添加控件 self .bind(wx.evt_button, self .onclick, self .button) # 将回调函数与按键事件绑定 def onclick( self , event): # 回调函数事件 self .button.setlabel( "提交成功" ) # 设置 self .clicknum + = 1 if self .clicknum % 2 = = 1 : # 根据按下次数判断 self .button.setlabel( "已经提交" ) # 修改按键的标签 a = self .combobox1.getvalue() b = self .combobox2.getvalue() np.savez( 'abc.npz' , k_a = a, k_b = b) #time.sleep(0.1) self .close() class app(wx.app): def oninit( self ): frame = myframe() frame.show() return true app = app() app.mainloop() #time.sleep(2) #sys.exit(0) |
3、row_col_show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import wx import numpy as np import threading import time from pil import image,imagedraw def draw_line( dir ,a,b): im = image. open ( dir ) draw = imagedraw.draw(im) #实例化一个对象 #a #行 图像的宽:im.size[0] #b #列 图像的高:im.size[1] a = int (a) b = int (b) c = im.size[ 0 ] d = im.size[ 1 ] for i in range (a): draw.line(( 0 , d * (i + 1 ) / a) + (c,d * (i + 1 ) / a), fill = 128 , width = 5 ) #线的起点和终点,线宽 for j in range (b): draw.line((c * (j + 1 ) / b, 0 ) + (c * (j + 1 ) / b,d), fill = 128 , width = 6 ) return (im.save( "00.jpeg" )) class myframe(wx.frame): clicknum = 0 def __init__( self ): # __init__(self) 是类的初始化方法,也称构造方法,是一种特殊的魔法方法。__init__(self)在实例化后,会自动调用,而不用手动调用,所以一般把属性设置在_init__()里。 super ().__init__(parent = none, title = "图像分割行数与列数" , size = ( 500 , 730 )) # 初始化窗口信息 panel = wx.panel( self ) # 框架的父窗口。对于顶级窗口,这个值是none 。#创建面板 # 模块1 选择签约主体 self .center() data_a = np.load( 'abc.npz' ) split_row = int (data_a[ 'k_a' ]) # 读取行数 split_col = int (data_a[ 'k_b' ]) # 读取列数 text1 = wx.statictext(parent = panel, id = - 1 , pos = ( 10 , 7 ), label = "图像分割行数:" ) list1 = [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ] self .combobox1 = wx.combobox(parent = panel, id = - 1 , pos = ( 100 , 5 ), value = str (split_row), choices = list1) # wx.combobox 默认它的文本框是可以修改的 text2 = wx.statictext(parent = panel, id = - 1 , pos = ( 250 , 7 ), label = "图像分割列数:" ) list2 = [ "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ] self .combobox2 = wx.combobox(parent = panel, id = - 1 , pos = ( 350 , 5 ), value = str (split_col), choices = list2) # wx.combobox 默认它的文本框是可以修改的 datadir = np.load( 'dir.npz' ) imgdir = str (datadir[ 'k_a' ]) copybookimg = wx.bitmap(imgdir, wx.bitmap_type_any) #img = wx.image(imgdir) draw_line(imgdir, str (split_row), str (split_col)) img3 = wx.image( "00.jpeg" ) w1, h1 = copybookimg.getsize() if h1 > 400 : neww1 = ( 400 * w1) / h1 newh1 = 400 img2 = img3.scale(neww1, newh1) img2 = wx.bitmap(img2) self .image = wx.staticbitmap(panel, - 1 , img2, pos = ( 10 , 90 )) st1 = wx.statictext(panel, - 1 , "字帖内容:" , pos = ( 10 , 505 )) for i in range (split_col): wx.statictext(panel, - 1 , "第" + str (i + 1 ) + "列:" , pos = ( 10 , 530 + 20 * i)) self .txt1 = wx.textctrl(panel, - 1 , pos = ( 60 , 530 ), size = ( 13.26 * split_row + 23.5 , split_col * 20 ), style = wx.te_multiline) # 提交模块 self .button = wx.button(panel, - 1 , "确定分割" , pos = ( 400 , 650 ), size = ( 60 , 30 )) # 在面板上添加控件 self .bind(wx.evt_button, self .onclick, self .button) # 将回调函数与按键事件绑定 def onclick( self , event): # 回调函数事件 self .button.setlabel( "提交成功" ) # 设置 self .clicknum + = 1 if self .clicknum % 2 = = 1 : # 根据按下次数判断 self .button.setlabel( "已经提交" ) # 修改按键的标签 a = self .combobox1.getvalue() b = self .combobox2.getvalue() c = self .txt1.getvalue() np.savez( 'abc.npz' , k_a = a, k_b = b, k_c = c) #k_c = c碑帖内容保存npz文件 self .close() class app(wx.app): def oninit( self ): frame = myframe() frame.show() return true app = app() app.mainloop() |
4、split_copybook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# -*- coding: utf-8 -*- import wx import os from pil import image import numpy as np #import wx.grid #import row_col #row_col ####################################################################################### data_a = np.load( 'dir.npz' ) src = str (data_a[ 'k_a' ]) #地址 def splitimage(src, rownum, colnum, dstpath): #分割图像,(输入图片路径,分割行数,分割列数,输出图片路径) img = image. open (src) src = src.replace( 'jpg' , 'jpeg' ) print (src) #os.getcwd() w, h = img.size if rownum < = h and colnum < = w: print ( '原碑帖图片信息: %sx%s, %s, %s' % (w, h, img. format , img.mode)) print (' |
5、main.py
1
2
3
4
5
6
7
8
|
import os os.system( "python ./getimgdir.py" ) os.system( "python ./row_col.py" ) os.system( "python ./row_col_show.py" ) os.system( "python ./split_copybook.py" ) os.unlink( '00.jpeg' ) os.unlink( 'abc.npz' ) os.unlink( 'dir.npz' ) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_43756157/article/details/114384721