旋转的图片看起来像是缺少像素

时间:2014-02-07 21:05:34

标签: python image image-processing matrix python-imaging-library

我正在玩PIL和转换矩阵,以了解简单的2D图像处理背后的原因。

在我尝试将图像旋转为“低级别”时(即,不使用任何rotate(degrees)功能,但进行数学运算)我决定使用顺时针旋转矩阵旋转图像的每个像素:

enter image description here

旋转很顺利,但图像现在看起来像缺少一些像素。

原始图像,绘制在435x353黑色背景上:

enter image description here

顺时针旋转45°并向右移动300像素:

enter image description here

奇怪的是,将图片顺时针旋转90°(并向右移动400像素)时不会出现此问题:

enter image description here

可能导致这种情况的原因是什么?使用Image.Image.rotate工作得很好,所以我想问题就在于我的代码。值得一提的是原始图片具有透明背景,在上传时压缩丢失了。但是,我对jpeg(非透明)图像做了完全相同的操作,结果是一样的。

用于进行轮换的代码:

import Image, ImageDraw
from scipy import misc
import math

WHITE = (255,255,255)
BLACK = (0,0,0)
W, H = 435, 353
im = Image.new('RGBA', (W, H), BLACK)
draw = ImageDraw.Draw(im)
bitmap = misc.imread('Image.png')

def affine_t(x, y, a, b, c, d, e, f):
    """Returns ((a, b), (c, d))*((x), (y)) + ((e), (f)).""" 
    return a*x + b*y + e, c*x + d*y + f

def crotate(x, y, r):
    """Rotate (x, y) clockwise by r radians."""
    # And move 300 px to the right for this example
    return affine_t(
        x, y, math.cos(-r), math.sin(-r), -math.sin(-r), math.cos(-r), 300, 0
    )

x, y = 0, 0
angle = math.pi/4
for row in bitmap:
    for pt in row:
        draw.point([crotate(x, y, angle),],fill=tuple(pt))
        x+= 1
    x = 0
    y += 1

im.save('out.png')

1 个答案:

答案 0 :(得分:10)

对于每个目标像素,您需要计算源像素,但反之亦然。由于舍入,您将多个源像素映射到同一目标像素。这就是为什么没有插值就无法实现良好的45°旋转。我的建议实际上是最近邻插值。

相关问题