78af59eea0c15cd96d0e6aef26f6ff3ff9c0f8c4
Python/5 Killer Automation Scripts in Python \342\200\224 Part 2.md
| ... | ... | @@ -0,0 +1,151 @@ |
| 1 | +https://medium.com/codex/5-killer-automation-scripts-in-python-part-2-5b5a8995eef8 |
|
| 2 | + |
|
| 3 | +# 5 Killer Automation Scripts in Python — Part-2 | by Ishaan Gupta | CodeX | Medium |
|
| 4 | +Here are some awesome automation scripts that you can use in your Python projects. |
|
| 5 | +---------------------------------------------------------------------------------- |
|
| 6 | + |
|
| 7 | +[Ishaan Gupta](https://medium.com/@ishaangupta1201) |
|
| 8 | + |
|
| 9 | + |
|
| 10 | + |
|
| 11 | +[CodeX](https://medium.com/codex) |
|
| 12 | + |
|
| 13 | +Credits — [LINK](https://www.freepik.com/free-photo/impressed-blond-female-ceo-manager-checking-out-something-amazing-gasping-fascinated-saying-wow-holding-hands-cheeks-staring-front-white-wall_19294243.htm#query=wow&position=5&from_view=search) |
|
| 14 | + |
|
| 15 | +**This is the second part of this article —** [**https://medium.com/codex/5-killer-automation-scripts-in-python-42c273fc37c4**](https://medium.com/codex/5-killer-automation-scripts-in-python-42c273fc37c4) |
|
| 16 | + |
|
| 17 | +While making projects, we need some ready-made codes that can help us to solve our daily life problems. This article has **5 Automation Scripts** for your Python Projects that will solve these problems. |
|
| 18 | + |
|
| 19 | +So bookmark it and let’s get started. |
|
| 20 | + |
|
| 21 | +1.) Photo Compressor |
|
| 22 | +-------------------- |
|
| 23 | + |
|
| 24 | +This will Compress your photos into lower sizes while the quality same. |
|
| 25 | + |
|
| 26 | +``` |
|
| 27 | +import PIL |
|
| 28 | +from PIL import Image |
|
| 29 | +from tkinter.filedialog import * |
|
| 30 | + |
|
| 31 | +fl=askopenfilenames() |
|
| 32 | + |
|
| 33 | +img = Image.open(fl[0]) |
|
| 34 | + |
|
| 35 | +img.save("result.jpg", "JPEG", optimize = True, quality = 10) |
|
| 36 | +``` |
|
| 37 | + |
|
| 38 | + |
|
| 39 | +2.) Image Watermarker |
|
| 40 | +--------------------- |
|
| 41 | + |
|
| 42 | +This simple script will watermark any image. You can set the Text, location, and Font. |
|
| 43 | + |
|
| 44 | +``` |
|
| 45 | +from PIL import Image |
|
| 46 | +from PIL import ImageFont |
|
| 47 | +from PIL import ImageDraw |
|
| 48 | + |
|
| 49 | +def watermark_img(img_path,res_path, text, pos): |
|
| 50 | + img = Image.open(img_path) |
|
| 51 | + wm = ImageDraw.Draw(img) |
|
| 52 | + col= (9, 3, 10) |
|
| 53 | + wm.text(pos, text, fill=col) |
|
| 54 | + img.show() |
|
| 55 | + img.save(res_path) |
|
| 56 | + |
|
| 57 | +img = 'initial.jpg' |
|
| 58 | +watermark_img(img, 'result.jpg','IshaanGupta', pos=(1, 0)) |
|
| 59 | +``` |
|
| 60 | + |
|
| 61 | + |
|
| 62 | +**3.) Plagiarism Checker** |
|
| 63 | +-------------------------- |
|
| 64 | + |
|
| 65 | +This script checks Plagiarism between two files. |
|
| 66 | + |
|
| 67 | +``` |
|
| 68 | +from difflib import SequenceMatcher |
|
| 69 | + |
|
| 70 | +def plagiarism_checker(f1,f2): |
|
| 71 | + with open(f1,errors="ignore") as file1,open(f2,errors="ignore") as file2: |
|
| 72 | + f1_data=file1.read() |
|
| 73 | + f2_data=file2.read() |
|
| 74 | + res=SequenceMatcher(None, f1_data, f2_data).ratio() |
|
| 75 | + |
|
| 76 | +print(f"These files are {res*100} % similar") |
|
| 77 | + |
|
| 78 | +f1=input("Enter file_1 path: ") |
|
| 79 | +f2=input("Enter file_2 path: ") |
|
| 80 | +plagiarism_checker(f1, f2) |
|
| 81 | +``` |
|
| 82 | + |
|
| 83 | + |
|
| 84 | +4.) File Encrypt and Decrypt |
|
| 85 | +---------------------------- |
|
| 86 | + |
|
| 87 | +A small script that can Encrypt/Decrypt any file. |
|
| 88 | + |
|
| 89 | +``` |
|
| 90 | +from cryptography.fernet import Fernet |
|
| 91 | + |
|
| 92 | +def encrypt(f_name, key): |
|
| 93 | + fernet = Fernet(key) |
|
| 94 | + |
|
| 95 | + with open(f_name, 'rb') as file: |
|
| 96 | + original = file.read() |
|
| 97 | + |
|
| 98 | + encrypted = fernet.encrypt(original) |
|
| 99 | + |
|
| 100 | + with open(f_name, 'wb') as enc_file: |
|
| 101 | + enc_file.write(encrypted) |
|
| 102 | + |
|
| 103 | +def decrypt(f_name, key): |
|
| 104 | + fernet = Fernet(key) |
|
| 105 | + |
|
| 106 | + with open(f_name, 'rb') as enc_file: |
|
| 107 | + encrypted = enc_file.read() |
|
| 108 | + |
|
| 109 | +decrypted = fernet.decrypt(encrypted) |
|
| 110 | + with open(f_name, 'wb') as dec_file: |
|
| 111 | + dec_file.write(decrypted) |
|
| 112 | + |
|
| 113 | +key = Fernet.generate_key() |
|
| 114 | + |
|
| 115 | +f_name = input("Enter Your filename: ") |
|
| 116 | +encrypt(f_name, key) |
|
| 117 | +decrypt(f_name, key) |
|
| 118 | +``` |
|
| 119 | + |
|
| 120 | + |
|
| 121 | +**5.) Making URLs short** |
|
| 122 | +------------------------- |
|
| 123 | + |
|
| 124 | +Sometimes big URLs become very annoying to read and share. This script uses an external API to short the URLs. |
|
| 125 | + |
|
| 126 | +``` |
|
| 127 | +from __future__ import with_statement |
|
| 128 | +import contextlib |
|
| 129 | +try: |
|
| 130 | + from urllib.parse import urlencode |
|
| 131 | +except ImportError: |
|
| 132 | + from urllib import urlencode |
|
| 133 | + |
|
| 134 | +try: |
|
| 135 | + from urllib.request import urlopen |
|
| 136 | +except ImportError: |
|
| 137 | + from urllib2 import urlopen |
|
| 138 | +import sys |
|
| 139 | + |
|
| 140 | +def make_tiny(url): |
|
| 141 | + request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':url})) |
|
| 142 | + with contextlib.closing(urlopen(request_url)) as response: |
|
| 143 | + return response.read().decode('utf-8') |
|
| 144 | + |
|
| 145 | +def main(): |
|
| 146 | + for tinyurl in map(make_tiny, sys.argv[1:]): |
|
| 147 | + print(tinyurl) |
|
| 148 | + |
|
| 149 | +if __name__ == '__main__': |
|
| 150 | + main() |
|
| 151 | +``` |