标签: python-imaging-library

使用粗糙灰度算法的问题?

所以我设计了编辑照片的几个方案中python使用PIL,其中之一是将图像转换为灰度图像(我避免因使用任何功能PIL).

我已经采用的算法是简单的:对于每个像素(色彩深度为24),我已经计算出的平均R,GB值,并设置RGB值到该平均.

我的程序生成的灰度图像似乎很准确,但我想知道我是否使用了正确的算法,并且我遇到了一个问题的答案,似乎"正确"的算法是计算的0.299 R + 0.587 G + 0.114 B.

我决定将我的程序与这个算法进行比较.我使用我的程序生成了一个灰度图像,另一个(使用相同的输入)来自一个网站(谷歌的最高结果)'image to grayscale'.

在我的肉眼看来,它们似乎完全相同,如果有任何变化,我看不到它.但是,我决定使用这个网站(谷歌的最佳结果'compare two images online')来比较我的灰度图像.事实证明,在像素的深处,它们有轻微的变化,但没有一个人乍一看是可感知的(差异可以被发现,但通常只有当图像相互叠加或在几毫秒之间切换时) .

我的问题(第一个是主要问题):

  1. 使用我的"粗糙"灰度算法有什么缺点吗?
  2. 有没有人有任何输入图像,我的灰度算法会产生一个明显不同的图像与"正确"的图像?
  3. 是否有任何颜色/ RBG组合我的算法不能正常工作?

我的关键代码(如果需要):

def greyScale(pixelTuple):
    return tuple([round(sum(pixelTuple) / 3)] * 3)
Run Code Online (Sandbox Code Playgroud)

'正确'的算法(似乎重量很重):

def greyScale(pixelTuple):
    return tuple([round(0.299 * pixelTuple[0] + 0.587 * pixelTuple[1] + 0.114 * pixelTuple[2])] * 3)
Run Code Online (Sandbox Code Playgroud)

我的输入图片: 我的输入图片

我的算法产生的灰度图像: 我的算法产生的灰度图像

"正确"的灰度图像:

当在线比较灰度图像时(突出显示红色是差异,使用10%的模糊): 当在线比较灰度图像时(突出显示红色是差异,使用10%的模糊)

尽管上面突出显示了像素的变化,但上面的灰度图像看起来几乎完全相同(至少对我而言).

另外,关于我的第一个问题,如果有人感兴趣, …

python algorithm image-comparison image-conversion python-imaging-library

60
推荐指数
6
解决办法
3259
查看次数

如何在屏幕上显示PIL图像?

我正在使用PIL库进行一些图像编辑.关键是,我不想每次都将图像保存在硬盘上以便在资源管理器中查看.是否有一个小模块可以让我设置一个窗口并显示图像?

python image python-imaging-library

59
推荐指数
6
解决办法
14万
查看次数

ImportError:没有名为Image的模块

我也试过了

from PIL import Image
Run Code Online (Sandbox Code Playgroud)

但它给了我ImportError:没有名为PIL的模块.

我已经成功安装了PIL

pip install pil
Run Code Online (Sandbox Code Playgroud)

我还安装了xcode命令行工具.似乎没什么用.

细节:

Mac OS X 10.9

Python 2.7.6

python是/Library/Frameworks/Python.framework/Versions/2.7/bin/python

python是/ usr/bin/python

python是/ usr/local/bin/python

姓名:PIL

版本:1.1.7

位置:/usr/local/lib/python2.7/site-packages/PIL

python macos python-imaging-library

58
推荐指数
1
解决办法
20万
查看次数

pip install PIL没有安装到virtualenv中

如何安装PIL?

>pip install PIL

Downloading/unpacking PIL
  Could not find any downloads that satisfy the requirement PIL  
  Some externally hosted files were ignored (use --allow-external PIL to allow). 
Cleaning up... 
No distributions at all found for PIL 
Storing debug log for failure in /root/.pip/pip.log 

>pip uninstall PIL
Can't uninstall 'PIL'. No files were found to uninstall.
Run Code Online (Sandbox Code Playgroud)

pip python-imaging-library

57
推荐指数
2
解决办法
5万
查看次数

如何为Python 3.6安装PIL/Pillow?

我有一个需要PIL运行的脚本.除了降级我的Python,我无论如何都找不到在我的Python 3.6上安装PIL

