Ale*_*lex 7 python image image-processing matrix python-imaging-library
我正在玩PIL和转换矩阵,以了解简单的2D图像处理背后的原因.
在我尝试将图像旋转为尽可能"低级别"(即,不使用任何rotate(degrees)函数,但进行数学运算)时,我决定使用顺时针旋转矩阵旋转图像的每个像素:

旋转很顺利,但图像现在看起来像缺少一些像素.
原始图像,绘制在435x353黑色背景上:

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

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

可能是什么导致了这个?使用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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1107 次 |
| 最近记录: |