# The resizeAndAddLogo.py program in this chapter works with PNG and JPEG files,
# but Pillow supports many more formats than just these two. Extend
# resizeAndAddLogo.py to process GIF and BMP images as well.
#
# Another small issue is that the program modifies PNG and JPEG files only if their
# file extensions are set in lowercase. For example, it will process zophie.png but
# not zophie.PNG. Change the code so that the file extension check is case insensitive.
#
# Finally, the logo added to the bottom-right corner is meant to be just a small mark,
# but if the image is about the same size as the logo itself, the result will look
# ugly. Modify resizeAndAddLogo.py so that the image must be at least
# twice the width and height of the logo image before the logo is pasted. Otherwise,
# it should skip adding the logo.

@@ -822,8 +822,1341 @@
 e logo.%0A
+%0Aimport os%0Afrom PIL import Image%0A%0ASQUARE_FIT_SIZE = 300%0ALOGO_FILENAME = '../catlogo.png'%0AEXTENSIONS = %5B'.png', '.jpg', '.gif', '.bmp'%5D%0AFOLDER = '../'%0A%0AlogoIm = Image.open(LOGO_FILENAME)%0AlogoWidth, logoHeight = logoIm.size%0A%0Aos.makedirs(os.path.join(FOLDER, 'withLogo'), exist_ok=True)%0A# Loop over all files in the working directory.%0Afor filename in os.listdir(FOLDER):%0A    if not filename.endswith(tuple(EXTENSIONS)) or filename == LOGO_FILENAME:%0A        continue  # skip non-image files and the logo file itself%0A%0A    im = Image.open(os.path.join(FOLDER, filename))%0A    width, height = im.size%0A%0A    # Check if image needs to be resized.%0A    if width %3E SQUARE_FIT_SIZE and height %3E SQUARE_FIT_SIZE:%0A        # Calculate the new width and height to resize to.%0A        if width %3E height:%0A            height = int((SQUARE_FIT_SIZE / width) * height)%0A            width = SQUARE_FIT_SIZE%0A        else:%0A            width = int((SQUARE_FIT_SIZE / height) * width)%0A            height = SQUARE_FIT_SIZE%0A%0A        # Resize the image.%0A        print('Resizing %25s...' %25 filename)%0A        im = im.resize((width, height))%0A%0A    # Add the logo.%0A    print('Adding logo to %25s...%5Cn' %25 filename)%0A    im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)%0A%0A    # Save changes.%0A    im.save(os.path.join(os.path.join(FOLDER, 'withLogo'), filename))%0A

Update P1_remixResizeAndAddLogo added new extensions and process files from FOLDER