python下载图片函数 python批量下载图片代码
请教如何用python下载文件到本地
知道文件的url地址就用urllib模块的urlretrieve函数。urllib.urlretrieve(url,
创新互联专注于企业成都全网营销、网站重做改版、永德网站定制设计、自适应品牌网站建设、html5、商城系统网站开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为永德等各大城市提供网站开发制作服务。
filename)filename是要保存到本地的文件名。函数后面还有2个可选参数,要用就看帮助文档吧。多线下载的话,每一线程要指定下载服务器上文件的哪一块。http协议中head里可以指定Range。下面用的是urllib2模块request
=
urllib2.Request(url)request.add_header("Range",
"bytes=%d-%d"%(1024,
204)
#指定下载文件的范围opener
=
urllib2.build_opener()data
=
opener.open(request).read()现在data里面就是文件的1024字节到2048字节的内容。
python爬虫大神帮忙看一下啊,使用 urllib.urlretrieve()下载不到图片,具体图片看下图代码
更改一下地址啊,因为你要下载的是图片,所以要用图片后缀,改成:
work_path = "E:/" + str(imgname) + ".jpg"
urllib.request.urlretrieve(imgurl, work_path)
这个imgname你定义一个循环的名字即可
有大神知道怎么使用python 往ftp服务器上连续上传下载多张图片吗?
例:下载、上传文件
# coding: utf-8
from ftplib import FTP
import time
import tarfile
import os
# !/usr/bin/python
# -*- coding: utf-8 -*-
from ftplib import FTP
def ftpconnect(host, username, password):
ftp = FTP()
#
(host, 21)
(username, password)
return ftp
#从ftp下载文件
def downloadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'wb')
('RETR ' + remotepath, fp.write, bufsize)
fp.close()
#从本地上传文件到ftp
def uploadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'rb')
('STOR ' + remotepath, fp, bufsize)
fp.close()
if __name__ == "__main__":
ftp = ftpconnect("113.105.139.xxx", "ftp***", "Guest***")
downloadfile(ftp, "Faint.mp4", "C:/Users/Administrator/Desktop/test.mp4")
#调用本地播放器播放下载的视频
os.system('start "C:\Program Files\Windows Media Player\wmplayer.exe" "C:/Users/Administrator/Desktop/test.mp4"')
uploadfile(ftp, "C:/Users/Administrator/Desktop/test.mp4", "test.mp4")
python读取保存多帧图片数量少了
cv2.imshow("left", img_left)
filename3=str(number)+'n3'+'.jpg' #打印第number张图片+增值方式+保存类型
cv2.imwrite(savedpath + filename3, img_left)
"""
# 数据增强实现
"""
import cv2
import numpy as np
import os
# 图像平移
def img_translation(image):
# 图像平移 下、上、右、左平移
M = np.float32([[1, 0, 0], [0, 1, 100]])
img_down = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, 0], [0, 1, -100]])
img_up = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, 100], [0, 1, 0]])
img_right = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, -100], [0, 1, 0]])
img_left = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
# 保存图片,需要保存上述的哪一图片,就在cv2.imwrite()中,将哪一图片名放入。
# filename='xxx' +'.jpeg'
# cv2.imwrite(savedpath + filename, img_left)
# 显示图形
cv2.imshow("down", img_down)
filename0=str(number)+'n0'+'.jpg'
cv2.imwrite(savedpath + filename0, img_down)
cv2.imshow("up", img_up)
filename1=str(number)+'n1'+'.jpg'
cv2.imwrite(savedpath + filename1, img_up)
cv2.imshow("right", img_right)
filename2=str(number)+'n2'+'.jpg'
cv2.imwrite(savedpath + filename2, img_right)
cv2.imshow("left", img_left)
filename3=str(number)+'n3'+'.jpg'
cv2.imwrite(savedpath + filename3, img_left)
# 图像缩放
def img_scale(image):
result = cv2.resize(image, (224, 224))
cv2.imshow("scale", result)
filename=str(number)+'n5'+'.jpg'
cv2.imwrite(savedpath + filename, result)
# 图像翻转
def img_flip(image):
# 0以X轴为对称轴翻转,0以Y轴为对称轴翻转, 0X轴Y轴翻转
horizontally = cv2.flip(image, 0) # 水平镜像
vertically = cv2.flip(image, 1) # 垂直镜像
hv = cv2.flip(image, -1) # 水平垂直镜像
# 显示图形
cv2.imshow("Horizontally", horizontally)
filename1=str(number)+'n6'+'.jpg'
cv2.imwrite(savedpath + filename1, horizontally)
cv2.imshow("Vertically", vertically)
filename2=str(number)+'n7'+'.jpg'
cv2.imwrite(savedpath + filename2, vertically)
cv2.imshow("Horizontally Vertically", hv)
filename3=str(number)+'n8'+'.jpg'
cv2.imwrite(savedpath + filename3, hv)
# 图像旋转
def img_rotation(image):
# 原图的高、宽 以及通道数
rows, cols, channel = image.shape
# 绕图像的中心旋转
# 参数:旋转中心 旋转度数 scale
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 30, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated = cv2.warpAffine(image, M, (cols, rows))
# 显示图像
cv2.imshow("rotated", rotated)
filename1=str(number)+'n9'+'.jpg'
cv2.imwrite(savedpath + filename1, rotated)
#选装60度
W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated1 = cv2.warpAffine(image, W, (cols, rows))
cv2.imshow("rotated", rotated)
filename2=str(number)+'n12'+'.jpg'
cv2.imwrite(savedpath + filename2, rotated1)
#选装145度
W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)
# 参数:原始图像 旋转参数 元素图像宽高
rotated2 = cv2.warpAffine(image, W, (cols, rows))
cv2.imshow("rotated", rotated)
filename3=str(number)+'n13'+'.jpg'
cv2.imwrite(savedpath + filename3, rotated2)
# 图像加噪
def img_noise(image, mean=0, var=0.001):
'''
添加高斯噪声
mean : 均值
var : 方差
'''
image = np.array(image / 255, dtype=float)
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out * 255)
cv2.imshow("noise", out)
filename3=str(number)+'n10'+'.jpg'
cv2.imwrite(savedpath + filename3, out)
# 图像亮度调节
def img_brightness(image):
contrast = 1 # 对比度
brightness = 100 # 亮度
pic_turn = cv2.addWeighted(image, contrast, image, 0, brightness)
# cv2.addWeighted(对象,对比度,对象,对比度)
'''cv2.addWeighted()实现的是图像透明度的改变与图像的叠加'''
cv2.imshow('bright', pic_turn) # 显示图片
filename3=str(number)+'n11'+'.jpg'
cv2.imwrite(savedpath + filename3, pic_turn)
if __name__ == '__main__':
i = 0
path = '../Data/'
print(path)
savedpath = './result_new/'
filelist = os.listdir(path)
total_num = len(filelist)
for item in filelist:
number = i + 1
i = number
print("######")
print("打印到第",i,"张图片")
src = cv2.imread(path + item)
img_translation(src)
img_scale(src)
img_flip(src)
img_rotation(src)
img_noise(src)
img_brightness(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
代码较为繁琐,有空之后进行优化
输出结果
本文题目:python下载图片函数 python批量下载图片代码
本文来源:http://hbruida.cn/article/doihpid.html