使用python转换和裁剪平铺中的图像

使用python转换和裁剪平铺中的图像,python,imagemagick,crop,imagemagick-convert,wand,Python,Imagemagick,Crop,Imagemagick Convert,Wand,我尝试用python平铺JPG图像。我通常使用imageMagick。。所以我看到比似乎做这项工作 但我不会翻译 convert -crop 256x256 +repage big_image.jpg tiles_%d.jpg 有人能帮我吗?Python的魔杖库提供了一个独特的使用wand.image.image[左:右,上:下]可以对新的虚拟像素图像进行子切片 从wand.image导入图像 使用图像(filename=“big_Image.jpg”)作为img: i=0 对于范围内的h(

我尝试用python平铺JPG图像。我通常使用imageMagick。。所以我看到比似乎做这项工作

但我不会翻译

 convert -crop 256x256 +repage big_image.jpg tiles_%d.jpg

有人能帮我吗?

Python的魔杖库提供了一个独特的使用
wand.image.image[左:右,上:下]
可以对新的虚拟像素图像进行子切片

从wand.image导入图像
使用图像(filename=“big_Image.jpg”)作为img:
i=0
对于范围内的h(0,img.height,256):
对于范围内的w(0,img.width,256):
w_end=w+256
h_端=h+256
将img[w:w_end,h:h_end]作为块:
chunk.save(filename='tiles_{0}.jpg'.格式(i))
i+=1
上面将从以下位置生成许多与
+repage
选项匹配的平铺图像:

convert -crop 256x256 +repage big_image.jpg tiles_%d.jpg

Python的魔杖库提供了一个独特的使用
wand.image.image[左:右,上:下]
可以对新的虚拟像素图像进行子切片

从wand.image导入图像
使用图像(filename=“big_Image.jpg”)作为img:
i=0
对于范围内的h(0,img.height,256):
对于范围内的w(0,img.width,256):
w_end=w+256
h_端=h+256
将img[w:w_end,h:h_end]作为块:
chunk.save(filename='tiles_{0}.jpg'.格式(i))
i+=1
上面将从以下位置生成许多与
+repage
选项匹配的平铺图像:

convert -crop 256x256 +repage big_image.jpg tiles_%d.jpg

用于构建要素金字塔样式的网络

def image_to_square_tiles(img, SQUARE_SIZE = 256, plot=False, save=False):
"""
Function that splits multi channel channel images into overlapping square tiles (divisible by 128)
:param img: image: multi channel image (NxMxC matrix)
:param number_of_tiles: squared number
:param plot: whether to plot an aimage
:return tiles: named tuple of tiled images (.img) and coordinates (.coords)

------------------------
Examples usage:
_ = image_to_square_tiles(img, SQUARE_SIZE = 512, plot=True)
--------------------------
"""
def get_overlap(l, SQUARE_SIZE):
    N_squares =  np.ceil(l/SQUARE_SIZE)
    pixel_padding = np.remainder(l,SQUARE_SIZE)
    if pixel_padding!=0:
        overlap = int((SQUARE_SIZE-pixel_padding)//(N_squares-1))
    else:
        overlap = 0
    return overlap
def get_tuples(l, overlap, SQUARE_SIZE):
    r = np.arange(0, l-overlap, (SQUARE_SIZE-overlap))
    tuples = [(i, i+SQUARE_SIZE) for i in r]
    return tuples

[w, h] = img.shape[:2]
assert SQUARE_SIZE%128==0, "has to be a multiple of 128 . i.e. [128,256,384,512,640,768,896,1024]"

w_overlap = get_overlap(w, SQUARE_SIZE)
w_tuples = get_tuples(w, w_overlap, SQUARE_SIZE)
h_overlap = get_overlap(h, SQUARE_SIZE)
h_tuples = get_tuples(h, h_overlap, SQUARE_SIZE)

tile_record = namedtuple("info", "img coords")
tiles = []
for row in range(len(w_tuples)):
    for column in range(len(h_tuples)):
        #print(row,column)
        x1, x2, y1, y2 = *w_tuples[row], *h_tuples[column] 
        record = tile_record(img[x1:x2, y1:y2], (x1, y1, x2, y2))
        tiles.append(record)

if plot:
    c = 0
    fig, axes = plt.subplots(len(w_tuples), len(h_tuples), figsize=(15,8))
    for row in range(len(w_tuples)):
        for column in range(len(h_tuples)):
            axes[row, column].imshow(tiles[c].img)
            #axes[row,column].set_title("ave: {:.3f}".format(np.average(tiles[c].img)))
            axes[row,column].axis('off')
            c+=1
    if save:
        plt.savefig("{}.png".format(SQUARE_SIZE), bbox_inches = "tight")
print("h overlap: {}\t w overlap: {}".format(w_overlap, h_overlap))
return tiles

用于构建要素金字塔样式的网络

def image_to_square_tiles(img, SQUARE_SIZE = 256, plot=False, save=False):
"""
Function that splits multi channel channel images into overlapping square tiles (divisible by 128)
:param img: image: multi channel image (NxMxC matrix)
:param number_of_tiles: squared number
:param plot: whether to plot an aimage
:return tiles: named tuple of tiled images (.img) and coordinates (.coords)

------------------------
Examples usage:
_ = image_to_square_tiles(img, SQUARE_SIZE = 512, plot=True)
--------------------------
"""
def get_overlap(l, SQUARE_SIZE):
    N_squares =  np.ceil(l/SQUARE_SIZE)
    pixel_padding = np.remainder(l,SQUARE_SIZE)
    if pixel_padding!=0:
        overlap = int((SQUARE_SIZE-pixel_padding)//(N_squares-1))
    else:
        overlap = 0
    return overlap
def get_tuples(l, overlap, SQUARE_SIZE):
    r = np.arange(0, l-overlap, (SQUARE_SIZE-overlap))
    tuples = [(i, i+SQUARE_SIZE) for i in r]
    return tuples

[w, h] = img.shape[:2]
assert SQUARE_SIZE%128==0, "has to be a multiple of 128 . i.e. [128,256,384,512,640,768,896,1024]"

w_overlap = get_overlap(w, SQUARE_SIZE)
w_tuples = get_tuples(w, w_overlap, SQUARE_SIZE)
h_overlap = get_overlap(h, SQUARE_SIZE)
h_tuples = get_tuples(h, h_overlap, SQUARE_SIZE)

tile_record = namedtuple("info", "img coords")
tiles = []
for row in range(len(w_tuples)):
    for column in range(len(h_tuples)):
        #print(row,column)
        x1, x2, y1, y2 = *w_tuples[row], *h_tuples[column] 
        record = tile_record(img[x1:x2, y1:y2], (x1, y1, x2, y2))
        tiles.append(record)

if plot:
    c = 0
    fig, axes = plt.subplots(len(w_tuples), len(h_tuples), figsize=(15,8))
    for row in range(len(w_tuples)):
        for column in range(len(h_tuples)):
            axes[row, column].imshow(tiles[c].img)
            #axes[row,column].set_title("ave: {:.3f}".format(np.average(tiles[c].img)))
            axes[row,column].axis('off')
            c+=1
    if save:
        plt.savefig("{}.png".format(SQUARE_SIZE), bbox_inches = "tight")
print("h overlap: {}\t w overlap: {}".format(w_overlap, h_overlap))
return tiles

如果要基于rowsXcols平铺图像:可以使用以下方法:

def TileImage(image,rows,cols):
imagename = image
im = Image.open(imagename) 
width, height = im.size
indexrow = 0
indexcolum = 0
left = 0
top = 0
right = width/col
buttom = 0
while(right<=width):    

    buttom = height/rows
    top = 0
    indexrow=0  

    while(top<height):
        print(f"h : {height}, w : {width}, left : {left},top : {top},right : {right}, buttom   :  {buttom}")
        cropimg= im.crop((left, top, right, buttom)) 
        cropimg.save(imagename + str(indexrow) + str(indexcolum) +".jpg")
        top = buttom
        indexrow += 1
        buttom += height/rows   

    indexcolum+=1
    left = right
    right += width/col  

如果要基于rowsXcols平铺图像:可以使用以下方法:

def TileImage(image,rows,cols):
imagename = image
im = Image.open(imagename) 
width, height = im.size
indexrow = 0
indexcolum = 0
left = 0
top = 0
right = width/col
buttom = 0
while(right<=width):    

    buttom = height/rows
    top = 0
    indexrow=0  

    while(top<height):
        print(f"h : {height}, w : {width}, left : {left},top : {top},right : {right}, buttom   :  {buttom}")
        cropimg= im.crop((left, top, right, buttom)) 
        cropimg.save(imagename + str(indexrow) + str(indexcolum) +".jpg")
        top = buttom
        indexrow += 1
        buttom += height/rows   

    indexcolum+=1
    left = right
    right += width/col  

谢谢@emcconville。。。如果我想在磁片名称中添加line和clone位置?类似于
chunk.save(filename=myfilename+'tiles.+chr(h)+'.+chr(w)+'.jpg.format(i))
我想是吧?但不像,我有个错误…是的。只需要重新查看一些字符串格式。比如:
“{}tiles{}{{}.jpg.”格式(myfilename,h,w)
,但这取决于您是否感谢@emcconville。。。如果我想在磁片名称中添加line和clone位置?类似于
chunk.save(filename=myfilename+'tiles.+chr(h)+'.+chr(w)+'.jpg.format(i))
我想是吧?但不像,我有个错误…是的。只需要重新查看一些字符串格式。比如:
“{}{}{}{}.jpg.format(myfilename,h,w)
,但这取决于您