我正在尝试拍摄硬币的灰度图像,并移动像素以获得新图像。看来我的代码正在将像素重新分配到新图像中,但是最后看不到任何内容。这是我第一次使用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
答案 0 :(得分:0)
请确保您的代码正在运行。
以下是可帮助您的代码段
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矩阵)。然后,修改像素只是对张量元素的重新排列。