diff options
Diffstat (limited to 'Chap17ProjAddingLogo.py')
-rw-r--r-- | Chap17ProjAddingLogo.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Chap17ProjAddingLogo.py b/Chap17ProjAddingLogo.py new file mode 100644 index 0000000..aca50ff --- /dev/null +++ b/Chap17ProjAddingLogo.py @@ -0,0 +1,37 @@ +#! python3 + +# Chapter 17 Project Adding a Logo +# Resizes all images in current directory to fit in a 300x300 square, +# and adds a catlogo.png to the lower-right corner. + +import os +from PIL import Image + +SQUARE_FIT_SIZE = 300 +LOGO_FILENAME = 'catlogo.png' + +logoIm = Image.open(LOGO_FILENAME) +logoWidth, logoHeight = logoIm.size + +os.makedirs('withLogo', exist_ok=True) + +for filename in os.listdir('.'): + if not (filename.endswith('.png') or filename.endswith('.jpg')) \ + or filename == LOGO_FILENAME: + continue + im = Image.open(filename) + width, height = im.size + if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE: + if width > height: + height = int((SQUARE_FIT_SIZE / width) * height) + width = SQUARE_FIT_SIZE + else: + width = int((SQUARE_FIT_SIZE / height) * width) + height = SQUARE_FIT_SIZE + print('Resizing %s...' % (filename)) + im = im.resize((width, height)) + + print('Adding logo to %s...' % (filename)) + im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm) + + im.save(os.path.join('withLogo', filename)) |