【发布时间】:2018-01-02 04:30:40
【问题描述】:
我在将图像转换为极坐标时遇到问题。在 Photoshop 中这很简单 :) 所以这对我来说是一个新领域。
我有以下图片:
它最终应该是这样的:
我已经看过here 并且我已经掌握了基础知识,但仍然对方形圆圈有点困惑:
import math
from PIL import Image, ImageDraw
# image size
imgX = 200
imgY = 200
image = Image.new("RGB", (imgX, imgY))
draw = ImageDraw.Draw(image)
#fill with white first
colour = "#ffffff"
box = [0,0, imgX, imgY]
image.paste(colour, box)
# draw line near base
draw.line((0,180, 200, 180), fill="#FF0000", width=2)
print "Line done!"
image.save("line.png", "PNG")
# there's got to be a way to get the current image
# without having to open it up again
im = Image.open("line.png")
rgb_im = im.convert("RGB")
# rectangle to polar coordinates
maxradius = math.sqrt(imgX**2 + imgY**2)/2
rscale = imgX / maxradius
tscale = imgY / (2*math.pi)
for y in range(0, imgY):
dy = y - imgY/2
for x in range(0, imgX):
dx = x - imgX/2
t = math.atan2(dy,dx)%(2*math.pi)
r = math.sqrt(dx**2+dy**2)
r, g, b = rgb_im.getpixel((x, y))
# this is where it goes wrong
col = b * 65536 + g * 256 + r
image.putpixel((x, y), b * 65536 + g * 256 + r
image.save("polar.png", "PNG")
我几乎对如何重绘图像有点困惑。还有一个警告:由于管理限制,我想避免使用像 Numpy 这样的外部库。
【问题讨论】:
标签: image python-2.7 python-imaging-library polar-coordinates