Skip to content

Commit

Permalink
简单的修复 fkxxyz/ssfconv#20
Browse files Browse the repository at this point in the history
  • Loading branch information
RadND committed Sep 16, 2024
1 parent c1162af commit e9ee97d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[project]
name="ssfconv"
version="1.0.6"
version="1.0.7"
authors=[
{name="nihui"},
{name="VOID001"},
Expand Down
10 changes: 8 additions & 2 deletions src/ssfconv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ def conv():
# "dest", help="输出文件夹,默认与输入文件夹相同", nargs="?", default=None
# )
parser.add_argument(
"--type",
"-t",
"--type",
help="输出文件(夹)格式,默认 fcitx5",
default="fcitx5",
choices=["fcitx", "fcitx5"],
)
# parser.add_argument(
# "-f", "--force", help="强制覆盖输出文件夹的内容", action="store_true"
# )
parser.add_argument(
"-i",
"--install",
help="将转换结果移动到该格式皮肤的默认位置",
action="store_true",
)
args = parser.parse_args()

if not os.path.exists(args.src):
Expand Down Expand Up @@ -51,8 +57,8 @@ def unpackage():
"dest", help="输出文件夹名,默认与皮肤文件名相同", nargs="?", default=None
)
parser.add_argument(
"--type",
"-t",
"--type",
help="要解压的皮肤格式,默认 ssf",
default="ssf",
choices=["ssf"],
Expand Down
21 changes: 18 additions & 3 deletions src/ssfconv/convert/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from .out import ssf2fcitx, ssf2fcitx5


import sys
import os
import sys, os, shutil
import logging

default_skins_dir = {
"fcitx5": "~/.local/share/fcitx5/themes/",
"fcitx": "~/.config/fcitx/skin/",
}


def convert(args):
Expand All @@ -13,5 +18,15 @@ def convert(args):
err = ssf2fcitx(args.src)
case _:
assert False

if args.install:
logging.debug(
"os.pardir %s \n"
+ "args.dest %s \n"
+ "default_skins_dir[args.type] %s \n",
os.pardir,
args.dest,
default_skins_dir[args.type],
)
# shutil.move(os.pardir + args.dest, default_skins_dir[args.type] + args.dest)
pass
return err
124 changes: 65 additions & 59 deletions src/ssfconv/convert/image_operation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from PIL import Image, ImageDraw
import numpy as np
import logging


def getImageAvg(image_path, area=(0, 0, 0, 0)):
Expand All @@ -13,74 +14,79 @@ def getImageAvg(image_path, area=(0, 0, 0, 0)):
返回 (r,g,b) 三元组
"""

file = Image.open(image_path)
size = file.size

# 确定区域
x1 = area[0] % size[0]
x2 = area[1] % size[0]
y1 = area[2] % size[1]
y2 = area[3] % size[1]
if x2 == 0:
x2 = size[0]
if y2 == 0:
y2 = size[1]

if x1 > x2:
t = x1
x1 = x2
x2 = t
if y1 > y2:
t = y1
y1 = y2
y2 = t
if x1 == x2:
if x2 != size[0]:
x2 += 1
else:
x1 -= 1
if y1 == y2:
if y2 != size[1]:
y2 += 1
else:
y1 -= 1
with Image.open(image_path) as file:
size = file.size

# 算出区域内所有像素点的平均值
img = np.asarray(file)
r = g = b = 0
count = 0
"""
NOTE https://github.com/fkxxyz/ssfconv/issues/20
此处会溢出,不影响最终结果,问题不大
"""
if img.shape[2] == 4:
for y in range(y1, y2):
for x in range(x1, x2):
if img[y][x][3] > 0:
# 确定区域
x1 = area[0] % size[0]
x2 = area[1] % size[0]
y1 = area[2] % size[1]
y2 = area[3] % size[1]
if x2 == 0:
x2 = size[0]
if y2 == 0:
y2 = size[1]

if x1 > x2:
t = x1
x1 = x2
x2 = t
if y1 > y2:
t = y1
y1 = y2
y2 = t
if x1 == x2:
if x2 != size[0]:
x2 += 1
else:
x1 -= 1
if y1 == y2:
if y2 != size[1]:
y2 += 1
else:
y1 -= 1

# 算出区域内所有像素点的平均值
img = np.asarray(file)
r = g = b = 0
count = 0

# 有没有透明度?
if img.shape[2] == 4:
for y in range(y1, y2):
for x in range(x1, x2):
if img[y][x][3] > 0:
r += img[y][x][0]
g += img[y][x][1]
b += img[y][x][2]
count += 1
else:
for y in range(y1, y2):
for x in range(x1, x2):
r += img[y][x][0]
g += img[y][x][1]
b += img[y][x][2]
count += 1
else:
for y in range(y1, y2):
for x in range(x1, x2):
r += img[y][x][0]
g += img[y][x][1]
b += img[y][x][2]
count += 1
if count == 0:
count = 1
r //= count
g //= count
b //= count
return (r, g, b)

if count == 0:
count = 1
#https://github.com/fkxxyz/ssfconv/issues/20
r=int(r)
g=int(g)
b=int(b)

r //= count
g //= count
b //= count
return (r, g, b)


# 获取图片大小的函数
def getImageSize(image_file):
size = Image.open(image_file).size
assert size[0] > 0 and size[0] < 65536 and size[1] > 0 and size[1] < 65536
return size
with Image.open(image_file) as file:
size = file.size
assert size[0] > 0 and size[0] < 65536 and size[1] > 0 and size[1] < 65536
return size


# 保存一个多边形到文件
Expand Down

0 comments on commit e9ee97d

Please sign in to comment.