![]() |
Adding logo to the image - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Adding logo to the image (/thread-16320.html) |
Adding logo to the image - Truman - Feb-23-2019 #! python3 # resizeandaddlogo.py - Resizes all images in the current working directory to fit # in a 300x300 square, and adds 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 # skip non-image files and the logo file 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(f'Resizing {filename}.') im = im.resize((width, height)) print(f'Adding log to {filename}...') im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm) im.save(os.path.join('withLogo', filename)) Does anyone understand what exactly is wrong with line 34?
RE: Adding logo to the image - Yoriz - Feb-23-2019 Looks like the lines print(f'Adding log to {filename}...') im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm) im.save(os.path.join('withLogo', filename))should be indented to be part of the for loop. RE: Adding logo to the image - Truman - Feb-24-2019 after applying your suggestion I get indentation error... RE: Adding logo to the image - Yoriz - Feb-24-2019 Align the indent correctly to be part of the for loop, in line with the first 'if', 4 spaces. RE: Adding logo to the image - Truman - Feb-25-2019 #! python3 # resizeandaddlogo.py - Resizes all images in the current working directory to fit # in a 300x300 square, and adds 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 # skip non-image files and the logo file 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(f'Resizing {filename}.') im = im.resize((width, height)) print(f'Adding log to {filename}...') im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm) im.save(os.path.join('withLogo', filename)) Always some problem of this kind...and I never use tab.
|