0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python による画像生成で Mac のクソダサデスクトップにさよなら・2

Last updated at Posted at 2022-07-14

前置き

以前、こちらで Mac の文字入り壁紙作成方法を検討しましたが、もっと便利にしたくなってきました。

テーブルで一挙に作成内容を指定

こんな感じ。

bglist.dat
#colorsets
colorset1,茜色,LightSalmon
colorset2,White,DeepSkyBlue
colorset3,DarkOliveGreen,OliveDrab
colorset4,Beige,DarkMagenta
colorset5,灰汁色,紺色
colorset6,Yellow,Tomato
colorset7,Gold,CadetBlue
colorset8,GreenYellow,MediumVioletRed
colorset9,MediumSeaGreen,茜色
colorset10,RosyBrown,LightGreen
colorset11,Wheat,DarkTurquoise
colorset12,CadetBlue,Chocolate
colorset13,Teal,茜色
#bglist
display_01.png,メイン,colorset1
display_02.png,論文作業,colorset2
display_03.png,コーディング,colorset3

カラーバリエーションの検討方法については、こちらにも書きました。

カラーコーディネートをランダムに

run_random.py 抜粋
colorset_random = random.sample(colorset,len(bglist))

#中略

    rgb_txt=find_color(colorset_random[i][1]).rgb
    rgb_bg=find_color(colorset_random[i][2]).rgb
#    for j in range(0,len(colorset)):
#        if bglist[i][2] == colorset[j][0]:
#            rgb_txt=find_color(colorset[j][1]).rgb
#            rgb_bg=find_color(colorset[j][2]).rgb
#            break

カラーセットを指定したい場合にはランダム指定をコメントアウトして、コメントアウト部分を復活します。

実行結果

1回目。

$ python3 run_random.py
(中略)
screen width =  1440
screen height =  900
# 0
out_file_name =  display_01.png
string_txt =  Teal
string_bg =  茜色
# 1
out_file_name =  display_02.png
string_txt =  White
string_bg =  DeepSkyBlue
# 2
out_file_name =  display_03.png
string_txt =  RosyBrown
string_bg =  LightGreen

以下が生成された画像サンプル。
sample_01.png

2回目。

$ python3 run_random.py
(中略)
screen width =  1440
screen height =  900
# 0
out_file_name =  display_01.png
string_txt =  Gold
string_bg =  CadetBlue
# 1
out_file_name =  display_02.png
string_txt =  Yellow
string_bg =  Tomato
# 2
out_file_name =  display_03.png
string_txt =  MediumSeaGreen
string_bg =  茜色

以下が生成された画像サンプル。
sample_02.png

気に入った組み合わせが出るまで何度でも色ガチャできます。

その他

再掲ですが、以下にご注意。

  • デスクトップの順番が入れ替わると不便なので入れ替え機能は外しておきましょう。

  • 「システム環境設定>デスクトップとスクリーンセーバー」のフォルダの項目に、生成画像が入っているフォルダを追加しておいた方がいいようです。
  • カラーコードの一覧は以下のサイトを参考にさせていただきました。

  • このページの記載内容について、改変して公開などはご自由に。

プログラム本文

上記の "bglist.dat" も同じフォルダ内に必要です。

run.py
import pyautogui as pag
from PIL import Image, ImageFont, ImageDraw
import cv2
import numpy as np
from color import *
import random

colorset=[]
bglist=[]
flag_bglist=False
f = open('bglist.dat', 'r')
f_lines = f.readlines()

for i in range(0,len(f_lines)):
    if f_lines[i].strip() == "#colorsets":
        continue
    if f_lines[i].strip() == "#bglist":
        flag_bglist = True
        continue
    if not flag_bglist:
        colorset.append(f_lines[i].strip().split(","))
    else:
        bglist.append(f_lines[i].strip().split(","))
f.close()

colorset_random = random.sample(colorset,len(bglist))

show_color_names() #利用可能な色名一覧を表示
rate_font_size = 0.1
rate_pos_txt = [0.05, 0.05]
font_dir = '/Users/(your username)/Library/Fonts/'
if ( font_dir == "" ):
    font_dir = "/System/Library/Fonts/"
font_file = 'sawarabi-gothic/sawarabi-gothic-medium.ttf'

