{"id":605,"date":"2024-02-26T14:03:31","date_gmt":"2024-02-26T06:03:31","guid":{"rendered":"http:\/\/139.224.70.39\/?p=605"},"modified":"2024-02-26T14:03:31","modified_gmt":"2024-02-26T06:03:31","slug":"python%e5%88%87%e5%9b%be","status":"publish","type":"post","link":"http:\/\/139.224.70.39\/?p=605","title":{"rendered":"python\u5207\u56fe"},"content":{"rendered":"<pre class=\"prettyprint lang-py linenums:1\"># importing functions\r\nimport sys\r\nimport os\r\nimport math\r\nfrom PIL import Image\r\nfrom tqdm import tqdm\r\n# source path\r\nimg_path = r\"C:\\Users\\Shen\\PycharmProjects\\pythonProject\\0-TileGroup\u6293\u56fe\u6cd5\\\u9b41\u661f\u56fe.jpg\"\r\n# output tile directory\r\ntile_path = 'tiles'\r\n# tile size\r\ntile_size = 256\r\nImage.MAX_IMAGE_PIXELS = None  # \u7981\u7528\u89e3\u538b\u7f29\u70b8\u5f39\u9650\u5236\r\n\r\n\r\ndef adjustImage(src_img):  # ajust image size to tile size\r\n\t# get image size\r\n\timg_w, img_h = src_img.size\r\n\t# calculate tilable img size\r\n\tout_w = img_w + (tile_size - img_w % tile_size)\r\n\tout_h = img_h + (tile_size - img_h % tile_size)\r\n\t# create transparent  extra background\r\n\tout_img = Image.new('RGBA', (out_w, out_h))\r\n\t# combine src image and background\r\n\tout_img.paste(src_img, (0,0))\r\n\treturn out_img\r\n\r\n\r\nimg = Image.open(img_path)\r\nw, h = img.size[0], img.size[1]\t\r\n\r\n# calculate max zoom level\r\nmax_zoom = int(math.ceil(math.log((max(w, h) \/ tile_size), 2)))\r\n# main loop for all zoom levels\r\nfor z in tqdm(range(max_zoom, -1, -1)):\r\n\t# adjusting image\r\n\tadj_img = adjustImage(img)\r\n\t# calculating number of rows and columns of tiles\r\n\tn_cols = int(adj_img.size[0] \/ tile_size)\r\n\tn_rows = int(adj_img.size[1] \/ tile_size)\r\n\t# print(n_cols,n_rows)\r\n\t# loop for creating directories and tiles\r\n\tfor x in range(n_cols):\r\n\t\t# create z\/x directory\r\n\t\tpath = os.path.join(tile_path, str(z), str(x))\r\n\t\tif not os.path.isdir(path):\r\n\t\t\tos.makedirs(path)\r\n\t\t# cut tiles\r\n\t\tfor y in range(n_rows):\r\n\t\t\tbounds = (x * tile_size, y * tile_size, (x + 1) * tile_size, (y + 1) * tile_size)\r\n\t\t\ttile = adj_img.crop(bounds)\r\n\t\t\tprint(x,y,z)\r\n\t\t\ttile.save('%s\/%s.png' % (path, y))\r\n\t# resize image - go to next zoom level\r\n\tw, h = img.size[0], img.size[1]\r\n\timg = img.resize((int(w\/2), int(h\/2)), Image.ANTIALIAS)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p># importing functions import sys import os import math &#8230;<\/p>\n<div class=\"read-more-link\"><a href=\"http:\/\/139.224.70.39\/?p=605\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13,6],"tags":[],"class_list":["post-605","post","type-post","status-publish","format-standard","hentry","category-python","category-6"],"acf":[],"_links":{"self":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts\/605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=605"}],"version-history":[{"count":1,"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":606,"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions\/606"}],"wp:attachment":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}