Pillow图片处理

我有一个以压榨折磨员工为己乐的极品领导,TA总甩给我一些枯燥乏味、耗时费力的案头工作,还天天让我爬网上给TA找免费图片写公众号。这不,今天又丢过来一任务,让我把公司自己拍的照片和制作的美图都加上公司logo。

我在心里一个劲儿滴翻白眼:你用了人家多少免费图啊?就你那公众号,图片都快比文字多啦!现在有点照片,不想着奉献出来回馈大众,反而要麻烦我加标记!真是个只知索取哒自私鬼,哼,鄙视你!!!

“得令~”,我笑容满面滴接受任务,还特意把音调提高好让声音接收方误认为我此刻满怀喜悦,要知道领导都喜欢勤快滴小伙计哒,我必须把自己伪装成内样子。

打开文件夹,头瞬间变成三个大,这么...这么多!如果一张一张加logo,做到晚上也做不完呀,做不完的话晚上就追不了琅邪榜啦......哦,不对,应该是琅琊(同“牙”音,学艺不精,之前视频把这字念错了,抱歉抱歉)。

看来得用我的秘密武器python来完成了,一直不敢让我的奇葩领导知道我会python,要不TA一定会扔给我更多工作哒。

python好像有个模块可以处理图片......打开搜索引擎敲入“how to add logo on photos python”,出来的搜索结果打开几个看下知道那个模块叫pillow,然后找来pillow的技术文档扫两眼。

看起来pillow模块可以帮我们对图片做些简单的处理,比如获取图片属性、建立新图片、打开图片文件、剪切粘贴图片、调节图片大小、图片的旋转和翻转、提取设置像素、画些简单图形、写字(字体大小颜色)...如果再配合上循环条件,批量处理图片没问题。

automate the boring stuff上有个类似的程序,先搬过来,有了大框架再修修补补工作量会少很多。

因为要把整个文件夹下的图片都加logo,所以还需要一个对文件和文件夹操作的模块。以前用的是os模块,不过最近我换成pathlib模块了,需要用到对文件或文件夹的哪项操作我就去pathlib模块的技术文档里去搜哪像操作。如果看不明白不知道怎么用,就用搜索引擎把需求一敲,github和stackoverflow上基本都有供参考的代码。

出错是难免的,除了监督程序是不是按设定在走,程序中的变量是不是按预计在变化,还可以直接把错误提示拷贝下来放到搜索引擎上去搜,看别人的解决方案对我们解决自己的问题是很有启发滴。

就这样,折腾了一会儿,新鲜的程序热乎乎出笼了,一秒钟就把文件夹里那“许多”的图片处理完毕。

小拍我哼着小曲,开着我的贴牌宝马自行车奔向地铁站,回家路上还要记得打包烤冷面和煎饼果子,再裹个大棉被,这样的刷剧状态才完美。

哦,对了,我的python程序已经设定好半夜12点会给老板发封汇报工作的邮件,邮件就说加好logo的图片存在某某文件夹下,不知道我那个笨蛋领导能不能体会到这个时间点发邮件的深层含义。

对图片的基本操作
 


指定文件夹下所有图片加logo
 


画布上画图写字
 


本篇配的是美国西部风情画家杜安(Duane Bryers)笔下的胖妞“希尔达”,跟文字未见得般配,可是我太喜欢希尔达,她太喜幸啦。

from PIL import ImageColor

# Get image color
# print('Red: {}'.format(ImageColor.getcolor('red', 'RGBA')))
# print('RED: {}'.format(ImageColor.getcolor('RED', 'RGBA')))
# #
# print('Black: {}'.format(ImageColor.getcolor('Black', 'RGBA')))
# #
# print('chocolate: {}'.format(ImageColor.getcolor('chocolate', 'RGBA')))
# print('CornflowerBlue: {}'.format(ImageColor.getcolor('CornflowerBlue', 'RGBA')))
# #
# print('Pink: {}'.format(ImageColor.getcolor('Pink', 'RGBA')))

# RGB color decimal code
# https://www.rapidtables.com/web/color/RGB_Color.html

from PIL import Image

# Get image data
dollIm = Image.open('doll.jpg')

print('Size: {}'.format(dollIm.size))
width, height = dollIm.size
print('Width: {}\nHeight: {}'.format(width, height))
#
print('Filename: {}'.format(dollIm.filename))
#
print('Format: {}'.format(dollIm.format))
#
print('Description: {}'.format(dollIm.format_description))
#
dollIm.save('doll_duplicate.png')


from PIL import Image

# new image
# im = Image.new('RGBA', (100, 200), 'purple')
# im.save('purpleImage.png')
#
# im2 = Image.new('RGBA', (20, 20))
# im2.save('transparentImage.png')

# crop image and paste
# dollIm = Image.open('doll.jpg')
# #
croppedIm = dollIm.crop((266,71,500,566))
# croppedIm.save('cropped.jpg')
# #
# dollCopyIm = dollIm.copy()
# #
# dollCopyIm.paste(croppedIm, (0,0))
# dollCopyIm.paste(croppedIm, (700,300))
# #
# dollCopyIm.save('pasted.jpg')

# Pasting Transparent Pixels
# reindeerIm = Image.open('reindeer.png')
# dollCopyIm.paste(reindeerIm, (0, 200))
# dollCopyIm.paste(reindeerIm, (500, 50), reindeerIm)
#
# dollCopyIm.save('transparentBackground.jpg')

# tiled balloon
hotAirBalloonIm = Image.open('hotAirBalloon.jpg')

balloonIm = hotAirBalloonIm.crop((627,164,755,322))
balloonIm.save('balloonIm.jpg')

