Python E化-PS图像

Python E化-图像整理工具

autobook-17.4.1 压缩照片

# 项目:压缩照片

import os
from PIL import Image
print(os.getcwd())
#设定相对路径
file_address='.\\input\\'
file_address2='.\\output\\'

# 第 1 个功能:压缩照片
# 遍历所有文件并打开图像
SQUARE_FIT_SIZE = int(input('请输入导出照片的最大的长或宽(整数),少于该长度的照片将被跳过不处理 '))
os.makedirs(file_address, exist_ok=True) 
# os.path.abspath('.')  ---'C:\\Python34'
# os.path.abspath('.\\Scripts')  ---'C:\\Python34\\Scripts'
for filename in os.listdir(file_address):
    #print(filename)
    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 # skip non-image files
    im = Image.open(file_address+filename)
    width, height = im.size

    # 调整图像的大小 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))

        # 保存更改图像 Save changes.
        im.save(file_address2+filename)

    # 被跳过的照片将直接保存
    else:
        print('直接保存'+filename)
        im.save(file_address2+filename)

print('ok')

autobook-17.4.2 加LOGO

# 项目:加LOGO

import os
from PIL import Image
print(os.getcwd())
#设定相对路径
file_address = '.\\input\\'
file_address2 = '.\\output\\'
file_address3 = '.\\logo\\'
LOGO_FILENAME = 'catlogo.png'

# 第 0 步:调整logo图像的大小
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


# 第 1 个功能:加LOGO
# 遍历所有文件并打开图像
#SQUARE_FIT_SIZE = int(input('请输入导出照片的最大的长或宽(整数),少于该长度的照片将被跳过不处理  '))
os.makedirs(file_address, exist_ok=True) 
# os.path.abspath('.')  ---'C:\\Python34'
# os.path.abspath('.\\Scripts')  ---'C:\\Python34\\Scripts'
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 # skip non-image files
        
    im = Image.open(file_address+filename)
    width, height = im.size

    # 粘贴LOGO
    print('Adding logo to %s...' % (filename))
    im.paste(logoIm, (width - logoWidth-10, height - logoHeight-10), logoIm)
    # 保存更改图像 Save changes.
    im.save(file_address2+filename)


print('ok')

autobook-17.4.3 PS照片

# 项目: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'



# 第 1 个功能:PS照片
# 遍历所有文件并打开图像
#SQUARE_FIT_SIZE = int(input('请输入导出照片的最大的长或宽(整数),少于该长度的照片将被跳过不处理  '))
os.makedirs(file_address, exist_ok=True) 
# os.path.abspath('.')  ---'C:\\Python34'
# os.path.abspath('.\\Scripts')  ---'C:\\Python34\\Scripts'
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 # skip non-image files

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

    # PS照片
    # 旋转
    # img_rotate = im.rotate(10) 
    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))
    # 图片的Detail
    img_DETAIL = im.filter(ImageFilter.DETAIL)		
    img_DETAIL.save(file_address2+"DETAIL-{}".format(filename))
    # 图片的BLUR
    img_BLUR = im.filter(ImageFilter.BLUR)		
    img_BLUR.save(file_address2+"BLUR-{}".format(filename))
    # 图片的EDGE_ENHANCE
    img_EDGE_ENHANCE = im.filter(ImageFilter.EDGE_ENHANCE)		
    img_EDGE_ENHANCE.save(file_address2+"EDGE_ENHANCE-{}".format(filename))
    # 图片的SMOOTH
    img_SMOOTH = im.filter(ImageFilter.SMOOTH)		
    img_SMOOTH.save(file_address2+"SMOOTH-{}".format(filename))
    # 图片的EMBOSS
    img_EMBOSS = im.filter(ImageFilter.EMBOSS)		
    img_EMBOSS.save(file_address2+"EMBOSS-{}".format(filename))
    # 对比度为初始的10倍
    img_Contrast = ImageEnhance.Contrast(im).enhance(1.3)		
    img_Contrast.save(file_address2+"contrast-{}".format(filename))
    # Brightness为初始的10倍
    img_Brightness = ImageEnhance.Brightness(im).enhance(1.1)		
    img_Brightness.save(file_address2+"Brightness-{}".format(filename))
    # Color为初始的10倍
    img_Color = ImageEnhance.Color(im).enhance(1.3)		
    img_Color.save(file_address2+"Color-{}".format(filename))
    # Sharpness为初始的10倍
    img_Sharpness = ImageEnhance.Sharpness(im).enhance(1.3)		
    img_Sharpness.save(file_address2+"Sharpness-{}".format(filename))

    # 复合多种PS技术
    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   ')

# 第 1 个功能:加文字
# 遍历所有文件并打开图像
#SQUARE_FIT_SIZE = int(input('请输入导出照片的最大的长或宽(整数),少于该长度的照片将被跳过不处理  '))
os.makedirs(file_address, exist_ok=True) 
# os.path.abspath('.')  ---'C:\\Python34'
# os.path.abspath('.\\Scripts')  ---'C:\\Python34\\Scripts'
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 # skip non-image files

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

    # 添加文字
    draw = ImageDraw.Draw(im)
    #draw.text((20, 20), 'My Love', fill='purple')
    fontsFolder = 'C:\\Windows\\Fonts' # e.g. ‘/Library/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)
    #im=im.convert('RGB')
    #im.save(file_address2+filename)


print('ok')

猜你喜欢

转载自blog.csdn.net/m0_46629123/article/details/109085170