Code snippets & tools

Scripts

python_create_gif_using_several_images.py

>have a sequence of images named 1.png, 2.png, 3.png, and so on in a new folder
>place this script.py into the same folder with ONLY the image files
>run script.py x y
>x is the number of images and y is the name you want to give to the gif
import imageio
import os
import argparse
import sys
g = int(sys.argv[1])
print(g)
x=1
files = []
for x in range (x,g):
    print(x)
    files.append(str(x)+'.png')
    x+1
print(files)
images = [imageio.imread(file) for file in files]
imageio.mimwrite(str(sys.argv[2])+'.gif', images, fps=7)

python_windows_bg_grab.py

>grab the windows background files from this directory:
>C:\Users\USERNAME\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets
>place them in a new folder, place this script.py in the same folder, and run it.
import os
for file in os.listdir(path):
    if file =="script.py":
        pass
    else:
        os.rename(file, file+".png")
        print(file)

exif_scrub.py

#######Step 1: Imports
#pip install pillow https://pypi.org/project/Pillow/2.2.1/  
#And use the tkinter module that comes standard with your python install
#use PIL as a pointer to the pillow module because it is a fork of the now dead PIL module)
from PIL import Image, ExifTags

#use filedialog to select the image file using the OS directory instead of manually typing in the location
from tkinter import filedialog

#######Step 2: the main function
def main():
    # open the file dialog and choose the image to get the location string
    img = filedialog.askopenfilename()
    a = str(img.rsplit('/',1)[1])
    a = a.rsplit('.',1)[0]
    print(a)
    #we open the image with PIL
    img = Image.open(img)

    #use a try-except block to error handle in case there is no exif data
    try:
    
        #with the _getexif().items() we first try to obtain a dictionary of all the exif data on the image
        exif = { ExifTags.TAGS[k]: v for k, v in img._getexif().items() if k in ExifTags.TAGS }
        #if there is none we exit this block and move to the exception
        
        #we grab the image data without the exif data and set it to an object called data
        data = list(img.getdata())
        
        #create a new image using the same mode and size of the original image
        image_without_exif = Image.new(img.mode, img.size)
        
        #place back the pixel data into the new image
        image_without_exif.putdata(data)
        
        #initiate the orientation object 
        for orientation in ExifTags.TAGS.keys():
            if ExifTags.TAGS[orientation]=='Orientation':
                break
                
        #PIL does not copy metadata when saving a new image
        #So first read the metadata, grab the orientation, and set up the new image with
        #the correct orientation
        #this means we are adding our own metadata to the new image
        rotate=dict(img._getexif().items())
        if rotate[orientation] == 3:
            img=img.rotate(180, expand=True)
        elif rotate[orientation] == 6:
            img=img.rotate(270, expand=True)
        elif rotate[orientation] == 8:
            img=img.rotate(90, expand=True)
        else:
            pass
       
        #clean up and final save
        img.save(u"scrubbed_{}".format(a+'.jpg'))
        
    except Exception as e:
        #print the error
        print(e)

    return

if __name__=="__main__":
    main()

bookmark_cleaner.py

#cleaning the brave bookmark txt file for easy reading
def brave():
    a = open("Bookmarks.txt", "r", encoding='utf-8')
    new_file = open("bookmarks_cleaned.txt", "w", encoding='utf-8')
    for line in a:
        if "date_added" in line:
            pass
        elif '"guid":' in line:
            pass
        elif '"id":' in line:
            pass
        elif '"type":' in line:
            pass
        else:
            line = line.replace('"name":', '')
            line = line.replace('"url":', '')
            line = line.replace('"', '')
            line = line.replace('{', '')
            line = line.replace('}', '')
            line = line.replace(',', '')
            new_file.write(line)
        
if __name__=="__main__":
    brave()

check_sha1_of_file.py

#py script.py filename


import hashlib
import sys

def main(x):
	BLOCKSIZE = 65536
	hasher = hashlib.sha1()
	with open(str(x[1]), 'rb') as afile:
		buf = afile.read(BLOCKSIZE)
		while len(buf) > 0:
			hasher.update(buf)
			buf = afile.read(BLOCKSIZE)
		print(hasher.hexdigest())

if __name__=='__main__':

	main(sys.argv)