在Python中为猫图像添加徽标

时间:2018-08-23 22:37:24

标签: python image-processing

对于字幕,这里是代码(由自动完成无聊的东西提供),我会对其进行一些调整。

import os
from PIL import Image

SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo.png'

logoIm = Image.open(LOGO_FILENAME)
logoWidth, logoHeight = logoIm.size

# Loop over all files in the working directory.
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 itself

    im = Image.open(filename)
    width, height = im.size

    # Add logo.
    print('Adding logo to %s...' % (filename))
    im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)

    # Save changes.
    im.save('Cat with Logo.png')

Cat Logo

Cat Image

由于某种原因,徽标未能在末尾添加。 save命令有问题吗?

2 个答案:

答案 0 :(得分:0)

我意识到resizeAndAddLogo.py会调整徽标粘贴到的文件的大小,而不是徽标大小与该文件成比例。我们不想要那个。因此,我更改了脚本,以将徽标大小与图像文件的比率更改为1/5。

...
# Resize the logo.
print(f'Resizing logo to fit {filename}...')
sLogo = logoIm.resize((int(width / 5), int(height / 5)))
sLogoWidth, sLogoHeight = sLogo.size

# Add the logo.
print(f'Adding logo to {filename}...')
im.paste(sLogo, (width - sLogoWidth, height - sLogoHeight), sLogo)
...

这时,我不需要SQUARE_FIT_SIZE = 300,所以我删除了它并使代码更短。这是我的完整剧本。

import os
from PIL import Image

LOGO_FILENAME = 'catlogo.png'
logoIm = Image.open(LOGO_FILENAME)
os.makedirs('withLogo', exist_ok=True)

# Loop over all files in the working directory.
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

    # Resize the logo.
    print(f'Resizing logo to fit {filename}...')
    sLogo = logoIm.resize((int(width / 5), int(height / 5)))
    sLogoWidth, sLogoHeight = sLogo.size

    # Add the logo.
    print(f'Adding logo to {filename}...')
    im.paste(sLogo, (width - sLogoWidth, height - sLogoHeight), sLogo)

    # Save changes.
    im.save(os.path.join('withLogo', filename))

请注意,应将调整大小的徽标分配给循环使用。

这是结果图像之一。

enter image description here

答案 1 :(得分:0)

@harryghgim 你的想法很棒,但我认为这个项目的重点是将所有图片与徽标统一为一个尺寸,因此结合您的解决方案,我已经将徽标添加到图片中,然后使用 SQUARE_FIT_SIZE = 300< /p>

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)
#TODO: loop over all files
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
#TODO: Resize the logo.
    print(f'Resizing logo to fit {filename}...')
    sLogo = logoIm.resize((int(width / 5), int(height / 5)))
    sLogoWidth, sLogoHeight = sLogo.size

#TODO: Add logo
    print('Dodanie logo do obrazu %s...' % (filename))
    im.paste(sLogo, (width - sLogoWidth, height - sLogoHeight), sLogo)
# TODO: check if there is a need to resize picture
    if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
# TODO: count new width and height
        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
# TODO: change image size
        print('Zmiana wielkości obrazu %s...' % (filename))
        im = im.resize((width, height))
#TODO: save images
        im.save(os.path.join('withLogo', filename))