gpt4 book ai didi

python - 使用python向图像添加黑框

转载 作者:行者123 更新时间:2023-12-03 08:19:32 26 4
gpt4 key购买 nike

我有一个包含几个不同尺寸图像的数据集。为了训练我的神经网络,我想通过在所有图像周围添加黑框来将这些图像的大小更改为一个普通的大小。我使用以下代码:

from PIL import Image 
import numpy as np


old_im = Image.open('C:/Users/tobis/OneDrive/Desktop/Masterarbeit/data/2017-IWT4S-HDR_LP-dataset/crop_h1/I00002.png')
old_size = old_im.size
print(old_size)

new_size = (800, 800)
print(new_size)
new_im = Image.new("RGB", new_size) ## luckily, this is already black!
new_im.paste(old_im, ((new_size[0]-old_size[0])/2,
(new_size[1]-old_size[1])/2))

并得到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-3363f60dba3b> in <module>
11 new_im = Image.new("RGB", new_size) ## luckily, this is already black!
12 new_im.paste(old_im, ((new_size[0]-old_size[0])/2,
---> 13 (new_size[1]-old_size[1])/2))

~\.conda\envs\python36\lib\site-packages\PIL\Image.py in paste(self, im, box, mask)
1466 self.im.paste(im, box, mask.im)
1467 else:
-> 1468 self.im.paste(im, box)
1469
1470 def alpha_composite(self, im, dest=(0, 0), source=(0, 0)):

TypeError: integer argument expected, got float

我不知道为什么会收到此错误。我检查了变量条目的类型,它们都是整数,没有浮点数...我也很惊讶,第一个减法似乎还可以,第二个减法引起了问题...

谁能帮我吗?

最佳答案

该除法产生一个浮点数,但该方法需要整数。

new_size = (800, 800)
print(new_size)
new_im = Image.new("RGB", new_size) ## luckily, this is already black!
x = int((new_size[0]-old_size[0])/2)
y = int((new_size[1]-old_size[1])/2))
new_im.paste(old_im, (x,y))

关于python - 使用python向图像添加黑框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60973285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com