screen = pag.size()
print("screen width = ",screen.width)
print("screen height = ",screen.height)

for i in range(0,len(bglist)):

    print("#",i)
    print("out_file_name = ",bglist[i][0])
    print("string_txt = ",colorset_random[i][1])
    print("string_bg = ",colorset_random[i][2])

    out_file_name = bglist[i][0]
    string_txt = bglist[i][1]
    rgb_txt=find_color(colorset_random[i][1]).rgb
    rgb_bg=find_color(colorset_random[i][2]).rgb
#    for j in range(0,len(colorset)):
#        if bglist[i][2] == colorset[j][0]:
#            rgb_txt=find_color(colorset[j][1]).rgb
#            rgb_bg=find_color(colorset[j][2]).rgb
#            break

    img = np.zeros((screen.height, screen.width, 3), np.uint8)
    for i in range(0,3):
        img[:, :, i] = rgb_bg[2-i]

    font_path = font_dir + font_file
    font_size = int(rate_font_size*screen.width)
    font = ImageFont.truetype(font_path, font_size)
    img = Image.fromarray(img) # cv2(NumPy)型の画像をPIL型に変換
    draw = ImageDraw.Draw(img)

    pos_txt = ( int(screen.height*rate_pos_txt[1]),int(screen.width*rate_pos_txt[0]) )
    color_txt =(rgb_txt[2],rgb_txt[1],rgb_txt[0],0)
    draw.text(pos_txt, string_txt, font=font, fill=color_txt)
    img = np.array(img) # PIL型の画像をcv2(NumPy)型に変換

    cv2.imwrite(out_file_name, img)
