python - 尝试从图像中移动像素

标签 python scipy python-imaging-library

我正在尝试拍摄硬币的灰度图像并移动像素以获得新图像。看起来我的代码正在将像素重新分配到新图像中,但最后我看不到任何内容。这是我第一次使用 PIL 和堆栈溢出!

from PIL import Image as image
import scipy.misc

im = image.open('dilation.jpg')
imageW = im.size[0]
imageH = im.size[1]
new = image.new("1",(imageW, imageH))
pixels = new.load()
b = round(imageW / 2)
n = b + 1

for x in range(imageW):
    for y in range(imageH):
        xy = (x,y)
        rgb = im.getpixel(xy)
        for i in range(3, -3,-1):          
        #Set new xy coordinates
            new_x = round(n * math.sin(i) + (width / 2))
            new_y = round(n * math.cos(i) + (height / 2))
            if new_x <= imageW and new_y <= imageH:
                pixels[new_x, new_y] = rgb

new

我希望得到某种灰度硬币的修改版本,但由于某种原因,新图像中的所有内容仍然是黑色的。我不知道如何具体上传我的硬币的图像,但它是通过在我的原始图像上运行 Canny 边缘检测生成的,如下所示:https://qph.fs.quoracdn.net/main-qimg-7eee051142768d5ed6cadb8fa44ae435.webp

最佳答案

请确保您的代码正在运行。

这是一个可以帮助您的代码片段

import math
import numpy as np

from PIL import Image


im = Image.open('bee_hive.jpeg')
im = np.array(im)

imageW = im.shape[0]
imageH = im.shape[1]
imageC = im.shape[2]

new = np.zeros(im.shape)

for x in range(imageW):
    for y in range(imageH):
        rgb = im[x,y,:]
        for i in range(3, -3,-1):
        #Set new xy coordinates
            new_x = round(n * math.sin(i) + (imageW / 2))
            new_y = round(n * math.cos(i) + (imageW / 2))
            if new_x <= imageW and new_y <= imageH:
                new[new_x, new_y, :] = rgb


new = Image.fromarray(new.astype('uint8'), 'RGB')
new.show()

基本思想是使用 numpy 生成图像的张量(3D 矩阵)。那么修改像素只是张量元素的重新排列。

关于python - 尝试从图像中移动像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295640/

相关文章:

python - 将curl翻译成python

python - Python中的逐步回归

python - 为阅读文档扩展 scipy.stats.rv_continuous 时出现元类错误

python - 枕头属性错误

python - 关于 PIL 错误 -- IOError : decoder zip not available

python - mingw32-gcc 编译 Cython 输出 : unknown multiarch location for pyconfig. h (和其他警告)

python - 邮件确认错误 rest-auth

python - 迄今为止的 Django 模板标签 ISO

python - scipy/numpy 中 matlab interp2 样条线的等效项

python - 当分离 < 1 px 时在 python 中使用 PIL 检测图像形状边缘的算法