【问题标题】:Problems with repeat use of crop method in Pillow (Python's PIL fork)Pillow 中重复使用crop 方法的问题(Python 的PIL fork)
【发布时间】: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


    【解决方案1】:

    问题出在这一行:

     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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 2015-01-03
      相关资源
      最近更新 更多