Added signatue photo to (transparent) png converter script.

This commit is contained in:
Rodolfo Ferro 2019-10-03 03:08:43 -05:00
parent 42f18a2d4a
commit 8ac09c04ff
5 changed files with 154 additions and 4 deletions

View File

@ -31,7 +31,7 @@ So far, the following projects have been integrated to this repo:
|[Gmail Mailing Script](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/mailing) |[mayank-kapur](https://github.com/kapurm17) | |[Gmail Mailing Script](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/mailing) |[mayank-kapur](https://github.com/kapurm17) |
|[Handwrting DNN recognizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Handwriting_Recognizer) |[Chris]() | |[Handwrting DNN recognizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Handwriting_Recognizer) |[Chris]() |
|[HTML Table to List](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/HTML_Table_to_List) | [Nitish Srivastava](https://github.com/nitish-iiitd)| |[HTML Table to List](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/HTML_Table_to_List) | [Nitish Srivastava](https://github.com/nitish-iiitd)|
|[Image circle formatter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Image-Circulator) |[Berk Gureken](https://github.com/bureken) | |[Image circle formatter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Image-Circulator) |[Berk Gureken](https://github.com/bureken) |
|[Image To PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/images2pdf)|[msaoudallah](https://github.com/msaoudallah)| |[Image To PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/images2pdf)|[msaoudallah](https://github.com/msaoudallah)|
|[Instadp Web Scrapper](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/InstadpShower)|[Psychiquest](https://github.com/psychiquest)| |[Instadp Web Scrapper](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/InstadpShower)|[Psychiquest](https://github.com/psychiquest)|
|[Own IP locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Own_IP_Adress)|[Chris]()| |[Own IP locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Own_IP_Adress)|[Chris]()|
@ -39,6 +39,7 @@ So far, the following projects have been integrated to this repo:
|[Python Algebra Solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Algebra-Solver)|[Sengxay Xayachack](https://github.com/frankxayachack)| |[Python Algebra Solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Algebra-Solver)|[Sengxay Xayachack](https://github.com/frankxayachack)|
|[Random name generator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Random_Names_Generator)| [Ayush Bhardwaj](https://github.com/hastagAB)| |[Random name generator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Random_Names_Generator)| [Ayush Bhardwaj](https://github.com/hastagAB)|
|[Server Ping](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Ping_Server)|[prince]()| |[Server Ping](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Ping_Server)|[prince]()|
|[Signature photo to PNG converter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/signature2png)|[Rodolfo Ferro](https://github.com/RodolfoFerro)|
|[Simple Webpage Parser](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/SimpleWebpageParser)|[Nitish Srivastava](https://github.com/nitish-iiitd)| |[Simple Webpage Parser](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/SimpleWebpageParser)|[Nitish Srivastava](https://github.com/nitish-iiitd)|
|[Slideshare downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Slideshare-Downloader)|[Chris Goes](https://github.com/GhostofGoes)| |[Slideshare downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Slideshare-Downloader)|[Chris Goes](https://github.com/GhostofGoes)|
|[SMS your location](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/SmsYourLocation)|[prince]()| |[SMS your location](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/SmsYourLocation)|[prince]()|
@ -63,14 +64,14 @@ Remember to star the repo if you love the scipts~ :wink:
## Contribuition Guidelines : ## Contribuition Guidelines :
- Make a **separate folder** for your script. - Make a **separate folder** for your script.
- There shouldn't be any **spaces** between the names of the script. (Use underscore or dash Symbol) - There shouldn't be any **spaces** between the names of the script. (Use underscore or dash Symbol)
- :x: Script One - :x: Script One
- :heavy_check_mark: Script_One - :heavy_check_mark: Script_One
- :heavy_check_mark: Script-One - :heavy_check_mark: Script-One
- The Folder should contain the followings - - The Folder should contain the followings -
- Main Python Script, - Main Python Script,
- Supporting files for the Script (If any) - Supporting files for the Script (If any)
- A separate `README.md` File with proper documentation. - A separate `README.md` File with proper documentation.
- Feel Free to add your script in the [project's list](https://github.com/hastagAB/Awesome-Python-Scripts#what-do-we-have) above. - Feel Free to add your script in the [project's list](https://github.com/hastagAB/Awesome-Python-Scripts#what-do-we-have) above.

59
signature2png/README.md Normal file
View File

@ -0,0 +1,59 @@
# Python signature editor
The present repo contains a Python script to process signature images, it returns a `png` image with transparent background containing only the signature.
## Setup
The Python PIL ([Python Image Library](http://pillow.readthedocs.io/en/latest/)) package with [Python 3](https://www.python.org/downloads/) is used in this repo.
To install PIL via pip:
```bash
pip install pillow
```
## Contents
The main script is [`signature.py`](https://github.com/RodolfoFerro/Signature/blob/master/scripts/signature.py), which contains a set of utility functions developed so far.
### Done so far
* Binarize your signature
* Remove background and make it transparent
### TODO
* Smooth image
* Image orientation
* Crop to get only signature
## Usage
The main script, [`signature.py`](https://github.com/RodolfoFerro/Signature/blob/master/scripts/signature.py) requires a set of parameters that are needed in the parser.
```bash
$ python signature.py -h
usage: signature.py [-h] -i INPUT [-o OUTPUT] [-th THRESHOLD]
optional arguments:
-h, --help show this help message and exit
-i INPUT, --input INPUT
Input image.
-o OUTPUT, --output OUTPUT
Output image.
-th THRESHOLD, --threshold THRESHOLD
```
An example to use this script is as follows:
```bash
$ python signature.py -i imgs/example.jpg -o imgs/result.png -th 190
```
### Results
<img src="https://raw.githubusercontent.com/RodolfoFerro/Signature/master/imgs/RudolfoAnaya.jpg" width="50%"><img src="https://raw.githubusercontent.com/RodolfoFerro/Signature/master/imgs/resultado.png" width="50%">
***
#### Reference to:
<https://github.com/RodolfoFerro/Signature>

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -0,0 +1,90 @@
# ===============================================================
# Author: Rodolfo Ferro Pérez
# Email: ferro@cimat.mx
# Twitter: @FerroRodolfo
#
# Script: Process signatures to remove background.
#
# ABOUT COPYING OR USING PARTIAL INFORMATION:
# This script was originally created by Rodolfo Ferro. Any
# explicit usage of this script or its contents is granted
# according to the license provided and its conditions.
# ===============================================================
from PIL import Image, ImageOps
import argparse
inFile = ''
outFile = ''
def binarize(img, threshold=127):
"""Utility function to binarize an image."""
for i in range(img.size[0]):
for j in range(img.size[1]):
if img.getpixel((i, j)) > threshold:
img.putpixel((i, j), 255)
else:
img.putpixel((i, j), 0)
return img
def make_transparent(img):
"""Utility function to make transparent background from image."""
img = img.convert("RGBA")
data = img.getdata()
transparent = []
for item in data:
if item[:3] == (255, 255, 255):
transparent.append((255, 255, 255, 0))
else:
transparent.append(item)
img.putdata(transparent)
return img
def main(inFile, outFile, threshold=190):
"""Main function to process image."""
img = Image.open(inFile).convert('L')
img = binarize(img, threshold=threshold)
img = make_transparent(img)
img.save(outFile)
return True
def parser():
"""Argument parser function."""
# Construct the argument parser:
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input",
required=True,
type=str,
default="result.png",
help="Input image.")
ap.add_argument("-o", "--output",
type=str,
default="result.png",
help="Output image.")
ap.add_argument("-th", "--threshold",
type=int,
default=127)
args = vars(ap.parse_args())
return args['input'], args['output'], args['threshold']
if __name__ == "__main__":
inFile, outFile, threshold = parser()
main(inFile, outFile, threshold=threshold)