├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── LICENSE ├── README.md ├── getseal.py ├── out.png └── result.png /1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edk24/getseal/118429361e5b9012dc18e107391c619607ffd1df/1.png -------------------------------------------------------------------------------- /2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edk24/getseal/118429361e5b9012dc18e107391c619607ffd1df/2.png -------------------------------------------------------------------------------- /3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edk24/getseal/118429361e5b9012dc18e107391c619607ffd1df/3.png -------------------------------------------------------------------------------- /4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edk24/getseal/118429361e5b9012dc18e107391c619607ffd1df/4.png -------------------------------------------------------------------------------- /5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edk24/getseal/118429361e5b9012dc18e107391c619607ffd1df/5.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 余晓波 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 印章提取 2 | 3 | #### Install 4 | 5 | ```bash 6 | $ pip3 install opencv-python 7 | $ pip3 install numpy 8 | $ pip3 install Pillow 9 | ``` 10 | 11 | #### Run 12 | 13 | ```bash 14 | $ python3 getseal.py -i source.png -o out.png 15 | ``` 16 | 17 | #### Result 18 | 19 | ![233](result.png) 20 | 21 | 22 | 23 | ![](out.png) 24 | 25 | #### Quote 26 | 27 | - https://blog.csdn.net/wsp_1138886114/article/details/82858380 28 | - https://blog.csdn.net/kongdacai4584/article/details/102824470 29 | 30 | 31 | 32 | #### 印章来源 (百度图片) 33 | 34 | ![tp](1.png) -------------------------------------------------------------------------------- /getseal.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import PIL.Image as Image 4 | import sys, getopt 5 | import time 6 | 7 | def transparent_back(img): 8 | img = img.convert('RGBA') 9 | L, H = img.size 10 | color_0 = (255,255,255,255) 11 | for h in range(H): 12 | for l in range(L): 13 | dot = (l,h) 14 | color_1 = img.getpixel(dot) 15 | if color_1 == color_0: 16 | color_1 = color_1[:-1] + (0,) 17 | img.putpixel(dot,color_1) 18 | return img 19 | 20 | def action(inputfile, outputfile): 21 | np.set_printoptions(threshold=np.inf) 22 | image=cv2.imread(inputfile) 23 | 24 | hue_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) 25 | # cv2.imshow("s", hue_image) 26 | # cv2.waitKey(0) 27 | 28 | low_range = np.array([150,24,150]) 29 | high_range = np.array([180, 255, 255]) 30 | th = cv2.inRange(hue_image, low_range, high_range) 31 | index1 = th == 255 32 | 33 | # save image 34 | img = np.zeros(image.shape, np.uint8) 35 | 36 | img[:, :] = (255,255,255) 37 | img[index1] = image[index1]#(0,0,255) 38 | # cv2.imshow("s", img) 39 | # cv2.waitKey(0) 40 | 41 | cv2.imwrite(outputfile, img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9]) 42 | # delete rgb(255,255,255) 43 | img1 = Image.open(outputfile) 44 | img1=transparent_back(img1) 45 | img1.save(outputfile) 46 | 47 | if __name__ == "__main__": 48 | inputfile = '' 49 | outputfile = '' 50 | argv = sys.argv[1:] 51 | try: 52 | opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) 53 | except getopt.GetoptError: 54 | print(' -i -o ') 55 | sys.exit(2) 56 | for opt, arg in opts: 57 | if opt == '-h': 58 | print(' -i -o ') 59 | sys.exit() 60 | elif opt in ("-i", "--ifile"): 61 | inputfile = arg 62 | elif opt in ("-o", "--ofile"): 63 | outputfile = arg 64 | start = time.time() 65 | action(inputfile, outputfile) 66 | end = time.time() 67 | print("耗时: "+str(end-start)) 68 | 69 | 70 | -------------------------------------------------------------------------------- /out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edk24/getseal/118429361e5b9012dc18e107391c619607ffd1df/out.png -------------------------------------------------------------------------------- /result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edk24/getseal/118429361e5b9012dc18e107391c619607ffd1df/result.png --------------------------------------------------------------------------------