Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这个函数会为我的所有图像添加边框?_Python_Image_Python Imaging Library - Fatal编程技术网

Python 为什么这个函数会为我的所有图像添加边框?

Python 为什么这个函数会为我的所有图像添加边框?,python,image,python-imaging-library,Python,Image,Python Imaging Library,我在用PIL im = im.rotate(angle=-90, expand = True) 当我这样做时,它会给我的图像添加一个灰色边框 为什么? 这是完整的代码。请注意,如果不旋转,则不会添加边框 def fixRotation(f, quality=96, image_type="JPEG"): #http://sylvana.net/jpegcrop/exif_orientation.html d =getEXIF(f) if d: orien

我在用PIL

im = im.rotate(angle=-90, expand = True)
当我这样做时,它会给我的图像添加一个灰色边框

为什么?

这是完整的代码。请注意,如果不旋转,则不会添加边框

def fixRotation(f, quality=96, image_type="JPEG"):
    #http://sylvana.net/jpegcrop/exif_orientation.html
    d =getEXIF(f)
    if d:
        orientation = int(d['Orientation'])
        im = Image.open(StringIO(f))
        if orientation == 6:
            im = im.rotate(angle=-90, expand = True)
        elif orientation == 3:
            im = im.rotate(angle=-180, expand=True)
        elif orientation == 8:
            im = im.rotate(angle=-270, expand=True)
        else:
            #It doesn't add a border here.
            im = im.rotate(0, expand=True)
        res = StringIO()
        im.save(res, image_type, quality=quality)
        res.seek(0)
        return res
    else:
        return StringIO(f)

我做了一些实验,确实改变了图像的大小,但我不了解确切的行为。在我看来,这就像是一只虫子。。。你应该报告

如果你只需要k*90度,那么你也可以使用numpy进行旋转

img = Image.fromarray(numpy.rot90(numpy.array(img), n))
n
是旋转90度的次数。

注意以下事项(CPython 2.6,PIL 1.1.7):

  • 角度
    是90的倍数时,不需要
    展开
  • 展开
    在直角倍数的情况下似乎激活了一个bug

  • 因此,如果您只关心90°-180°-270°旋转,只需删除
    expand=1
    参数;更好的方法是使用
    转置
    方法(参见中的几何变换)

    旋转时,算法(基本上)用“下一个”像素值平均每个像素。对于图像“内部”的所有像素,定义下一个像素。对于图像边缘的所有像素,该像素未定义

    因此,灰色是已知周长像素和未定义外部像素之间的平均值

    import Image
    i= Image.open("someimage.jpg")
    ir0= i.rotate(-90)
    ir1= i.rotate(-90, expand=1)
    
    for img in i, ir0, ir1:
        print(img.size)
    
    # output follows
    (720, 400)
    (400, 720)
    (401, 721)