mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-30 15:31:07 +00:00
Merge branch 'master' into virtual_env
This commit is contained in:
commit
b9601cfa30
24
Find-PhoneNumber-in-String/Find-PhoneNumber-in-String.py
Normal file
24
Find-PhoneNumber-in-String/Find-PhoneNumber-in-String.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
def check_phone_number(string):
|
||||||
|
if len(string) != 12:
|
||||||
|
return False
|
||||||
|
for i in range(0, 3):
|
||||||
|
if not string[i].isdecimal():
|
||||||
|
return False
|
||||||
|
if string[3] != '-':
|
||||||
|
return False
|
||||||
|
for i in range(4, 7):
|
||||||
|
if not string[i].isdecimal():
|
||||||
|
return False
|
||||||
|
if string[7] != '-':
|
||||||
|
return False
|
||||||
|
for i in range(8, 12):
|
||||||
|
if not string[i].isdecimal():
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
string = input("Enter a Sentence: ")
|
||||||
|
|
||||||
|
for i in range(len(string)):
|
||||||
|
split = string[i:i+12]
|
||||||
|
if check_phone_number(split):
|
||||||
|
print('Phone number has been found! : ' + split)
|
10
Find-PhoneNumber-in-String/README.md
Normal file
10
Find-PhoneNumber-in-String/README.md
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# Find Phone Number in a string
|
||||||
|
|
||||||
|
A python script that will extract phone numbers in a string
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
Python 3.7.3
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
$ python Find-PhoneNumber-in-String.py
|
||||||
|
Enter a Sentence: Call me in this number 403-867-2229
|
22
Minecraft_server_in_background/README.md
Normal file
22
Minecraft_server_in_background/README.md
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# Running a Minecraft server in the background
|
||||||
|
|
||||||
|
This program runs a script (which can be specified) in a subprocess with redirected output
|
||||||
|
(new output location can be specified) and periodically checks a file for a keyword (both
|
||||||
|
the name of the file to check and the keyword to check for can be specified)
|
||||||
|
and exits (stopping the subprocess via sending a command), if the contents of the file
|
||||||
|
include the keyword.
|
||||||
|
|
||||||
|
You probably want to run this script in background, e.g. calling it via './run.py &'
|
||||||
|
or via 'nohup ./run.py &'.
|
||||||
|
|
||||||
|
A sample invocation could look like this:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
nohup ./run.py &
|
||||||
|
```
|
||||||
|
Now the specified script, e.g. a Minecraft server, is running in the background.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
echo stop > command.txt
|
||||||
|
```
|
||||||
|
After a short delay, the script in the background will be stopped.
|
29
Minecraft_server_in_background/run.py
Normal file
29
Minecraft_server_in_background/run.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
|
filename_script = './start.sh' # the script that will be executed
|
||||||
|
filename_script_output = './log.txt'
|
||||||
|
filename_own_input = 'command.txt' # the file this script periodically reads from
|
||||||
|
stop_command = b'stop\n' # must be a binary string
|
||||||
|
exit_keyword = 'stop'
|
||||||
|
|
||||||
|
with open(filename_own_input, 'w') as f:
|
||||||
|
f.write('') # reset content of file and create it if needed
|
||||||
|
|
||||||
|
fd_script_output = open(filename_script_output, 'w') # create file descriptor for script to write its stdout to
|
||||||
|
script_process = subprocess.Popen( # start new process running script
|
||||||
|
filename_script,
|
||||||
|
stdin=subprocess.PIPE, # needed for script_process.communicate() (see below)
|
||||||
|
stdout=fd_script_output # redirect output
|
||||||
|
)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
with open(filename_own_input, 'r') as f:
|
||||||
|
if exit_keyword in f.read(): # check if we should exit
|
||||||
|
script_process.communicate(input=stop_command) # stop subprocess and wait for it to terminate
|
||||||
|
break
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
fd_script_output.close()
|
3
Minecraft_server_in_background/start.sh
Normal file
3
Minecraft_server_in_background/start.sh
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
java -Xmx2048M -Xms2048M -jar server.jar -nogui
|
|
@ -34,11 +34,13 @@ So far, the following projects have been integrated to this repo:
|
||||||
|[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)|
|
||||||
|
|[Minecraft Server in background](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Minecraft_server_in_background)|[Max von Forell](https://github.com/mvforell)|
|
||||||
|[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]()|
|
||||||
|[Port Scanner](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Port_Scanner)|[Plutoberth](https://github.com/Plutoberth)|
|
|[Port Scanner](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Port_Scanner)|[Plutoberth](https://github.com/Plutoberth)|
|
||||||
|[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]()|
|
||||||
|
@ -53,6 +55,9 @@ So far, the following projects have been integrated to this repo:
|
||||||
|[Youtube video downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Youtube_Video_Downloader)|[Christopher He](https://github.com/hecris)|
|
|[Youtube video downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Youtube_Video_Downloader)|[Christopher He](https://github.com/hecris)|
|
||||||
|[Zabbix API](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zabbix_api)|[msg4sunny](https://github.com/msg4sunny)|
|
|[Zabbix API](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zabbix_api)|[msg4sunny](https://github.com/msg4sunny)|
|
||||||
|[Zip password cracker](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zip_password_cracker)|[umar abdullahi](https://github.com/umarbrowser)|
|
|[Zip password cracker](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zip_password_cracker)|[umar abdullahi](https://github.com/umarbrowser)|
|
||||||
|
|[CLI Calculator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/cli_calculator)|[Willian GL](https://github.com/williangl) |
|
||||||
|
|[Find PhoneNumber in String](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Find-PhoneNumber-in-String)|[Austin Zuniga](https://github.com/AustinZuniga)|
|
||||||
|
|[IMDB TV Series Info Extractor](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/imdb_episode_ratings)|[Yash Raj Sarrof](https://github.com/yashYRS) |
|
||||||
|
|
||||||
## How to use :
|
## How to use :
|
||||||
|
|
||||||
|
|
11
cli_calculator/README.md
Normal file
11
cli_calculator/README.md
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# calc_argparser
|
||||||
|
Calculadora via CLI
|
||||||
|
|
||||||
|
# Instructions
|
||||||
|
To run CLI_Calculator execute:
|
||||||
|
|
||||||
|
- `python args.py -h` - For help
|
||||||
|
- `python args.py --sum x y` - To sum two numbers
|
||||||
|
- `python args.py --sub x y` - To substraction two numbers
|
||||||
|
- `python args.py --mult x y` - To multiplication two numbers
|
||||||
|
- `python args.py --div x y` - To divide two numbers
|
3
cli_calculator/calc/__init__.py
Normal file
3
cli_calculator/calc/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from .calc import sub, mult, div, soma
|
||||||
|
|
||||||
|
__all__ = ['sub', 'mult', 'div', 'soma']
|
29
cli_calculator/calc/args.py
Normal file
29
cli_calculator/calc/args.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
"""Calculadora utilizando ArgumentParser."""
|
||||||
|
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
from calc import soma, sub, mult, div
|
||||||
|
|
||||||
|
|
||||||
|
parser = ArgumentParser(description='Calculadora')
|
||||||
|
|
||||||
|
parser.add_argument('--sum', help='Operação de soma', action='store_true')
|
||||||
|
parser.add_argument('--sub', help='Operação de subtração', action='store_true')
|
||||||
|
parser.add_argument('--mult', help='Operação de multiplicação', action='store_true')
|
||||||
|
parser.add_argument('--div', help='Operação de divisão', action='store_true')
|
||||||
|
parser.add_argument('x', type=int, help='Primeiro valor')
|
||||||
|
parser.add_argument('y', type=int, help='Segundo valor')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.sum:
|
||||||
|
print(f'{soma(args.x, args.y)}')
|
||||||
|
|
||||||
|
if args.sub:
|
||||||
|
print(f'{sub(args.x, args.y)}')
|
||||||
|
|
||||||
|
if args.mult:
|
||||||
|
print(f'{mult(args.x, args.y)}')
|
||||||
|
|
||||||
|
if args.div:
|
||||||
|
print(f'{div(args.x, args.y)}')
|
22
cli_calculator/calc/calc.py
Normal file
22
cli_calculator/calc/calc.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
|
||||||
|
def soma(x :int, y: int) -> int:
|
||||||
|
"""Função de soma."""
|
||||||
|
return x + y
|
||||||
|
|
||||||
|
|
||||||
|
def sub(x :int, y: int) -> int:
|
||||||
|
"""Função de subtração."""
|
||||||
|
return x - y
|
||||||
|
|
||||||
|
def mult(x :int, y: int) -> int:
|
||||||
|
"""Função de multiplicação."""
|
||||||
|
return x * y
|
||||||
|
|
||||||
|
|
||||||
|
def div(x :int, y: int) -> int:
|
||||||
|
"""Função de divisão."""
|
||||||
|
try:
|
||||||
|
return x / y
|
||||||
|
|
||||||
|
except ZeroDivisionError:
|
||||||
|
return 'Divisao por zero mal sucedida!!'
|
28
cli_calculator/tests/test_calc.py
Normal file
28
cli_calculator/tests/test_calc.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from calc import soma, sub, mult, div
|
||||||
|
|
||||||
|
|
||||||
|
class testCalc(TestCase):
|
||||||
|
def test_should_return_two_values_sum(self):
|
||||||
|
esperado = 1 + 2
|
||||||
|
self.assertEqual(esperado, soma(1,2))
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_two_values_sub(self):
|
||||||
|
esperado = 1 - 2
|
||||||
|
self.assertEqual(esperado, sub(1,2))
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_two_values_mult(self):
|
||||||
|
esperado = 1 * 2
|
||||||
|
self.assertEqual(esperado, mult(1,2))
|
||||||
|
|
||||||
|
def test_should_return_two_values_div(self):
|
||||||
|
esperado = 1 / 2
|
||||||
|
self.assertEqual(esperado, div(1,2))
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_exceptio_on_division_by_zero(self):
|
||||||
|
esperado = 'Divisao por zero mal sucedida!!'
|
||||||
|
self.assertEqual(esperado, div(1,0))
|
19
imdb_episode_ratings/README.md
Normal file
19
imdb_episode_ratings/README.md
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
# Get information about your favorite TV shows at once
|
||||||
|
This python script will make a excel files, with information about every episode from every season of the TV show that you searched for
|
||||||
|
|
||||||
|
## Requirement
|
||||||
|
|
||||||
|
Python 3.6 onwards
|
||||||
|
```bash
|
||||||
|
pip3 install requests
|
||||||
|
pip3 install xlwt
|
||||||
|
pip3 install bs4
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
#Usage
|
||||||
|
Call python following with the simple algebra problem
|
||||||
|
```bash
|
||||||
|
$ python scraper.py
|
||||||
|
```
|
||||||
|
Then simply enter the name of the show you want to search for, and then you will find a excel file in the same directory with the name of the show you searched for
|
107
imdb_episode_ratings/scraper.py
Normal file
107
imdb_episode_ratings/scraper.py
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup as BS
|
||||||
|
import xlwt
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def get_static_html ( search_url ) :
|
||||||
|
## create the soup object for the page
|
||||||
|
try:
|
||||||
|
r_page = requests.get ( search_url )
|
||||||
|
except:
|
||||||
|
print("Connection refused by the server..")
|
||||||
|
time.sleep(5)
|
||||||
|
soup_object = BS( r_page.content , 'html.parser' )
|
||||||
|
#print ( soup_object.prettify() )
|
||||||
|
return soup_object
|
||||||
|
|
||||||
|
def get_url () :
|
||||||
|
## convert to query url , and get raw HTML for the page
|
||||||
|
show_name = input ( " Enter show name ")
|
||||||
|
show_name = '+'.join ( show_name.split() )
|
||||||
|
search_url = "https://www.imdb.com/find?ref_=nv_sr_fn&q="+ show_name + "&s=all"
|
||||||
|
return search_url, show_name
|
||||||
|
|
||||||
|
|
||||||
|
def get_new_url ( soup_object ) :
|
||||||
|
## list of possible search results
|
||||||
|
list_queries = soup_object.find_all('td', class_ = "result_text")
|
||||||
|
|
||||||
|
show_final = None
|
||||||
|
## find the first TV show listing in the relevant searches
|
||||||
|
for show in list_queries :
|
||||||
|
if "(TV Series)" in show.text :
|
||||||
|
show_final = show
|
||||||
|
break
|
||||||
|
|
||||||
|
if show_final == None :
|
||||||
|
print( " No relevant search ")
|
||||||
|
exit()
|
||||||
|
#print ( " Show found - " , show_final )
|
||||||
|
|
||||||
|
## find the link to open the new page
|
||||||
|
hyperlink = show_final.find('a')
|
||||||
|
url_change = hyperlink['href']
|
||||||
|
|
||||||
|
show_url = "https://www.imdb.com/" + url_change + "episodes?season="
|
||||||
|
return show_url
|
||||||
|
|
||||||
|
|
||||||
|
def start() :
|
||||||
|
|
||||||
|
search_url , show_name = get_url()
|
||||||
|
soup_object = get_static_html(search_url)
|
||||||
|
show_url = get_new_url(soup_object)
|
||||||
|
result_file = xlwt.Workbook()
|
||||||
|
|
||||||
|
season_number = 1
|
||||||
|
|
||||||
|
while True :
|
||||||
|
|
||||||
|
soup_object = get_static_html( show_url + str(season_number) )
|
||||||
|
|
||||||
|
## verify if extra season exists
|
||||||
|
verify_season = soup_object.find('h3' , attrs = {'id' :'episode_top'})
|
||||||
|
curr_season = int ( verify_season.text[6:] )
|
||||||
|
if not season_number == curr_season :
|
||||||
|
break
|
||||||
|
|
||||||
|
print ("Season - ", season_number , " information extracted " )
|
||||||
|
|
||||||
|
## excel file
|
||||||
|
result_sheet = result_file.add_sheet( verify_season.text , cell_overwrite_ok=True)
|
||||||
|
result_sheet.write( 0 , 0 , " Name " )
|
||||||
|
result_sheet.write( 0 , 1 , " Rating " )
|
||||||
|
result_sheet.write( 0 , 2 , " Total votes " )
|
||||||
|
result_sheet.write( 0 , 3 , " Summary " )
|
||||||
|
result_sheet.col(3).width = 21000
|
||||||
|
result_sheet.col(0).width = 10000
|
||||||
|
|
||||||
|
episodes_season = soup_object.find_all('div' , class_ = 'info' )
|
||||||
|
curr_episode = 1
|
||||||
|
for episode in episodes_season :
|
||||||
|
## get the name of the episode
|
||||||
|
name_episode = episode.find('strong')
|
||||||
|
## get the rating of the episode
|
||||||
|
rating_episode = episode.find('span' , class_ = 'ipl-rating-star__rating' )
|
||||||
|
## total votes
|
||||||
|
votes_episode = episode.find('span' , class_ = 'ipl-rating-star__total-votes' )
|
||||||
|
## summary
|
||||||
|
summary_episode = episode.find('div' , class_ = 'item_description' )
|
||||||
|
|
||||||
|
## write to the excel file
|
||||||
|
if name_episode :
|
||||||
|
result_sheet.write( curr_episode , 0 , name_episode.text )
|
||||||
|
if rating_episode :
|
||||||
|
result_sheet.write( curr_episode , 1 , rating_episode.text )
|
||||||
|
if votes_episode :
|
||||||
|
result_sheet.write( curr_episode , 2 , votes_episode.text[1:-1] )
|
||||||
|
if summary_episode :
|
||||||
|
result_sheet.write( curr_episode , 3 , summary_episode.text )
|
||||||
|
curr_episode = curr_episode + 1
|
||||||
|
season_number = season_number + 1
|
||||||
|
|
||||||
|
print ( " Finished ")
|
||||||
|
result_file.save( show_name.replace('+' , '_') + '.xls')
|
||||||
|
|
||||||
|
start()
|
59
signature2png/README.md
Normal file
59
signature2png/README.md
Normal 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>
|
BIN
signature2png/imgs/example.jpg
Normal file
BIN
signature2png/imgs/example.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
BIN
signature2png/imgs/result.png
Normal file
BIN
signature2png/imgs/result.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
90
signature2png/signature.py
Normal file
90
signature2png/signature.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user