# -*- coding: utf-8 -*-
from io import BytesIO
from PIL import Image
import requests
import math
import json
import oss2
import uuid
import logging
import time
import _thread
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='imagePillow.log',
filemode='w')
newPath='/Users/lvdapeng/PycharmProjects/dataProcessing/image_pillow/images/'
bash_path='/Users/lvdapeng/PycharmProjects/dataProcessing/image_pillow/directory/err_fail.json'
auth = oss2.Auth('*******', '************')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com', '*****')
bucketname='*******/'
headers = {"content-type": "application/json"}
def get_imageUrl():
file = open(bash_path, 'r')
count = 0
for line in file.readlines():
json_line = json.loads(line)
#print(json_line['picName'])
count += 1
#line=line.strip("\n")
if count > 500:
break
get_ImageSize(json_line['picName'],json_line)
def get_ImageSize(line,msg):
try:
response = requests.get("https:"+line)
#response = requests.get("https://img.alicdn.com/imgextra/T1T94dFxRhXXXXXXXX_!!0-item_pic.jpg")
image = Image.open(BytesIO(response.content), 'r')
print(image.size)
# print(image.width) # 长度
# print(image.height) # 宽度
#print(image.format) # 格式
if image.width>800 and image.height>800 :
w, h = 800, 800 # 去掉浮点,防报错
img = image.resize((w, h), Image.ANTIALIAS)
uid = str(uuid.uuid1())
imaName = ''.join(uid.split('-'))
img.save(newPath+imaName+"."+image.format, optimize=True, quality=85) # 质量为85效果最好
# 上传本地文件
res = bucket.put_object_from_file(bucketname+imaName+"."+image.format, newPath+imaName+"."+image.format)
logging.info('Thread:(%d) msg:%s Code:%d ImaName:%s s\n' % (_thread.get_ident(), msg, res.status, imaName + "." + image.format))
elif image.width>800 and image.height<800:
w, h = math.trunc(800), math.trunc(800/image.width * image.height) #去掉浮点,防报错
img = image.resize((w, h), Image.ANTIALIAS)
uid = str(uuid.uuid1())
imaName = ''.join(uid.split('-'))
img.save(newPath+imaName+"."+image.format, optimize=True, quality=85) # 质量为85效果最好
res = bucket.put_object_from_file(bucketname + imaName + "." + image.format, newPath + imaName + "." + image.format)
logging.info('Thread:(%d) msg:%s Code:%d ImaName:%s s\n' % (_thread.get_ident(), msg, res.status, imaName + "." + image.format))
elif image.width<800 and image.height>800:
w, h = math.trunc(800 / image.height * image.width),math.trunc(800) # 去掉浮点,防报错
img = image.resize((w, h), Image.ANTIALIAS)
uid = str(uuid.uuid1())
imaName = ''.join(uid.split('-'))
img.save(newPath+imaName+"."+image.format, optimize=True, quality=85) # 质量为85效果最好
res = bucket.put_object_from_file(bucketname + imaName + "." + image.format, newPath + imaName + "." + image.format)
logging.info('Thread:(%d) msg:%s Code:%d ImaName:%s s\n' % (_thread.get_ident(), msg, res.status, imaName + "." + image.format))
else:
#上传网络流
input = requests.get("https:"+line)
uid = str(uuid.uuid1())
imaName = ''.join(uid.split('-'))
res = bucket.put_object(bucketname+imaName+"."+image.format,input)
logging.info('Thread:(%d) msg:%s Code:%d ImaName:%s s\n' % (_thread.get_ident(), msg, res.status, imaName + "." + image.format))
except Exception as e:
logging.info('Thread:(%d) msg:%s Error:%s\n'
% (_thread.get_ident(), msg, e))
if __name__=='__main__':
get_imageUrl()
|