Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated the converter script. error checking and readablity #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions JPGtoPNGconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@
import os
from PIL import Image

path = sys.argv[1]
directory = sys.argv[2]

if not os.path.exists(directory):
os.makedirs(directory)


for filename in os.listdir(path):
clean_name = os.path.splitext(filename)[0]
img = Image.open(f'{path}{filename}')
#added the / in case user doesn't enter it. You may want to check for this and add or remover it.
img.save(f'{directory}/{clean_name}.png', 'png')
print('all done!')
def run():
if len(sys.argv) == 3: # check correct input
# The full path of the current dir > str
curpath = os.path.dirname(__file__) + "\\"
source = sys.argv[1] # name of the pokemon dir
newDirectory = sys.argv[2] # name of the new dir to create
try:
os.makedirs(curpath + newDirectory) # create new folder
print(f"{newDirectory[:-1]} created succesfuly")
loopDirectory(curpath + source, curpath + newDirectory) # start looping with full paths
print('all done!')
except OSError: # if makedirs fails
print("folder already exists")
else:
print(f"{sys.argv[0]} requires 2 args to run")

def loopDirectory(source, newDirectory):
for file in os.listdir(source):
clean_name = os.path.splitext(file)[0]
ext = os.path.splitext(file)[1]
if ext == '.jpg': # run only if the file is a .jpg image
try:
img = Image.open(source + file)
img.save(newDirectory + clean_name + '.png', 'png')
except:
print("somthing went wrong")

if __name__ == "__main__":
run()