hotAirBalloonImWidth, hotAirBalloonImHeight = hotAirBalloonIm.size
balloonWidth, balloonHeight = balloonIm.size

hotAirBalloonCopy = hotAirBalloonIm.copy()

for left in range(0, hotAirBalloonImWidth, balloonWidth):
	for top in range(0, hotAirBalloonImHeight, balloonHeight):
		hotAirBalloonCopy.paste(balloonIm, (left,top))

hotAirBalloonCopy.save('tiled.jpg')


from PIL import Image

ArwenIm = Image.open('Arwen.jpg')

# resize an image
# print(ArwenIm.size)

width, height = ArwenIm.size

quartersizedIm = ArwenIm.resize((int(width/2), int(height/2)))
# quartersizedIm.save('ArwenQuartersized.jpg')
# print(quartersizedIm.size)
#
# sveltIm = ArwenIm.resize((width, height+200))
# sveltIm.save('ArwenSvelte.jpg')

# rotate an image
ArwenIm.rotate(90).save('ArwenRotated90.jpg')
# ArwenIm.rotate(180).save('ArwenRotated180.jpg')
# ArwenIm.rotate(270).save('ArwenRotated270.jpg')

# expand=True keyword argument
# ArwenIm.rotate(10).save('ArwenRotated10.jpg')
# ArwenIm.rotate(10, expand=True).save('ArwenRotated10_extend.jpg')

# mirror flip
ThranduilIm = Image.open('thranduil.jpg')
#
ThranduilIm.transpose(Image.FLIP_LEFT_RIGHT).save('ThranduilHorizontal_flip.jpg')
# ThranduilIm.transpose(Image.FLIP_TOP_BOTTOM).save('ThranduilVertical_flip.jpg')
# ThranduilIm.rotate(180).save('ThranduilRotated180.jpg')


from PIL import Image

im = Image.new('RGBA', (100, 100))
print('transparent background: {}'.format(im.getpixel((0, 0))))

for x in range(100):
	for y in range(50):
		im.putpixel((x, y), (20, 165, 210))
#
from PIL import ImageColor
#
for x in range(100):
	for y in range(50, 100):
		im.putpixel((x, y), ImageColor.getcolor('violet', 'RGBA'))
#
print('upper part: {}'.format(im.getpixel((0, 0))))
print('lower part: {}'.format(im.getpixel((0, 50))))
#
im.save('putPixel.png')


# # addLogo.py - Add logo to the lower-right corner.
#
from pathlib import Path
from PIL import Image
#
# # image folder
PATH = '/Users/Smonkey/Documents/Python/PythonABC_Online/2-16image/toAddLogo/'
LOGOFILENAME = 'transparentLogo.png'
#
# # function to resize big logo image
def resizeLogo(p):
#
    originalLogoIm = Image.open(str(p))
    w, h = originalLogoIm.size
#
    nWidth, nHeight = int(w/3), int(h/3)
#
#     # nFilename: path of smallLogo.pnp
    nFilename = str(p.parent/'withLogo'/'smallLogo.png')
#
    originalLogoIm.resize((nWidth, nHeight)).save(nFilename)
#
    return nWidth, nHeight, nFilename
#
# # set up new folder for image with logo
imagePath = Path(PATH)
#
# # setup new folder 'withLogo'
imageWithLogoFolder = imagePath.joinpath('withLogo')
imageWithLogoFolder.mkdir(777, exist_ok=True,)
#
# # resize big logo image
logoWidth, logoHeight, logo = resizeLogo(imagePath/LOGOFILENAME)
#
logoIm = Image.open(logo)
#
# # Loop over all files in the working directory.
for fname in imagePath.iterdir():
# for fname in [x for x in imagePath.iterdir() if x.is_file]:
    filename = fname.name
    if not (filename.endswith('.png') or filename.endswith('.jpg')) or filename == LOGOFILENAME:
        continue # skip non-image files
#
    imageLocation = PATH + filename
    im = Image.open(imageLocation)
    width, height = im.size
#
    # Add logo.
    print('Adding logo to {}...'.format(filename))
    im.paste(logoIm, (width - logoWidth, height - logoHeight), logoIm)
#
    # Save changes.
    im.save(str(imagePath.joinpath('withLogo').joinpath(filename)))
#
Path(logo).unlink()


from PIL import Image, ImageDraw

# drawing shapes
# im = Image.new('RGBA', (200, 200), 'white')
# draw = ImageDraw.Draw(im)
#
# draw.line([(0,0), (199,0), (199, 199), (0,199), (0,0)], fill='pink', width=10)
# draw.rectangle((20,30,60,60), fill='blue')
# draw.ellipse((120,30,160,60), fill='red')
# draw.polygon(((57,87),(79,62),(94,85),(120,90),(103,113)), fill='brown', outline='green')
# #
# for i in range(100,200,10):
# 	draw.line([(i,0), (200,i-100)], fill='purple')
# im.save('drawing.png')

# drawing text
from PIL import ImageFont

im = Image.new('RGBA', (200,200), 'white')
draw = ImageDraw.Draw(im)

draw.text((20,150), 'Hello', fill='purple')

# fontsFolder = '/Library/Fonts/'
# SignPainterFont = ImageFont.truetype(fontsFolder+'SignPainter.ttc', 32)

SignPainterFont = ImageFont.truetype('SignPainter.ttc', 32)

# On Windows: C:\Windows\Fonts; On OS X: /Library/Fonts

draw.text((100,150), 'Howdy', fill='gray', font=SignPainterFont)
im.save('text.png')

1 1 1 1 1 1 1 1 1 1 Rating 3.33 (6 Votes)