用Python和PIL对图像进行切片
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Python和PIL对图像进行切片相关的知识,希望对你有一定的参考价值。
Create smaller tiles out of a larger bitmap image
def process_floor_image(infile, outfile, copy_pics): """ Copy the orignal image and make also 1024 x 1024 sliced tiles out of it. Because our green robot friend sucks. http://stackoverflow.com/questions/6632140/android-pixel-quality-reduction-in-images-loaded-in-webview @return Image size in [width, height] """ im = Image.open(infile) w = im.size[0] h = im.size[1] dimensions = [ w, h ] if copy_pics: # Copy master path = os.path.dirname(outfile) base = os.path.basename(outfile) try: os.makedirs(path) except OSError: pass print "Copying image:" + outfile shutil.copyfile(infile, outfile) # slice the image to 1000 x 1000 tiles slice_size = 1000 for y in range(0, h, slice_size): for x in range(0, w, slice_size): fname = os.path.join(path, "tile-%d-%d-%s" % (x, y, base)) print "Creating tile:" + fname mx = min(x+slice_size, w) my = min(y+slice_size, h) buffer = Image.new("RGB", [slice_size, slice_size], (255, 255, 255)) tile = im.crop((x, y, mx, my)) buffer.paste(tile, (0, 0)) buffer.save(fname, "PNG") return dimensions
以上是关于用Python和PIL对图像进行切片的主要内容,如果未能解决你的问题,请参考以下文章
图像处理基本工具——Python 环境下的 Pillow( PIL )
《计算机视觉和图像处理简介 - 中英双语版》:使用 PIL (Python Image Library)对图像进行几何变换及数学变换Geometric Operations
《计算机视觉和图像处理简介 - 中英双语版》:使用 PIL (Python Image Library)对图像进行滤波Spatial_Filtering