Python 为什么在尝试将图像分割为两半时出现无法扩展外部图像错误
我的程序应该拍摄一幅图像并将其垂直分割为n个部分,然后将这些部分保存为单独的png文件。在两个部分中应该是这样的 我现在遇到了问题,我得到的是正确保存图像的前半部分,然后当它尝试裁剪后半部分时,我得到以下错误:Python 为什么在尝试将图像分割为两半时出现无法扩展外部图像错误,python,image,python-imaging-library,crop,Python,Image,Python Imaging Library,Crop,我的程序应该拍摄一幅图像并将其垂直分割为n个部分,然后将这些部分保存为单独的png文件。在两个部分中应该是这样的 我现在遇到了问题,我得到的是正确保存图像的前半部分,然后当它尝试裁剪后半部分时,我得到以下错误: SystemError:磁贴无法扩展外部图像 我正在处理的图像 宽度:1180px 身高:842px 它计算要裁剪的矩形为: (0.0,0590.0842)-这可以正常工作 (590.0,0590.0842)-这会使程序崩溃 我的问题是:为什么这个子矩形超出了边界,我如何修复它
SystemError:磁贴无法扩展外部图像
我正在处理的图像
- 宽度:1180px
- 身高:842px
-这可以正常工作(0.0,0590.0842)
-这会使程序崩溃(590.0,0590.0842)
from PIL import Image, ImageFilter
im = Image.open("image.png")
width, height = im.size
numberOfSplits = 2
splitDist = width / numberOfSplits #how many pixels each crop should be in width
print(width, height) #prints 1180, 842
for i in range(0, numberOfSplits):
x = splitDist * i
y = 0
w = splitDist
h = height
print(x, y, w, h)
#first run through prints 0.0, 0, 590.0, 842
#second run through prints 590.0, 0, 590.0, 842 then crashes
croppedImg = im.crop((x,y,w,h)) #crop the rectangle into my x,y,w,h
croppedImg.save("images\\new-img" + str(i) + ".png") #save to file
框(x,y,w,h)的所有坐标都是从图像的左上角开始测量的 所以盒子的坐标应该是(x,y,w+x,h+y)。对代码进行以下更改
for i in range(0, numberOfSplits):
x = splitDist * i
y = 0
w = splitDist+x
h = height+y
好的,它是用坐标来工作的,而不是用x,y,w,h来工作的矩形。谢谢