1 Unstar Star 0 Fork 0

fuwu360 / fuwu360-toolsPython

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
fuwu360-tools spread retract

Clone or download
rename_taobao_images.py 5.28 KB
Copy Edit Web IDE Raw Blame History
# !/usr/bin/python3
# -*- coding: utf-8 -*-
"""
本py程序的作用:丛淘宝助理导出的数据再导入到网店
注意:操作之前必须先备份数据库,以防万一
请在下面的__init__方法里面修改数据库连接的账号和相关信息
"""
import datetime
import logging
import os
import sys
import pymysql
from PIL import Image
class TaobaoAssistantImages():
def __init__(self):
# 设置日志
self.set_logging()
# 数据库ip
self.host = 'localhost'
# 数据库名称
self.database_name = 'fuwu360'
# 数据库用户名
user = 'root'
# 数据库密码
passwd = 'root'
# 数据库表前缀
self.table_prifix = 'ecs_'
# 淘宝助理导出来的图片目录。比如说你导出的csv文件是taobao.csv,则图片目录是taobao
self.old_images_dir = '../taobao'
# host是数据库连接ip或域名
self.conn = pymysql.connect(host=self.host, user=user, passwd=passwd,
db=self.database_name,
port=3306, charset='utf8',
cursorclass=pymysql.cursors.DictCursor,
connect_timeout=7200)
self.conn.autocommit(False)
self.cursor = self.conn.cursor()
# 缩略图配置
self.images_config_dict = self.images_config()
# 设置日志
def set_logging(self) -> None:
today = datetime.date.today().strftime("%Y-%m-%d")
logging.basicConfig(
# 控制台打印的日志级别
level=logging.INFO,
# 日志格式
format='%(asctime)s %(funcName)s [line:%(lineno)d] %(levelname)s %(message)s',
# 日志文件名
filename=os.path.join(os.getcwd(), 'taobao_assistant_images' + '-' + today + '.txt'),
# 模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志, a是追加模式,默认如果不写的话,就是追加模式
filemode='a'
)
def image_size_off(self):
cwd = os.getcwd()
root_dir = cwd + os.path.sep + self.old_images_dir + os.path.sep
thumb_dir = cwd + os.path.sep + "thumb_img" + os.path.sep
if not os.path.exists(thumb_dir):
os.makedirs(thumb_dir)
for lists in os.listdir(root_dir):
# 需要什么格式的图片自己手动改改就好了
if lists[lists.rfind('.'):].lower() == '.jpg':
path = os.path.join(root_dir, lists)
img = Image.open(path)
if img.mode != "RGB":
img = img.convert('RGB')
box = self.clipimage(img.size)
region = img.crop(box)
size = (int(self.images_config_dict.get('thumb_width')), int(self.images_config_dict.get('thumb_height')))
region.thumbnail(size, Image.ANTIALIAS)
# 这里保存thumbnail以后的结果
region.save(
os.path.join(thumb_dir, lists))
# 重命名文件
def rename_images(self):
cwd = os.getcwd()
path = cwd + os.path.sep + self.old_images_dir + os.path.sep
count = 0
for root, dirs, files in os.walk(path):
for file in files:
# 分离文件名字和后缀
portion = os.path.splitext(file)
# 根据后缀来修改,如无后缀则空
if portion[1] == ".tbi":
# 要改的新后缀
old_name = root + file
new_name = old_name.replace(".tbi", ".jpg")
os.rename(old_name, new_name)
count += 1
print("一共重命名了" + str(count) + "个文件")
# 取宽和高的值小的那一个来生成裁剪图片用的box
# 并且尽可能的裁剪出图片的中间部分,一般人摄影都会把主题放在靠中间的,个别艺术家有特殊的艺术需求我顾不上
def clipimage(self, size):
width = int(size[0])
height = int(size[1])
if (width > height):
dx = width - height
box = (dx / 2, 0, height + dx / 2, height)
else:
dx = height - width
box = (0, dx / 2, width, width + dx / 2)
return box
# 从数据库的配置表里面查询缩略图的宽和高
def images_config(self):
select_shop_config_sql = f"""
select code, value from {self.table_prifix}shop_config WHERE code in
('thumb_width', 'thumb_height', 'image_width', 'image_height')
"""
self.cursor.execute(select_shop_config_sql)
rows = self.cursor.fetchall()
images_config_dict = dict()
for row in rows:
images_config_dict.update({row.get('code'): row.get('value')})
return images_config_dict
if __name__ == "__main__":
taobao_assistant_images = TaobaoAssistantImages()
# 开始重命名文件,把tbi文件改为jpg
taobao_assistant_images.rename_images()
# 缩略图操作
taobao_assistant_images.image_size_off()
# 数据库连接提交
taobao_assistant_images.conn.commit()
# 关闭数据库连接
taobao_assistant_images.cursor.close()
# 退出程序
sys.exit(0)

Comment ( 0 )

Sign in for post a comment

Python
1
https://gitee.com/fuwu360/fuwu360-tools.git
git@gitee.com:fuwu360/fuwu360-tools.git
fuwu360
fuwu360-tools
fuwu360-tools
master

Search

131423 f1aaba0b 1899542 094922 1c74bed3 1899542