工具:PyCharm
今天我们从百度图片上下载一个图片放入文件中,然后将这个用代码文件复制一份,首先建一个picture.py的python file,然后写入一下代码:
#下载图片
import requests
download ='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535716637&di=202bbd1c0f714a5bb43aab089429ab19&imgtype=jpg&er=1&src=http%3A%2F%2Fa3.topitme.com%2F1%2Fa7%2F7c%2F11239382057cc7ca71o.jpg'
pic = requests.get(download)
with open("picture.jpg",'wb') as picture: #要读取二进制文件,如图片、视频等,要用'wb'模式写入文件
picture.write(pic.content)
#图片复制
with open('picture.jpg','rb') as pic:
with open('picture_copy.jpg','wb') as pic_copy:
pic_size = pic.read()
while len(pic_size)>0: #循环写入
pic_copy.write(pic_size)
pic_size = pic.read()
可以在左侧栏中看到自动生成了picture.jpg 和picture_copy.jpg 两个文件,点开即可看到图片。