使用 pillow 创建和编辑图像时出现错误 "attribute access"
created and edited image with pillow, gives an error "attribute access"
所以,我正在尝试创建一个用颜色填充的图像,然后显示它以进行调试。我用枕头创建了图像,将其设置为黑色或 rgb 中的 (0,0,0),我循环浏览并用实际颜色填充图像,等等。但是后来我得到了一些我不知道的属性的错误'知道如何填写 python.
import cv2
import tkinter as tk
import copy
from PIL import Image
from tkinter import filedialog
colorwheel = Image.new('RGB', (1, 255*7), color='black')
colorwheel = colorwheel.load()
colorwheel[0,1]=(255,255,0)
colorwheel[0,2] = (255, 255, 0)
r=255
g=255
b=0
#keep value of green, add take away red
for i in range( 255):
colorwheel[0, i] = (255-1, 255, 0)
#go to blue take away green
for i in range( 255):
colorwheel[0,255*2 + i] = (0, 255-i,0+1)
#go to purple 128 keep blue, add red to 128
for i in range (255):
colorwheel[0, 255*3 + i] = (int(i/2), 0, 255)
#go to red
for i in range( 255):
colorwheel[0,255*4 + i]=(int(i/2),0,255-i)
#go to orange
for i in range(255):
colorwheel[0, 255 * 5 + i] = (128, int(i / 2), 0) #keep red 128
for i in range(255):
colorwheel[0, 255 * 6 + i] =(128,int(128+i/2),0)
colorwheel.resize((50,255*7))
cv2.imshow('image', colorwheel)
它应该具有调整大小和显示图像所需的一切,但我明白了。
File "C:/Users/misterE/PycharmProjects/frame2cc/base contraster", line 64, in <module>
colorwheel.resize((50,255*7))
AttributeError: 'PixelAccess' object has no attribute 'resize'
根据 Pillow docs,PixelAccess
似乎没有函数 resize()
。
如果您要调整图片大小,this post about resizing an image using PIL 可能会有用!
尝试:-
import cv2
import tkinter as tk
import copy
from PIL import Image
from tkinter import filedialog
colorwheel1 = Image.new('RGB', (1, 255*7), color='black')
colorwheel = colorwheel1.load()
colorwheel[0,1]=(255,255,0)
colorwheel[0,2] = (255, 255, 0)
r=255
g=255
b=0
#keep value of green, add take away red
for i in range( 255):
colorwheel[0, i] = (255-1, 255, 0)
#go to blue take away green
for i in range( 255):
colorwheel[0,255*2 + i] = (0, 255-i,0+1)
#go to purple 128 keep blue, add red to 128
for i in range (255):
colorwheel[0, 255*3 + i] = (int(i/2), 0, 255)
#go to red
for i in range( 255):
colorwheel[0,255*4 + i]=(int(i/2),0,255-i)
#go to orange
for i in range(255):
colorwheel[0, 255 * 5 + i] = (128, int(i / 2), 0) #keep red 128
for i in range(255):
colorwheel[0, 255 * 6 + i] =(128,int(128+i/2),0)
colorwheel1.resize((50,255*7))
colorwheel1.show()
输出图像:-
您出错的原因是,您试图在 PixelAccess
元素上使用 Image
对象命令 (Image.resize()
)。这导致了这个错误。为了解决这个问题,我们只是将原始 Image 对象的名称更改为 colorwheel1
,以便我们可以进一步使用该对象来调整大小。
P.S.:- 最后我用 Image.show()
来显示图像而不是 cv2.imshow()
.
所以,我正在尝试创建一个用颜色填充的图像,然后显示它以进行调试。我用枕头创建了图像,将其设置为黑色或 rgb 中的 (0,0,0),我循环浏览并用实际颜色填充图像,等等。但是后来我得到了一些我不知道的属性的错误'知道如何填写 python.
import cv2
import tkinter as tk
import copy
from PIL import Image
from tkinter import filedialog
colorwheel = Image.new('RGB', (1, 255*7), color='black')
colorwheel = colorwheel.load()
colorwheel[0,1]=(255,255,0)
colorwheel[0,2] = (255, 255, 0)
r=255
g=255
b=0
#keep value of green, add take away red
for i in range( 255):
colorwheel[0, i] = (255-1, 255, 0)
#go to blue take away green
for i in range( 255):
colorwheel[0,255*2 + i] = (0, 255-i,0+1)
#go to purple 128 keep blue, add red to 128
for i in range (255):
colorwheel[0, 255*3 + i] = (int(i/2), 0, 255)
#go to red
for i in range( 255):
colorwheel[0,255*4 + i]=(int(i/2),0,255-i)
#go to orange
for i in range(255):
colorwheel[0, 255 * 5 + i] = (128, int(i / 2), 0) #keep red 128
for i in range(255):
colorwheel[0, 255 * 6 + i] =(128,int(128+i/2),0)
colorwheel.resize((50,255*7))
cv2.imshow('image', colorwheel)
它应该具有调整大小和显示图像所需的一切,但我明白了。
File "C:/Users/misterE/PycharmProjects/frame2cc/base contraster", line 64, in <module>
colorwheel.resize((50,255*7))
AttributeError: 'PixelAccess' object has no attribute 'resize'
根据 Pillow docs,PixelAccess
似乎没有函数 resize()
。
如果您要调整图片大小,this post about resizing an image using PIL 可能会有用!
尝试:-
import cv2
import tkinter as tk
import copy
from PIL import Image
from tkinter import filedialog
colorwheel1 = Image.new('RGB', (1, 255*7), color='black')
colorwheel = colorwheel1.load()
colorwheel[0,1]=(255,255,0)
colorwheel[0,2] = (255, 255, 0)
r=255
g=255
b=0
#keep value of green, add take away red
for i in range( 255):
colorwheel[0, i] = (255-1, 255, 0)
#go to blue take away green
for i in range( 255):
colorwheel[0,255*2 + i] = (0, 255-i,0+1)
#go to purple 128 keep blue, add red to 128
for i in range (255):
colorwheel[0, 255*3 + i] = (int(i/2), 0, 255)
#go to red
for i in range( 255):
colorwheel[0,255*4 + i]=(int(i/2),0,255-i)
#go to orange
for i in range(255):
colorwheel[0, 255 * 5 + i] = (128, int(i / 2), 0) #keep red 128
for i in range(255):
colorwheel[0, 255 * 6 + i] =(128,int(128+i/2),0)
colorwheel1.resize((50,255*7))
colorwheel1.show()
输出图像:-
您出错的原因是,您试图在 PixelAccess
元素上使用 Image
对象命令 (Image.resize()
)。这导致了这个错误。为了解决这个问题,我们只是将原始 Image 对象的名称更改为 colorwheel1
,以便我们可以进一步使用该对象来调整大小。
P.S.:- 最后我用 Image.show()
来显示图像而不是 cv2.imshow()
.