├── LICENSE ├── README.md ├── baiduImgSpider.py ├── example.py └── resources ├── downloadPic ├── 00000.jpg ├── 00001.jpg ├── 00002.jpg ├── 00003.jpg └── 00004.jpg └── example.csv /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Zhener 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BaiduPanoramaSpider 2 | 3 | ![GitHub](https://img.shields.io/badge/license-MIT-green) 4 | ![GitHub](https://img.shields.io/badge/python-3.6-blue) 5 | 6 | 百度全景/街景图爬虫 Baidu Panorama Spider / Baidu Street View spider / BSV 7 | 8 | **免责声明:本项目旨在学习python的一点实践,不可使用于商业和个人其他意图。若使用不当,均由个人承担。** 如果有帮助到你,点个星星呗嘻嘻。 9 | 10 | Disclaimer: This project is intended to learn Python and should not be used for commercial or personal purposes. Any improper use shall be borne by the individual. 11 | 12 | ## Usage 13 | 14 | ```python 15 | from baiduImgSpider import baiduImgDownloader 16 | 17 | baiduImgDownloader(pointsCsvPath, toFolderPath, baiduApiKey, zoom=3) 18 | ``` 19 | 20 | **pointsCsvPath**: WGS84 points csv file 21 | 22 | | X | Y | 23 | | -------- | -------- | 24 | | 121.4 | 31.2 | 25 | | 121.5 | 31.2 | 26 | | 121.6 | 31.2 | 27 | | ... | ... | 28 | 29 | **toFolderPath**: folder to store the pictures 30 | 31 | **baiduApiKey**: api key to project wgs84 to bd09mc 32 | 33 | **zoom**: 34 | 35 | |zoom| 1 | 2 | 3 | 4 | 36 | | - | ------- | -------- | --------- | --------- | 37 | |img | 512*256 | 1024*512 | 2048*1024 | 4096*2048 | 38 | 39 | 40 | 41 | ## Dependency 42 | 43 | ``` 44 | pandas 45 | pillow 46 | requests 47 | ``` 48 | 49 | 50 | 51 | ## Technical Route 52 | 53 | wgs84 points => bd09mc points => getImageID => getImage => mergeImage => saveImage 54 | 55 | ## Problems 56 | 57 | 网络上找到的方法还无法实现高精度的wgs84 => bd09mc,只有借助baiduApi,不是特别爽。 58 | 59 | 如果有知道解决方法的大佬请赐教! 60 | 61 | 62 | -------------------------------------------------------------------------------- /baiduImgSpider.py: -------------------------------------------------------------------------------- 1 | import requests,json 2 | from PIL import Image 3 | from io import BytesIO 4 | from math import ceil 5 | import pandas as pd 6 | import time 7 | import os 8 | 9 | 10 | def getImageID(x,y): 11 | # input: x, y (bd09mc) 12 | # output: sid (imgID) 13 | url = f"https://mapsv1.bdimg.com/?qt=qsdata&x={x}&y={y}" 14 | h = requests.get(url).text 15 | return json.loads(h) 16 | 17 | def getImageBytesList(sid,z=2): 18 | # input: sid (imgID) 19 | # output: List of imgBytes 20 | if z==2: 21 | xrange,yrange=1,2 22 | elif z==3: 23 | xrange,yrange=2,4 24 | elif z==1: 25 | xrange,yrange=1,1 26 | elif z==4: 27 | xrange,yrange=4,8 28 | imgBytes=[] 29 | for x in range(xrange): 30 | for y in range(yrange): 31 | url = f"https://mapsv1.bdimg.com/?qt=pdata&sid={sid}&pos={x}_{y}&z={z}&from=PC" 32 | b = requests.get(url).content 33 | imgBytes.append(b) 34 | return imgBytes 35 | 36 | def bytes2Img(imgByte): 37 | # input: imgByte 38 | # output: PIL.Image 39 | return Image.open(BytesIO(imgByte)) 40 | 41 | def bytesList2ImgList(x): 42 | # input: List of imgBytes 43 | # output: List of PIL.Image 44 | return [bytes2Img(_) for _ in x] 45 | 46 | def mergeImage(imgList,imgNumPerRow): 47 | # input: List of PIL.Image, imgNumPerRow 48 | # output: merged image 49 | assert isinstance(imgList[0],Image.Image) 50 | w,h = imgList[0].size 51 | rowNum = ceil(len(imgList)/imgNumPerRow) 52 | width = w * imgNumPerRow 53 | height = h * rowNum 54 | newImg = Image.new("RGB",(width,height)) 55 | for i,img in enumerate(imgList): 56 | x = i//imgNumPerRow 57 | y = i%imgNumPerRow 58 | newImg.paste(img,(y*h,x*w,)) 59 | return newImg 60 | 61 | def download(x,y,zoom,fp): 62 | # input: x, y (bd09mc) 63 | # output: None 64 | # saves the picture 65 | imgPerRow = {1:1,2:2,3:4,4:8} 66 | imgId = getImageID(x,y)["content"]['id'] 67 | imgBytes = getImageBytesList(imgId,z=zoom) 68 | imgList = bytesList2ImgList(imgBytes) 69 | img = mergeImage(imgList,imgPerRow[zoom]) 70 | img.save(fp) 71 | 72 | def inputPoints(fp): 73 | # input: WGS84 points csv file 74 | # output: list of str which consists 100 points 75 | points = pd.read_csv(fp,encoding="utf8") 76 | points = points.to_numpy().tolist() 77 | points100 =[] 78 | for i,(x,y) in enumerate(points): 79 | n = i//100 80 | if len(points100)==n: 81 | points100 += [f"{x},{y}"] 82 | else: 83 | points100[n] += f";{x},{y}" 84 | return points100 85 | 86 | def convertWGStoBD09MC(coords,ak): 87 | # input: list of str which consists 100 points, baiduapi ak 88 | # output: list of BD09MC points 89 | url = f"http://api.map.baidu.com/geoconv/v1/?coords={coords}&from=1&to=6&ak={ak}" 90 | h = requests.get(url) 91 | points = json.loads(h.text) 92 | points = [[x["x"],x["y"]] for x in points['result']] 93 | return points 94 | 95 | def baiduImgDownloader(pointsCsvPath,toFolderPath,ak,zoom=3): 96 | # input: WGS84 points csv file, toFolderPath, baiduapi ak, zoom 97 | # output: None 98 | points100 = inputPoints(pointsCsvPath) 99 | points = [] 100 | for p in points100: 101 | points += convertWGStoBD09MC(p,ak) 102 | for i,(x,y) in enumerate(points): 103 | fp = os.path.join(toFolderPath,f"{i:0>5d}.jpg") 104 | download(x,y,zoom,fp) 105 | time.sleep(3) 106 | print(fp) 107 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | from baiduImgSpider import baiduImgDownloader 2 | import json 3 | 4 | # f = open("./resources/ak.json","r") 5 | # YourAK = json.load(f) 6 | # f.close() 7 | 8 | YourAK = "****" 9 | 10 | baiduImgDownloader("resources/example.csv", # CRS: WGS84 11 | "resources/downloadPic", # folder 12 | ak=YourAK, zoom=3) -------------------------------------------------------------------------------- /resources/downloadPic/00000.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zhen3r/BaiduPanoramaSpider/095fe26a558439abfec5f83f8e9292798a368ccc/resources/downloadPic/00000.jpg -------------------------------------------------------------------------------- /resources/downloadPic/00001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zhen3r/BaiduPanoramaSpider/095fe26a558439abfec5f83f8e9292798a368ccc/resources/downloadPic/00001.jpg -------------------------------------------------------------------------------- /resources/downloadPic/00002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zhen3r/BaiduPanoramaSpider/095fe26a558439abfec5f83f8e9292798a368ccc/resources/downloadPic/00002.jpg -------------------------------------------------------------------------------- /resources/downloadPic/00003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zhen3r/BaiduPanoramaSpider/095fe26a558439abfec5f83f8e9292798a368ccc/resources/downloadPic/00003.jpg -------------------------------------------------------------------------------- /resources/downloadPic/00004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zhen3r/BaiduPanoramaSpider/095fe26a558439abfec5f83f8e9292798a368ccc/resources/downloadPic/00004.jpg -------------------------------------------------------------------------------- /resources/example.csv: -------------------------------------------------------------------------------- 1 | X,Y 2 | 121.4933418,31.28783638 3 | 121.4941693,31.28847133 4 | 121.4954526,31.28913514 5 | 121.4971075,31.28939489 6 | 121.4987286,31.28927944 7 | --------------------------------------------------------------------------------