Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding logo to the image
#1
#! 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))
Error:
Traceback (most recent call last): File "C:\Python36\lib\site-packages\PIL\Image.py", line 1974, in save format = EXTENSION[ext] KeyError: '' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python36\kodovi\resizeandaddlogo.py", line 34, in <module> im.save(os.path.join('withLogo', filename)) File "C:\Python36\lib\site-packages\PIL\Image.py", line 1976, in save raise ValueError('unknown file extension: {}'.format(ext)) ValueError: unknown file extension:
Does anyone understand what exactly is wrong with line 34?
Reply
#2
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.
Reply
#3
after applying your suggestion I get indentation error...
Reply
#4
Align the indent correctly to be part of the for loop, in line with the first 'if', 4 spaces.
Reply
#5
#! 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))
Error:
C:\Python36\kodovi>resizeandaddlogo.py File "C:\Python36\kodovi\resizeandaddlogo.py", line 28 im = im.resize((width, height)) ^ TabError: inconsistent use of tabs and spaces in indentation
Always some problem of this kind...and I never use tab.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logo language code into python aayushi98 2 66,540 Jan-26-2021, 09:02 PM
Last Post: Serafim
  Adding markers to Folium map only adding last element. tantony 0 2,124 Oct-16-2019, 03:28 PM
Last Post: tantony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020