【发布时间】:2016-02-29 22:49:52
【问题描述】:
我有一些代码使用 Pillow 的 crop 方法将单个图像拆分为多个子图像。我的代码类似于以下:
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)
当我调用 display 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()
【问题讨论】:
标签: python python-imaging-library crop pillow imaging