Python E化-图像整理工具
autobook-17.4.1 压缩照片
import os
from PIL import Image
print(os.getcwd())
file_address='.\\input\\'
file_address2='.\\output\\'
SQUARE_FIT_SIZE = int(input('请输入导出照片的最大的长或宽(整数),少于该长度的照片将被跳过不处理 '))
os.makedirs(file_address, exist_ok=True)
for filename in os.listdir(file_address):
if not (filename.endswith('.png') or filename.endswith('.PNG')
or filename.endswith('.jpg') or filename.endswith('.JPG')
or filename.endswith('.gif') or filename.endswith('.GIF')
or filename.endswith('.tif') or filename.endswith('.TIF')
or filename.endswith('.bmp') or filename.endswith('.BMP')):
continue
im = Image.open(file_address+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('Resizing %s...' % (filename))
im = im.resize((width, height))
im.save(file_address2+filename)
else:
print('直接保存'+filename)
im.save(file_address2+filename)
print('ok')
autobook-17.4.2 加LOGO
import os
from PIL import Image
print(os.getcwd())
file_address = '.\\input\\'
file_address2 = '.\\output\\'
file_address3 = '.\\logo\\'
LOGO_FILENAME = 'catlogo.png'
logosize = input('请输入一个数字n,表示LOGO的长和宽是原来的1/n ')
logoIm0 = Image.open(file_address3 + LOGO_FILENAME)
logoWidth0, logoHeight0 = logoIm0.size
logoIm = logoIm0.resize((int(logoWidth0 / int(logosize)), int(logoHeight0 / int(logosize))))
logoWidth, logoHeight = logoIm.size
os.makedirs(file_address, exist_ok=True)
for filename in os.listdir(file_address):
if not (filename.endswith('.png') or filename.endswith('.PNG')
or filename.endswith('.jpg') or filename.endswith('.JPG')
or filename.endswith('.gif') or filename.endswith('.GIF')
or filename.endswith('.tif') or filename.endswith('.TIF')
or filename.endswith('.bmp') or filename.endswith('.BMP')):
print(filename+'被跳过')
continue
im = Image.open(file_address+filename)
width, height = im.size
print('Adding logo to %s...' % (filename))
im.paste(logoIm, (width - logoWidth-10, height - logoHeight-10), logoIm)
im.save(file_address2+filename)
print('ok')
autobook-17.4.3 PS照片
import os
from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
print(os.getcwd())
file_address = '.\\input\\'
file_address2 = '.\\output\\'
file_address3 = '.\\logo\\'
LOGO_FILENAME = 'catlogo.png'
os.makedirs(file_address, exist_ok=True)
for filename in os.listdir(file_address):
if not (filename.endswith('.png') or filename.endswith('.PNG')
or filename.endswith('.jpg') or filename.endswith('.JPG')
or filename.endswith('.gif') or filename.endswith('.GIF')
or filename.endswith('.tif') or filename.endswith('.TIF')
or filename.endswith('.bmp') or filename.endswith('.BMP')):
print(filename+'被跳过')
continue
im = Image.open(file_address+filename)
width, height = im.size
img_rotate = im.rotate(10, Image.BICUBIC,1)
img_rotate.save(file_address2+"rotate-{}".format(filename))
img_convert=im.convert('L')
img_convert.save(file_address2+"gray-{}".format(filename))
img_CONTOUR = im.filter(ImageFilter.CONTOUR)
img_CONTOUR.save(file_address2+"contour-{}".format(filename))
img_DETAIL = im.filter(ImageFilter.DETAIL)
img_DETAIL.save(file_address2+"DETAIL-{}".format(filename))
img_BLUR = im.filter(ImageFilter.BLUR)
img_BLUR.save(file_address2+"BLUR-{}".format(filename))
img_EDGE_ENHANCE = im.filter(ImageFilter.EDGE_ENHANCE)
img_EDGE_ENHANCE.save(file_address2+"EDGE_ENHANCE-{}".format(filename))
img_SMOOTH = im.filter(ImageFilter.SMOOTH)
img_SMOOTH.save(file_address2+"SMOOTH-{}".format(filename))
img_EMBOSS = im.filter(ImageFilter.EMBOSS)
img_EMBOSS.save(file_address2+"EMBOSS-{}".format(filename))
img_Contrast = ImageEnhance.Contrast(im).enhance(1.3)
img_Contrast.save(file_address2+"contrast-{}".format(filename))
img_Brightness = ImageEnhance.Brightness(im).enhance(1.1)
img_Brightness.save(file_address2+"Brightness-{}".format(filename))
img_Color = ImageEnhance.Color(im).enhance(1.3)
img_Color.save(file_address2+"Color-{}".format(filename))
img_Sharpness = ImageEnhance.Sharpness(im).enhance(1.3)
img_Sharpness.save(file_address2+"Sharpness-{}".format(filename))
img_multiPS = im.rotate(0, Image.BICUBIC,1)
img_multiPS = ImageEnhance.Brightness(img_multiPS).enhance(1.1)
img_multiPS = ImageEnhance.Color(img_multiPS).enhance(1.1)
img_multiPS = ImageEnhance.Sharpness(img_multiPS).enhance(1.1)
img_multiPS.save(file_address2+"multiPS-{}".format(filename))
print('ok')
autobook-17.4.4 加文字
import os
from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
from PIL import ImageFont
from PIL import ImageDraw
print(os.getcwd())
file_address = '.\\input\\'
file_address2 = '.\\output\\'
file_address3 = '.\\logo\\'
LOGO_FILENAME = 'catlogo.png'
MyWord=input('请输入需要添加的文字 ')
Myword_Size=int(input('根据图片的像素来输入文字的大小(整数),建议30以上 '))
Myword_Color=input('请输入文字的颜色,如yellow,gray,red,blue,green,blace,white ')
os.makedirs(file_address, exist_ok=True)
for filename in os.listdir(file_address):
if not (filename.endswith('.png') or filename.endswith('.PNG')
or filename.endswith('.jpg') or filename.endswith('.JPG')
or filename.endswith('.gif') or filename.endswith('.GIF')
or filename.endswith('.tif') or filename.endswith('.TIF')
or filename.endswith('.bmp') or filename.endswith('.BMP')):
print(filename+'被跳过')
continue
im = Image.open(file_address+filename)
width, height = im.size
draw = ImageDraw.Draw(im)
fontsFolder = 'C:\\Windows\\Fonts'
arialFont = ImageFont.truetype(os.path.join(fontsFolder, 'arial.ttf'), Myword_Size)
draw.text((20, 20), MyWord, fill=Myword_Color, font=arialFont)
im.save(file_address2+filename)
print('ok')