給你的圖片加上水印

#! python3
# -*- coding:utf-8 -*-
# resizeAndAddLogo.py - Resizes all images in current working directory to fit
# in a 300*30 square

import os
from PIL import Image

SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo.png'	# 填上你要做成水印的圖片位置(最好是絕對位置)

logoIm = Image.open(LOGO_FILENAME)	
logoWidth, logoHeight = logoIm.size
# logoIm = logoIm.resize((int(logoWidth / 8), int(logoHeight / 8)))
# logoWidth, logoHeight = logoIm.size

os.makedirs('WithLogo', exist_ok = True)
# Loop over all files in the working directory.這裏遍歷的是工作目錄
for filename in os.listdir('.'):
	filename = filename.lower()
	if not (filename.endswith('.png') or filename.endswith('.jpg')\
	or filename.endswith('.gif') or filename.endswith('.bmp'))\
	or filename == LOGO_FILENAME:
		continue	# skip non-image files and logo file itself
		
	im = Image.open(filename)
	width, height = im.size
	
	if (logoWidth > width or logoHeight > height)\
	or (logoWidth * 2 > width or logoHeight * 2 > height):
		continue	# skip, if logo size too big
		
	# Check if image needs to be resized.
	if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
		# Calculate the new width and height to resize to
		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
			
		# Resize the image.
		print('Resizing %s...' % (filename))
		im = im.resize((width, height))
		# Add the logo
		print('Adding logo to %s...' % filename)
		im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)
		
		
		# Save changes.
		im.save(os.path.join('WithLogo', filename))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章