Skip to content

Instantly share code, notes, and snippets.

@cnDelbert
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnDelbert/7fcb33b4dd9a38fa8bcc to your computer and use it in GitHub Desktop.
Save cnDelbert/7fcb33b4dd9a38fa8bcc to your computer and use it in GitHub Desktop.
Modify the path to save and the url which obtained, the script can download the pictures and paste them into one. The two files fit different URL formats.
# -*- coding:utf-8 -*-
__author__ = 'Delbert'
# for Python 3.4 and Python 2.7
# Pillow and request module are needed
import requests
import os
import re
from PIL import Image
url = "https://lh3.googleusercontent.com/-tzg5ggkNsKQ/U9n27EXRdlI/AAAAAAAARgo/JvvUpv350xo/x2-y1-z3/roma%2B04.jpg"
def down_img(folder):
"""
Download images to the folder.
:param folder: path to download.
:return: NULL
"""
print("%s, %s, %s, %s" % (x_begin, x_end, y_begin, y_end))
for x in range(x_begin, x_end + 1):
for y in range(y_begin, y_end + 1):
url_req = url % (x, y)
img_file = requests.get(url_req).content
img = open("./%s/z%02s_x%02d_y%02d.jpg" % (folder, zoom, x, y), "wb")
img.write(img_file)
img.close()
print("z%02s_x%02d_y%02d.jpg" % (zoom, x, y))
def paste_img(folder):
print("Joining images...")
width = (x_end + 1) * 512
height = (dy_end - dy_begin + 1) * 512
target = Image.new("RGBA", (width, height))
for x in range(0, x_end + 1):
for y in range(dy_begin, dy_end + 1):
# print("./%s/z%02s_x%02d_y%02d.jpg" %(folder, z, x, y))
img = Image.open("./%s/z%02s_x%02d_y%02d.jpg" % (folder, zoom, x, y))
target.paste(img, (512 * x, 512 * (y - dy_begin), 512 * (x + 1), 512 * (y - dy_begin + 1)))
if folder.endswith('.jpg'):
target.save("./%s/%s" % (folder, folder))
else:
target.save("./%s/%s.jpg" % (folder, folder))
print("Done.")
def check_param(url_address):
x, y, x_max, y_max = 0, 0, 0, 0
print("Checking parameter x ...")
while True:
url_req = url_address % (x, y)
# print(url_req)
code = requests.get(url_req).headers
# print(code["content-type"])
if code["content-type"] == "image/jpeg":
x += 1
else:
x_max = x - 1
break
print("X-max is %s." % x_max)
x, y = 0, 0
print("Checking parameter y ...")
while True:
url_req = url_address % (x, y)
code = requests.get(url_req).headers
if code["content-type"] == "image/jpeg":
y += 1
else:
y_max = y - 1
break
print("Y-max is %s." % y_max)
print("Thresholds for x and y are %s and %s." % (x_max, y_max))
return x_max, y_max
# text/html; charset=UTF-8
# image/jpeg; charset=UTF-8
def check_img(folder, y_max):
"""
:param folder: folder to store images
:param y_max: thresthold for y-value
:return:space interval of 512*512 size images.
"""
x = 0
li = []
print("Checking image size...")
for y in range(0, y_max + 1):
img = Image.open('./%s/z%02s_x%02d_y%02d.jpg' % (folder, zoom, x, y))
print(img.getbbox())
if img.getbbox()[2] == 512:
li.append(y)
lg_y_begin = li[0]
print("lg_y_begin is " + str(lg_y_begin))
lg_y_end = li[-1]
print("lg_y_end is " + str(lg_y_end))
return lg_y_begin, lg_y_end
def check_url(url_address):
"""
Parse the URL input.
:param url_address: str
:return: url, path, x.
"""
print("Checking URL ...")
url_to_be_checked = url_address
url_to_be_checked = url_to_be_checked.replace(r'%2B', '+')
url_to_be_checked = url_to_be_checked.replace(r'%20', ' ')
url_to_be_checked = url_to_be_checked.replace(r'%2F', '/')
url_to_be_checked = url_to_be_checked.replace(r'%3F', '?')
new_url = re.sub(r'x\d+-y\d+-z', 'x%s-y%s-z', url_to_be_checked)
if url_to_be_checked.split("/")[-1] == '':
path = url_to_be_checked.split('/')[-2]
else:
path = url_to_be_checked.split('/')[-1]
z = re.search(re.compile(r'x\d+-y\d+-z\d'), url_to_be_checked).group()[-1]
print('URL with params is {0}'.format(new_url))
print('All files will be download in {save_folder}'.format(save_folder=path))
print("zoom is %s." % z)
return new_url, path, z
if __name__ == '__main__':
x_begin = 0
x_end = 0
y_begin = 0
y_end = 0
zoom = 0
dy_begin, dy_end = 0, 0
save_path = ""
url, save_path, zoom = check_url(url)
if not os.path.exists('./%s/' % save_path):
os.mkdir('./%s/' % save_path)
(x_end, y_end) = check_param(url)
# print(x_end, y_end)
down_img(save_path)
dy_begin, dy_end = check_img(save_path, y_end)
paste_img(save_path)
# -*- coding:utf-8 -*-
__author__ = 'Delbert'
# for Python 3.4 and Python 2.7
# Pillow and request module are needed
import requests
import os
from PIL import Image
save_path = "notre-dame-cathed1"
url = "https://geo1.ggpht.com/cbk?cb_client=maps_photos.ugc&panoid=mJBuWGKD2zCCoIbBh-R83w&output=tile&x=2&y=1&zoom=5&nbt&fover=2"
def down_img(folder):
print("%s, %s, %s, %s" % (x_begin, x_end, y_begin, y_end))
for x in range(x_begin, x_end + 1):
for y in range(y_begin, y_end + 1):
url_req = url % (x, y)
img_file = requests.get(url_req).content
img = open("./%s/z%02d_x%02d_y%02d.jpg" % (folder, zoom, x, y), "wb")
img.write(img_file)
img.close()
print("z%02d_x%02d_y%02d.jpg" % (zoom, x, y))
def paste_img(folder):
print("Joining images...")
width = (x_end + 1)*512
height = (dy_end - dy_begin + 1)*512
target = Image.new("RGBA", (width, height))
for x in range(0, x_end + 1):
for y in range(dy_begin, dy_end + 1):
#print("./%s/z%02d_x%02d_y%02d.jpg" %(folder, z, x, y))
img = Image.open("./%s/z%02d_x%02d_y%02d.jpg" % (folder, zoom, x, y))
target.paste(img, (512*x, 512*(y-dy_begin), 512*(x+1), 512*(y-dy_begin+1)))
target.save("./%s/z%02d_all.jpg" % (folder, zoom))
print("Done.")
def check_param(url_address):
x, y, x_max, y_max = 0, 0, 0, 0
print("Checking parameter x ...")
while True:
url_req = url_address % (x, y)
#print(url_req)
code = requests.get(url_req).headers
#print(code["content-type"])
if code["content-type"] == "image/jpeg; charset=UTF-8":
x += 1
else:
x_max = x-1
break
x, y = 0, 0
print("Checking parameter y ...")
while True:
url_req = url_address % (x, y)
code = requests.get(url_req).headers
if code["content-type"] == "image/jpeg; charset=UTF-8":
y += 1
else:
y_max = y-1
break
print("Thresholds for x and y are %s and %s." % (x_end, y_end))
return x_max, y_max
# text/html; charset=UTF-8
# image/jpeg; charset=UTF-8
def check_img(folder, y_max):
"""
:param folder: folder to store images
:param y_max: thresthold for y-value
:return:space interval of 512*512 size images.
"""
x = 0
li = []
print("Checking image size...")
for y in range(0, y_max + 1):
img = Image.open('./%s/z%02d_x%02d_y%02d.jpg' % (folder, zoom, x, y))
if img.getbbox()[3] == 512:
li.append(y)
lg_y_begin = li[0]
lg_y_end = li[-1]
return lg_y_begin, lg_y_end
def check_url(url_address):
"""
:param url_address: url to be checked
:return: url after check and zoom in int
"""
print("Checking URL ...")
url_to_be_checked = url_address
url_sp = url_to_be_checked.split("&")
new_url = ""
z = 0
for url_item in url_sp:
if url_item.startswith("x="):
url_item = "x=%s"
if url_item.startswith("y="):
url_item = "y=%s"
if url_item.startswith("zoom="):
z = int(url_item[-1])
if new_url == "":
new_url = url_item
else:
new_url = '{0}&{1}'.format(new_url, url_item)
print('URL with params is {0}'.format(new_url))
print("zoom is %d." % z)
return new_url, z
if __name__ == '__main__':
x_begin = 0
x_end = 0
y_begin = 0
y_end = 0
zoom = 0
dy_begin, dy_end = 0, 0
if not os.path.exists('./%s/' % save_path):
os.mkdir('./%s/' % save_path)
url, zoom = check_url(url)
(x_end, y_end) = check_param(url)
#print(x_end, y_end)
down_img(save_path)
dy_begin, dy_end = check_img(save_path, y_end)
paste_img(save_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment