小脚本

2019-01-22  本文已影响0人  JohnSnow_b20c
针对美术给出的图片命名和代码中不一致的问题, 特地写了个脚本, 来进行批量修改
{
    "bind_dialog_bg" : "binding_dialog",
    "tips_bg" : "alert_dialog",
    "tips_nav" : "button_cancel",
    "tips_pos" : "button_okay",
    "tips_unable" : "button_disabled",
    "b_guanbi" : "button_close"
}
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import sys
import json
from typing import Union

if len(sys.argv) == 1:
    print("请传入图片所在目录")
    sys.exit()

file_source = sys.argv[1]
print (file_source)

pic_rename_json = {}
# 提供一个json文件, 映射
with open('json绝对路径', encoding='utf-8') as f:
    pic_rename_json = json.loads(f.read())
    print (pic_rename_json)

for file in os.listdir(file_source):
    # 找出图片的特征, 将不满足特征的过滤掉
    is_need_rename = ("~ipad" in file) or ("@2x" in file) or ("@3x" in file)
    if is_need_rename == False:
        continue

    # 获取@或者~之前的字符串和范围
    b = file.find("@")
    c = file.find("~")
    extension = ""
    file_name = ""
    if b > 0:
        extension = file[b:]
        file_name = file[:b]
    elif c > 0:
        extension = file[c:]
        file_name = file[:c]
    else:
        pass

    if file_name in pic_rename_json:
        new_file = pic_rename_json[file_name] + extension
        old_file_full_path = os.path.join(file_source, file)
        new_file_full_path = os.path.join(file_source, new_file)
        # print(new_file)
        os.rename(old_file_full_path, new_file_full_path)
把Assets.xcassets中的所有图片提取到指定文件夹
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import sys
import shutil
from typing import Union

if len(sys.argv) == 1:
    print("请传入图片所在目录")
    sys.exit()

file_source = sys.argv[1]
print (file_source)

# 设置存放图片路径
destination_file_dir = file_source + "../cw_pics"
# 如果存在该路径, 则先删除该目录
if os.path.exists(destination_file_dir):
    shutil.rmtree(destination_file_dir)
# 创建目录
os.mkdir(destination_file_dir)
print (destination_file_dir)
for parent, folders, files in os.walk(file_source):
    for file in files:
        old_file_full_path = os.path.join(parent, file)
        if file.endswith(".png"):
            shutil.copy(old_file_full_path, destination_file_dir)

将指定文件夹中的图片, 纵向拼接为一张(需求场景是: 一张发票打印在A4纸中太大, 两张一起打印比较合适)
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
from PIL import Image

UNIT_WIDTH = 2479 # 图像的宽
UNIT_HEIGHT = 1750 # 图像的高
TARGET_WIDTH = UNIT_WIDTH #
TARGET_HEIGHT = 3508 #
STEP = 2

path = "/Users/WangRuzhou/Desktop/1"
imagefile = []
for root, dirs, files in os.walk(path):
    for f in files :
        # 找出图片的特征, 将不满足特征的过滤掉
        is_need_rename = (".jpg" in f) or (".png" in f) or (".JPG" in f) or (".PNG" in f)
        if is_need_rename == False:
            continue
        imagefile.append(Image.open(path+'/'+f))

b = [imagefile[i:i+STEP] for i in range(0, len(imagefile), STEP)]

for idx, images in enumerate(b):
    target = Image.new('RGB', (TARGET_WIDTH, TARGET_HEIGHT), (255,255,255))  # 最终拼接的图像的大小为210*297
    for index, image in enumerate(images):
        temp = image.resize((UNIT_WIDTH, UNIT_HEIGHT), Image.ANTIALIAS)
        value = index % 2
        if value == 0:
            target.paste(temp, (0, 0, UNIT_WIDTH, UNIT_HEIGHT))
            pass
        else:
            pass
            target.paste(temp, (0, (TARGET_HEIGHT - UNIT_HEIGHT), UNIT_WIDTH, TARGET_HEIGHT))

        quality_value = 100
        dest_image = '/result%s.jpg' % (idx)
        target.save(path+dest_image, quality = quality_value)
print 'done'

上一篇下一篇

猜你喜欢

热点阅读