Intro:
Hello, I'm a python beginner. I haven't had anyone use my code or anything. I want to improve and become a "professional python developer". I thank you for taking the time to help me along that path.
Question:
In context of this forum my specific question is; regarding my codes clarity of purpose. Does my code clearly demonstrate it's use, requirements, and results? If you were handed this in a package to update or maintain would you need to reference an expanded help doc?
Regardless of the answer, if you feel you have useful feedback on improving the readability of the code please let me know. Thank you very much.
The blog link:
https://valtyrtriit.blogspot.com/2021/12/python-wallpaper-scroller-entry-1-rough.html
Blog has a video demonstration of the code in action.
Optional Assistance:
- looking for a code portfolio review. I was told I should start a code blog with some python projects for 'other-eyes' to see. I'm the only one reading my code so I have no idea what it's quality is. I would appreciate any advice on the blog format / presentation / readability / topic / credits what have you.
Pre-Post Realizations:
As it does while considering the presentation of my work. I realize the unpack_gif() also stores, gets the gif_fps, and plays the image as well. This could be three different functions to isolate responsibility.
The source code:
import os # To create folders and save gif frames
import ctypes # To set the windows background image
import glob # To pull file contents of a directory
from PIL import Image # To parse gif data
def import_images( direct:str, fext: [ str ] ) -> { str : [ str ] }:
"""Takes a directory and a list of files of image type to include. Returns a dictionary with the file extension as a key and a array of image names."""
"""Prime the dictionary with the { key(extension) : value(empty array) } """
img_dict = { }
for extension in fext:
img_dict.update( { extension : [] } )
"""Use glob() to pull in all files of extension at path, clean the name string and append it to appropriate key : array."""
for extension in fext:
for filename in glob.glob( f'{ direct }\\*.{ extension }' ):
file = filename.replace( f"{ direct }\\", "" )
img_dict[extension].append( file )
return img_dict
def set_background( path:str ) -> None:
"""Takes in a absolute path and call the OS to set background image."""
ctypes.windll.user32.SystemParametersInfoW(
20, # System wallpaper index
0, # Buffer
f"{ path }", # File Absolute Path
0 # Final Buffer
)
def unpack_gif( directory:str, image:str, temp:str="temp_frames" ) -> None:
"""Takes in a directory path, an image name.gif, and a new temp folder name. Then parses out the gif's frames into the temp folder at directory. Once a valid path is available set that background to path."""
image_path = os.path.join(directory, image)
with Image.open( image_path ) as im: # Open the gif with pillow
temp_folder = temp # Name of temporary gif frames folder
folder_path = os.path.join(directory, temp_folder)
try:
os.mkdir( folder_path ) # Create the temporary folder to hold the gifs frames
except: pass
frame_duration = im.info['duration'] # Get gif run length in milliseconds
img_fps = ( ( frame_duration / im.n_frames ) / 1000 ) # Get the time per frame in milliseconds and convert it to seconds.
for frame in range(im.n_frames): # Get the number of frames in the gif
im.seek( frame ) # The current frame in for loop 0 - end_frame
name = f'temp_gif_{frame}.png' # Create a name for the frame image
path = os.path.join(folder_path, name)
im.save( path, 'GIF' ) # Save the frame image to the temporary folder
set_background( path ) # Now we have a path to set the background, call the set function
time.sleep( img_fps * 10 ) # slow down to not congest the wallpaper assignment
=
on type-hinted kwargs look amazingly weird. \$\endgroup\$