color.py
COLORS = {
    "IndianRed": (205,92,92),
    "LightCoral": (240,128,128),
    "Salmon": (250,128,114),
    "DarkSalmon": (233,150,122),
    "LightSalmon": (255,160,122),
    "Crimson": (220,20,60),
    "Red": (255,0,0),
    "FireBrick": (178,34,34),
    "DarkRed": (139,0,0),
    "Pink": (255,192,203),
    "LightPink": (255,182,193),
    "HotPink": (255,105,180),
    "DeepPink": (255,20,147),
    "MediumVioletRed": (199,21,133),
    "PaleVioletRed": (219,112,147),
    "Coral": (255,127,80),
    "Tomato": (255,99,71),
    "OrangeRed": (255,69,0),
    "DarkOrange": (255,140,0),
    "Orange": (255,165,0),
    "Gold": (255,215,0),
    "Yellow": (255,255,0),
    "LightYellow": (255,255,224),
    "LemonChiffon": (255,250,205),
    "LightGoldenrodYellow": (250,250,210),
    "PapayaWhip": (255,239,213),
    "Moccasin": (255,228,181),
    "PeachPuff": (255,218,185),
    "PaleGoldenrod": (238,232,170),
    "Khaki" : (240,230,140),
    "DarkKhaki": (189,183,107),
    "Lavender": (230,230,250),
    "Thistle": (216,191,216),
    "Plum": (221,160,221),
    "Violet" : (238,130,238),
    "Orchid": (218,112,214),
    "Fuchsia": (255,0,255),
    "Magenta": (255,0,255),
    "MediumOrchid": (186,85,211),
    "MediumPurple": (147,112,219),
    "Amethyst": (153,102,204),
    "BlueViolet": (138,43,226),
    "DarkViolet": (148,0,211),
    "DarkOrchid": (153,50,204),
    "DarkMagenta": (139,0,139),
    "Purple": (128,0,128),
    "Indigo": (75,0,130),
    "SlateBlue": (106,90,205),
    "DarkSlateBlue": (72,61,139),
    "GreenYellow": (173,255,47),
    "Chartreuse": (127,255,0),
    "LawnGreen": (124,252,0),
    "Lime": (0,255,0),
    "LimeGreen": (50,205,50),
    "PaleGreen": (152,251,152),
    "LightGreen": (144,238,144),
    "MediumSpringGreen": (0,250,154),
    "SpringGreen": (0,255,127),
    "MediumSeaGreen": (60,179,113),
    "SeaGreen": (46,139,87),
    "ForestGreen": (34,139,34),
    "Green": (0,128,0),
    "DarkGreen": (0,100,0),
    "YellowGreen": (154,205,50),
    "OliveDrab": (107,142,35),
    "Olive": (128,128,0),
    "DarkOliveGreen": (85,107,47),
    "MediumAquamarine": (102,205,170),
    "DarkSeaGreen": (143,188,143),
    "LightSeaGreen": (32,178,170),
    "DarkCyan": (0,139,139),
    "Teal": (0,128,128),
    "Aqua": (0,255,255),
    "Cyan" : (0,255,255),
    "LightCyan": (224,255,255),
    "PaleTurquoise": (175,238,238),
    "Aquamarine": (127,255,212),
    "Turquoise": (64,224,208),
    "MediumTurquoise": (72,209,204),
    "DarkTurquoise": (0,206,209),
    "CadetBlue": (95,158,160),
    "SteelBlue": (70,130,180),
    "LightSteelBlue": (176,196,222),
    "PowderBlue": (176,224,230),
    "LightBlue": (173,216,230),
    "SkyBlue": (135,206,235),
    "LightSkyBlue": (135,206,250),
    "DeepSkyBlue": (0,191,255),
    "DodgerBlue": (30,144,255),
    "CornflowerBlue": (100,149,237),
    "MediumSlateBlue": (123,104,238),
    "RoyalBlue": (65,105,225),
    "Blue": (0,0,255),
    "MediumBlue": (0,0,205),
    "DarkBlue": (0,0,139),
    "Navy": (0,0,128),
    "MidnightBlue": (25,25,112),
    "Cornsilk": (255,248,220),
    "BlanchedAlmond": (255,235,205),
    "BlanchedAlmond": (255,228,196),
    "NavajoWhite": (255,222,173),
    "Wheat": (245,222,179),
    "BurlyWood": (222,184,135),
    "Tan": (210,180,140),
    "RosyBrown": (188,143,143),
    "SandyBrown": (244,164,96),
    "Goldenrod": (218,165,32),
    "DarkGoldenrod": (184,134,11),
    "Peru": (205,133,63),
    "Chocolate": (210,105,30),
    "SaddleBrown": (139,69,19),
    "Sienna": (160,82,45),
    "Brown": (165,42,42),
    "Maroon": (128,0,0),
    "White": (255,255,255),
    "Snow": (255,250,250),
    "Honeydew": (240,255,240),
    "MintCream": (245,255,250),
    "Azure": (240,255,255),
    "AliceBlue": (240,248,255),
    "GhostWhite": (248,248,255),
    "WhiteSmoke": (245,245,245),
    "Seashell": (255,245,238),
    "Beige": (245,245,220),
    "OldLace": (253,245,230),
    "FloralWhite": (255,250,240),
    "Ivory": (255,255,240),
    "AntiqueWhite": (250,235,215),
    "Linen": (250,240,230),
    "LavenderBlush": (255,240,245),
    "MistyRose": (255,228,225),
    "Gainsboro": (220,220,220),
    "LightGrey": (211,211,211),
    "Silver": (192,192,192),
    "DarkGray": (169,169,169),
    "Gray": (128,128,128),
    "DimGray": (105,105,105),
    "LightSlateGray": (119,136,153),
    "SlateGray": (112,128,144),
    "DarkSlateGray": (47,79,79),
    "Black": (0,0,0),
    "紺色": (35,59,108),
    "藍色": (22,94,131),
    "桔梗色": (106,76,156),
    "江戸紫": (116,83,153),
    "鶯色": (145,141,64),
    "灰汁色": (188,176,156),
    "亜麻色": (214,198,175),
    "山吹色": (248,180,0),
    "小豆色": (152,81,75),
    "えんじ色": (179,66,74),
    "茜色": (177,53,70),
}


class Color:
  def __init__(self, name, rgb):
    self.name = name
    self.r, self.g, self.b = self.rgb = rgb
    self.code = f'#{self.r:02x}{self.g:02x}{self.b:02x}'


def color_table_full():
    return [Color(name, rgb) for name, rgb in COLORS.items()]


def find_color(name):
    return Color(name, COLORS[name]) if name in COLORS else None


def show_color_names():
    print(*COLORS)


def color_table_random(num_color):

    import random

    color_table = color_table_full()

    result = []

    for i in range(0,num_color):
        result.append(color_table[random.randrange(len(color_table))])

    return result
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?