2016-02-29 64 views
0

我有一些代码使用Pillow的作物方法将单个图像分割成多个子图像。我的代码是类似以下内容:在枕头中重复使用裁剪方法的问题(Python的PIL叉)

from PIL import Image 
from PIL import ImageTk 
import Tkinter  


# Open image from file path 
baseimg = Image.open("PathToLargeImage.tif") 

# Get image attributes 
height = baseimg.height 
width = baseimg.width 

# Create a list of sub-images 
subimages = [] 
for y in range(0, height, 50): 
    subimage = baseimg.crop((0, y, width, 10)) 
    subimage.load() # Call load on sub-image to detach it from baseimg 
    subimages.append(subimage) 
    showimage(subimage) 

当我拨打电话,显示子图像第一子图像将正常显示,那么所有下面的子图像将具有零高度(从调试发现与PyCharm)和显示不正确。

showimage功能使用Tkinter的是如下:

def showimage(img): 
    # Build main window 
    root = Tkinter.Tk() 
    # Convert image 
    tkimage = ImageTk.PhotoImage(img) 
    # Add image on window 
    Tkinter.Label(root, image=tkimage).pack() 
    # Start gui loop 
    root.mainloop() 

回答

0

的问题是在这条线:

subimage = baseimg.crop((0, y, width, 10)) 

如果您在http://effbot.org/imagingbook/image.htm#tag-Image.Image.crop 检查作物的文档,你会看到它不会将框的宽度和高度进行裁剪,而是将其绝对坐标值作为 。

所以,你只应该有相应地更改您的来电:

subimage = baseimg.crop((0, y, width, y + 10))