2016-02-29 47 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))