Skip to content

Instantly share code, notes, and snippets.

@sakage24
Last active August 30, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakage24/1b6a2a22c0a4672bf92eb6f9f49c5885 to your computer and use it in GitHub Desktop.
Save sakage24/1b6a2a22c0a4672bf92eb6f9f49c5885 to your computer and use it in GitHub Desktop.
ウォーターマーク入れる君

ウォーターマーク入れる君とは?

自身が作成したり描いた画像に対し、薄い透かしと透かし文字(以下、ウォーターマークと呼びます)を入れて新規作成するプログラムです。 元の画像には一切手を加えませんのでご安心下さい。

本プログラムはPython3.6.2を用いて作成されました。 ソースコードを公開していますので、知識のある方は以下から自由に改造していただいても結構です

ウォーターマーク入れる君

使い方

  1. ウォーターマークを入れる君.exeを起動して下さい。もしくはご自身でsourcesフォルダを作成して下さい。
  2. sourcesフォルダが作成されますので、その中にウォーターマークを入れたい画像を放り込んで下さい。
  3. ウォーターマークを入れる君.exeを起動して下さい。黒い窓が立ち上がります。枚数によりますがそれなりに時間がかかるかもしれません。
  4. destフォルダが作成されます。その中に透かしと透かし文字を入れた画像が新規作成されます。

config.iniの設定方法

透かし文字の色だったり、文字サイズだったり変換元のフォルダの場所とかを記述しています。 自由に設定して頂いて結構ですが、一応バックアップを取ってから修正することをオススメ致します。

[files]
# 変換元の画像を入れるフォルダの名前
sources_dir = sources
# 変換先の画像を入れるフォルダの名前
dest_dir = dest
[texts]
# 透かし文字
text = www.kiwi-bird.xyz
# 透かし文字の色
text_color = gray
# 透かし文字のサイズ
font_size = 48
# 使用するフォント名
fonts = arial.ttf

注意

  • 本プログラムを利用して発生したあらゆる問題に対し、作者は何の責任も負いません。
  • 画像は元の画像を直接編集するのではなく、新規作成してから編集しますので元の画像には影響を及ぼしません。
  • 透かし文字に日本語を含めないで下さい。文字化けします。
  • config.ini中に日本語を含めるとエラー落ちします。
  • スクリプトを直接実行する場合、事前にPython3.6.2をインストールした上で、pip install pillowをコマンドラインから実行して下さい。
    • 上記処理は.exe形式を利用する場合は必要ありません。

開発者情報

Author

Operation KIWI

[files]
sources_dir = sources
dest_dir = dest
[texts]
text = www.kiwi-bird.xyz
text_color = gray
font_size = 48
fonts = arial.ttf
from cx_Freeze import setup, Executable
author = 'kiwi-bird'
version = '1.0.0'
name = 'ウォーターマークを入れるだけの人生'
base = None
includes = ["PIL"]
excludes = []
packages = []
include_files = ["README.md", "config.ini"]
setup(name=name,
author=author,
version=version,
url='http://www.kiwi-bird.xyz/',
description="守ろう著作権、守ろう股間の正義",
options={
"build_exe": {
"includes": includes,
"excludes": excludes,
"include_files": include_files,
"packages": packages,
}
},
executables=[Executable("sukashi.py",
base=base,
targetName="ウォーターマークを入れる君.exe")])
from PIL import Image, ImageFont, ImageDraw
import os, sys
import configparser
cfg = configparser.ConfigParser()
cfg.read("config.ini")
# 変換元フォルダ
sources_dir = cfg["files"]["sources_dir"]
# 変換先フォルダ
dest_dir = cfg["files"]["dest_dir"]
# 透かし文字
text = cfg["texts"]["text"]
# 文字色
text_color = cfg["texts"]["text_color"]
# 使用するフォントファイル
fonts = cfg["texts"]["fonts"]
# 文字サイズ
font_size = int(cfg["texts"]["font_size"])
# 透かしの色と明度
img_color = (0, 0, 0, 160)
# 検索する画像の拡張子
ext_lists = ("jpg", "jpeg", "png", "gif")
def text_center(width=0):
if width:
return width / 4
else:
return 0
if not os.path.exists(sources_dir):
os.mkdir(sources_dir)
input(f"{sources_dir}フォルダを作成しました。この中に画像を放り込んでから再度起動して下さい。")
sys.exit(0)
elif not os.listdir(sources_dir):
input(f"{sources_dir}フォルダ内にファイルが存在しません。この中に画像を放り込んでから再度起動して下さい。")
sys.exit(0)
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
for file in os.listdir(sources_dir):
root, ext = os.path.splitext(file)
ext = ext.lower()
if ext[1:] in ext_lists:
img = Image.open(os.path.join(sources_dir, file)).convert("RGBA")
width, height = img.size
# 文字の座標("横(px)", "縦(px)")
text_position = (text_center(width=width), height / 2)
img_object = Image.new("RGBA", img.size, color=img_color)
draw = ImageDraw.Draw(img_object)
arial_font = ImageFont.truetype(fonts, font_size)
draw.text(xy=text_position, text=text, fill=text_color, font=arial_font)
output = Image.alpha_composite(img, img_object)
output.save(os.path.join(dest_dir, root) + ".png")
else:
print(f"{file}は画像ではありません。パスします。")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment