【发布时间】:2018-11-11 17:54:06
【问题描述】:
在高中,我们从 Python 开始,我们被要求使用 Pillow 模块编写一个小型图像处理器。目标是打开一个文件,然后选择一个过滤器。
我尝试编程的第一个滤镜是改变色温。 但是还有一个带有 tkinter 的极简界面,它显示了一些按钮,并且可以正常工作。
这是打开文件的函数
def Ouvrir():
fichier = askopenfilename(title="Ouvrir une image",filetypes=[('jpg files','.jpg'),('all files','.*')])
global img
img =Image.open(fichier)
l, h = img.size
img.show() #visualisation de l'image
global img2
img2 = img #img2 est une copie de img
它创建一个从文件加载的全局img 图像对象。然后l和h加载图片的宽高。
为输出图像创建另一个全局对象图像img2,它是img的副本。
那么这里就是处理图像的函数
def filtreTC(): #Filtre permettant de changer la température de couleur
coef = sliderTC.get() / 100 #On récupère le coefficient à partir de l'échelle. Le coefficient compris entre -1 et 1: -1 = froid (image bleu-vert), 0 = neutre, 1 = chaud (image orangée)
fenTC.destroy() #On ferme la fenêtre
if(coef <= 0): #Calcul des coefficients rouges, verte et bleus
coefR = 1 + coef
coefV = 1 + (coef / 2)
coefB = 1
else:
coefR = 1
coefV = 1 - (coef / 2)
coefB = 1 - coef
for y in range(0, h, 1):
for x in range(0, l, 1):
r, v, b = img.getpixel((x, y))
r = int(float(r * coefR))
v = int(float(v * coefV))
b = int(float(b * coefB))
img2.putpixel((x, y), (r, v, b))
img2.show()
这是一个循环,它扫描整个图像并从img 中获取一个像素,将其 rgb 值乘以其相应的系数,然后将该像素放入img2
问题是它不起作用。它没有给出任何错误,但是当它显示img2 时,它与img1 相同,就像putpixel 函数没有做任何事情一样。
我检查了很多东西,所以我知道问题既不是 rgb 值也不是 x/y 坐标。
- 我尝试将
img2 = img替换为img2 = Image.new("RGB", (l, h)),但得到的是黑色图像。 - 然后我尝试用
img2.putpixel((100, 100), (127, 127, 127))替换img2.putpixel((x, y), (r, v, b)),这样我应该在左上角附近得到一个灰色像素。但我仍然得到一张黑色图像。
然后我尝试删除
global img2
img2 = img
来自打开文件并放置的函数
img2 = Image.new("RGB", (l, h))
就在fenTC.destroy() 之后,我得到了这个:
Exception in Tkinter callback
Traceback (most recent call last):
File "e:\xxxx\programmes\anaconda\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "H:\ISN\Programmes\TP-image\projet.py", line 62, in filtreTC
img2.show()
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\Image.py", line 2016, in show
_show(self, title=title, command=command)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\Image.py", line 2876, in _show
_showxv(image, **options)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\Image.py", line 2881, in _showxv
ImageShow.show(image, title, **options)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\ImageShow.py", line 51, in show
if viewer.show(image, title=title, **options):
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\ImageShow.py", line 75, in show
return self.show_image(image, **options)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\ImageShow.py", line 95, in show_image
return self.show_file(self.save_image(image), **options)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\ImageShow.py", line 91, in save_image
return image._dump(format=self.get_format(image), **self.options)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\Image.py", line 639, in _dump
self.save(filename, format, **options)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\Image.py", line 1969, in save
save_handler(self, fp, filename)
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\BmpImagePlugin.py", line 319, in _save
(rawmode, stride, -1))])
File "e:\xxxx\programmes\anaconda\lib\site-packages\PIL\ImageFile.py", line 512, in _save
e.setimage(im.im, b)
SystemError: tile cannot extend outside image
基本上很多东西我都不知道是什么意思,除了:
SystemError: tile cannot extend outside image
我检查了 x 和 y 坐标,它们从未超出边界,所以我不明白这是什么错误。
我花了几个小时试图找出问题所在,并尝试了很多不同的方法,但都没有奏效。所以我认为最好寻求帮助
【问题讨论】:
-
避免在函数中使用全局变量。这让我们很难或不可能真正知道发生了什么。而是包含minimal reproducible example。使用 all 重现您的问题所需的代码。如果您收到“图块无法扩展到图像外部”的错误消息,则表明您对图像形状做出了一些错误的假设。
标签: python image-processing python-imaging-library