- python版本:python3.7
- 包版本
Pillow 7.2.0
opencv-python 4.4.0.46
redis 2.10.5
import os
import redis
import json
from PIL import Image
import requests
import cv2 as cv
def image_jpg(url, standard_brand_id):
"""
:param url:
:param standard_brand_id:
:param geshi: .jpg-->.jpg
:return:
"""
for i in range(3):
try:
r = requests.get(url)
if r.status_code == 200:
with open('D:/images/Brands_jpg/{standard_brand_id}.jpg'.format(standard_brand_id=standard_brand_id),
'wb') as f:
f.write(r.content)
f.close()
except Exception as n:
print(n)
def image_gif_jpg(url, standard_brand_id, geshi):
"""
:param url:
:param standard_brand_id:
:param geshi: .gif-->.jpg
:return:
"""
for i in range(3):
try:
r = requests.get(url)
if r.status_code == 200:
with open('D:/images/Brands_gif/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi),
'wb') as f:
f.write(r.content)
f.close()
gif = cv.VideoCapture(
'D:/images/Brands_gif/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi))
ret, frame = gif.read()
cv.imwrite('D:/images/Brands_jpg/{standard_brand_id}.jpg'.format(standard_brand_id=standard_brand_id),
frame)
except Exception as n:
print(n)
def image_jpeg_jpg(url, standard_brand_id, geshi):
"""
:param url:
:param standard_brand_id:
:param geshi: .jpeg-->.jpg
:return:
"""
for i in range(3):
try:
r = requests.get(url)
if r.status_code == 200:
with open(
'D:/images/Brands_jpeg/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi),
'wb') as f:
f.write(r.content)
f.close()
img = Image.open(
'D:/images/Brands_jpeg/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi))
img = img.convert('RGB')
img.thumbnail((128, 128))
img.save('D:/images/Brands_jpg/{standard_brand_id}.jpg'.format(standard_brand_id=standard_brand_id),
"JPEG", quality=80, optimize=True, progressive=True)
remove_image(
'D:/images/Brands_jpeg/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi))
except Exception as n:
print(n, url)
def image_png_jpg(url, standard_brand_id, geshi):
"""
:param url:
:param standard_brand_id:
:param geshi: .png-->.jpg
:return:
"""
for i in range(3):
try:
r = requests.get(url)
if r.status_code == 200:
with open('D:/images/Brands_png/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi),
'wb') as f:
f.write(r.content)
f.close()
img = Image.open(
'D:/images/Brands_png/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi))
img = img.convert('RGB')
img.save('D:/images/Brands_jpg/{standard_brand_id}.jpg'.format(standard_brand_id=standard_brand_id),
quality=95)
remove_image(
'D:/images/Brands_png/{standard_brand_id}.{geshi}'.format(standard_brand_id=standard_brand_id,
geshi=geshi))
except Exception as n:
print(n, url)
def remove_image(file_name):
"""
:param url:
:param standard_brand_id:
:param geshi: 删除改变格式前的图片
:return:
"""
if os.path.exists(file_name):
os.remove(file_name)
print('成功删除文件:', file_name)
else:
print('未找到此文件:', file_name)
if __name__ == '__main__':
redisPool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1)
client = redis.Redis(connection_pool=redisPool)
while True:
mm = client.lpop('pingpai')
if mm is None:
break
data = json.loads(mm)
item = {}
for key in data:
item[key] = data[key]
url = data['brand_logo_url']
standard_brand_id = data['standard_brand_id']
geshi = url.split('.')[-1]
print(standard_brand_id, geshi)
if geshi == 'gif' or geshi == 'GIF':
image_gif_jpg(url=url, standard_brand_id=standard_brand_id, geshi=geshi)
elif geshi == 'jpeg' or geshi == 'JPEG':
image_jpeg_jpg(url=url, standard_brand_id=standard_brand_id, geshi=geshi)
elif geshi == 'png' or geshi == 'PNG':
image_png_jpg(url=url, standard_brand_id=standard_brand_id, geshi=geshi)
else:
image_jpg(url=url, standard_brand_id=standard_brand_id)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)