以下是我的尝试:

pip install pil
Collecting pil
  Could not find a version that satisfies the requirement pil (from versions: )
No matching distribution found for pil

pip install Pillow
Collecting Pillow
  Using cached Pillow-3.3.1.zip
Installing collected packages: Pillow
  Running setup.py install for Pillow ... error
    Complete output from command c:\python\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ABDULR~1\\AppData\\Local\\Temp\\pip-build-rez5zpri\\Pillow\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\ABDULR~1\AppData\Local\Temp\pip-a5bugnjo-record\install-record.txt --single-version-externally-managed --compile:
    Single threaded build for windows
    running install
    running build
    running build_py
    creating build
    creating …
Run Code Online (Sandbox Code Playgroud)

python-imaging-library python-3.x pillow

55
推荐指数
2
解决办法
14万
查看次数

使用PIL使所有白色像素透明?

我正在尝试使用Python Image Library使所有白色像素透明.(我是一个C黑客试图学习python所以要温柔)我已经有转换工作(至少像素值看起来正确)但我无法弄清楚如何将列表转换为缓冲区来重新创建图片.这是代码

img = Image.open('img.png')
imga = img.convert("RGBA")
datas = imga.getdata()

newData = list()
for item in datas:
    if item[0] == 255 and item[1] == 255 and item[2] == 255:
        newData.append([255, 255, 255, 0])
    else:
        newData.append(item)

imgb = Image.frombuffer("RGBA", imga.size, newData, "raw", "RGBA", 0, 1)
imgb.save("img2.png", "PNG")
Run Code Online (Sandbox Code Playgroud)

python image python-imaging-library

54
推荐指数
6
解决办法
7万
查看次数

PIL:缩略图,最后是方形图像

调用

image = Image.open(data)
image.thumbnail((36,36), Image.NEAREST)
Run Code Online (Sandbox Code Playgroud)

将保持纵横比.但我需要最终显示这样的图像:

<img src="/media/image.png" style="height:36px; width:36px" />
Run Code Online (Sandbox Code Playgroud)

我可以在图像周围使用透明或白色的信箱样式吗?

python png alpha thumbnails python-imaging-library

54
推荐指数
2
解决办法
3万
查看次数

54
推荐指数
5
解决办法
3万
查看次数

如何使用PIL减小图像文件大小

我正在使用PIL通过将较大的图像转换为较小的图像来调整图像的大小.有没有任何标准的方法来减少图像的文件大小而不会过多地损失质量,让我们说图像的原始大小是100KB,我想把它降低到5或10 KB,特别是对于png和jpeg格式.

python compression image python-imaging-library

54
推荐指数
5
解决办法
8万
查看次数

Python PIL"IOError:图像文件被截断",带有大图像

我认为这个问题与Zope无关.尽管如此,我还是要解释一下我要做的事情:

我在Zope中使用PUT_factory将图像上传到每个FTP的ZODB.上传的图像将作为Zope Image保存在新创建的容器对象中.这工作正常,但我想调整图像大小,如果它超过一定的大小(宽度和高度).所以我使用PIL的缩略图功能来调整它们的大小,即200x200.只要上传的图像相对较小,这就可以正常工作.我没有查看确切的限制,但976x1296px仍然可以.

随着更大的图片,我得到:

Module PIL.Image, line 1559, in thumbnail
Module PIL.ImageFile, line 201, in load
IOError: image file is truncated (nn bytes not processed).
Run Code Online (Sandbox Code Playgroud)

我从相机测试了很多jpeg.我不认为它们都被截断了.

这是我的代码:

if img and img.meta_type == 'Image':
  pilImg = PIL.Image.open( StringIO(str(img.data)) )
elif imgData:
  pilImg = PIL.Image.open( StringIO(imgData) )

pilImg.thumbnail((width, height), PIL.Image.ANTIALIAS)
Run Code Online (Sandbox Code Playgroud)

因为我正在使用PUT_factory,所以我没有文件对象,我使用的是工厂的原始数据或以前创建的(Zope)Image对象.

我听说PIL在超过一定大小时处理图像数据的方式不同,但我不知道如何调整代码.或者它与PIL的延迟加载有关?

python zope image python-imaging-library

54
推荐指数
4
解决办法
4万
查看次数