Python 萬能程式碼模版:批量搞圖,秀翻全場(上)

專注的阿熊發表於2021-10-07

# -*- coding: utf-8 -*-

# @Author: AI 悅創

# @Date:   2021-10-02 10:26:52

# @Last Modified by:   aiyc

# @Last Modified time: 2021-10-04 20:15:13

import cv2

import numpy

from PIL import Image, ImageDraw, ImageFont

import os

class WaterMark(object):

def __init__(self, OperationFilename=".", output_dir="watermark", textSize=10, watermarkText=" 水印 ", textColor="#ffffff", system=False, winfontfile=r"C:\Windows\Fonts\STZHONGS.ttf", macfontfile="/System/Library/Fonts/PingFang.ttc"):

self.OperationFilename = OperationFilename

self.output_dir = output_dir

self.textSize = textSize

self.watermarkText = watermarkText

self.textColor = textColor

self.system = system

self.winfontfile = winfontfile

self.macfontfile = macfontfile

def mkdirs(self):

if not os.path.exists(self.output_dir):

os.makedirs(self.output_dir)

print(f" 資料夾 {self.output_dir} 已經自動為你建立,圖片將儲存到: {self.output_dir}")

else:

print(f" 資料夾外匯跟單gendan5.com  {self.output_dir} 已經存在,圖片將儲存到: {self.output_dir}")

def system_font(self):

if not self.system:

return ImageFont.truetype(self.textSize, encoding="utf-8")

if self.system.upper() == "MAC":

# FontFilePath = "/System/Library/Fonts/PingFang.ttc"

return ImageFont.truetype(font=self.macfontfile, size=self.textSize, encoding="utf-8")

elif self.system.upper() == "WINDOWS":

# FontFilePath = r"C:\Windows\Fonts\STZHONGS.ttf"

return ImageFont.truetype(font=self.winfontfile, size=self.textSize, encoding="utf-8")

def parsepath(self):

path_lst = []

# a = os.walk("tips_3/")

root, dirs, files = next(os.walk(self.OperationFilename))

# root, dirs, files = next(os.walk("tips_3/"))

# print(list(a))

for item in files:

file_path = os.path.join(root, item)

# self.process_file(file_path)

path_lst.append(file_path)

return path_lst

def process_file(self, file_path):

img = cv2.imread(file_path)

image_shape = img.shape

height = image_shape[0]

width = image_shape[1]

# print(img.size)

if (isinstance(img, numpy.ndarray)):

img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

draw = ImageDraw.Draw(img)

fontStyle = self.system_font()

# 繪製文字

# textColor = (168, 121, 103)

draw.text((width/2, height-30), self.watermarkText, self.textColor, font=fontStyle)

# draw.text((width/2, height-30), self.watermarkText, fill=self.textColor, font=fontStyle)

# 轉換回 OpenCV 型別

img2 = cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)

# 儲存圖片

file_name = file_path.split("/")[-1]

cv2.imwrite(os.path.join(self.output_dir, file_name), img2)

print(f"proceed {file_path}")

def main(self):

self.mkdirs()

path_lst = self.parsepath()

# print(path_lst)

for path in path_lst:

self.process_file(path)

if __name__ == '__main__':

run = WaterMark(

OperationFilename="tips_3/",

output_dir="image_watermark",

textSize=10,

watermarkText="@ 黃家寶 |

textColor="gray",

system="Windows",

winfontfile="JiZiJingDianKaiTiJianFan-.ttf")

run.main()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2794802/,如需轉載,請註明出處,否則將追究法律責任。

相關文章