标签: python-imaging-library

使用PIL中的Image.point()方法来处理像素数据

我正在使用Python Imaging Library使用定义颜色关系的查找表为黑白图像着色.查找表只是一个256元素的RGB元组列表:

>>> len(colors)
256
>>> colors[0]
(255, 237, 237)
>>> colors[127]
(50, 196, 33)
>>> 
Run Code Online (Sandbox Code Playgroud)

我的第一个版本使用了getpixel()putpixel()方法:

    for x in range(w):
        for y in range(h):
            pix = img.getpixel((x,y))
            img.putpixel((x,y), colors[pix[0]])
Run Code Online (Sandbox Code Playgroud)

这非常缓慢.一份profile报告指出这些putpixelgetpixel方法是罪魁祸首.稍微调查(即阅读文档),我发现" 请注意,这种方法相对较慢. "re : putpixel. (实际运行时间:对于1024x1024图像为53s in putpixelgetpixel50ss)

根据文档中的建议,我使用im.load()并直接使用像素访问:

    pixels = img.load()
    for x in range(w):
        for y in range(h):
            pix = pixels[x, y]
            pixels[x, y] = colors[pix[0]]                
Run Code Online (Sandbox Code Playgroud)

处理加速了一个数量级,但仍然 …

python image-manipulation image image-processing python-imaging-library

19
推荐指数
1
解决办法
3万
查看次数

使用PIL在中心裁剪图像

如何在中心裁剪图像?因为我知道盒子是一个定义左,上,右和下像素坐标的4元组,但我不知道如何获得这些坐标,所以它在中心作物.

python crop python-imaging-library

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

ImportError:无法导入名称_imaging

我安装了Pillow,之后我想做:

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

我收到以下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 61, in <module>
ImportError: cannot import name _imaging
Run Code Online (Sandbox Code Playgroud)

但是,如果我单独导入这些,一切都很好,即:

import _imaging
import Image
Run Code Online (Sandbox Code Playgroud)

你知道问题可能是什么吗?

python django imaging python-imaging-library pillow

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

使用Xcode4在OS X Snow Leopard上安装PIL(无PPC支持)

Xcode4放弃了PPC支持,所以当我尝试构建PIL时,它会引发仇恨:

Bens-MacBook-Air:Imaging-1.1.7 bkeating$ python setup.py build
running buildrunning build_pyrunning build_ext
--- using frameworks at /System/Library/Frameworks
building '_imaging' extension
/usr/bin/gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -DHAVE_LIBJPEG -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -I/usr/local/include/freetype2 -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.6/include -I/usr/local/include -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c decode.c -o build/temp.macosx-10.6-universal-2.6/decode.o
/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed
Installed assemblers are:
/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64
/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386
decode.c:688: fatal error: error writing to -: Broken pipe …
Run Code Online (Sandbox Code Playgroud)

python xcode powerpc python-imaging-library

18
推荐指数
3
解决办法
6702
查看次数

在Numpy图像中查找子图像

我有两个从PIL图像转换的Numpy数组(3维uint8).

我想查找第一个图像是否包含第二个图像,如果是,请找出匹配所在的第一个图像内左上角像素的坐标.

有没有办法在Numpy中以足够快的方式完成这个,而不是使用(4!非常慢)纯Python循环?

2D示例:

a = numpy.array([
    [0, 1,  2,  3],
    [4, 5,  6,  7],
    [8, 9, 10, 11]
])
b = numpy.array([
    [2, 3],
    [6, 7]
])
Run Code Online (Sandbox Code Playgroud)

怎么做这样的事情?

position = a.find(b)
Run Code Online (Sandbox Code Playgroud)

position那会是(0, 2).

python numpy image python-imaging-library

18
推荐指数
2
解决办法
1万
查看次数

用Python堆叠天文图像

我觉得这会更容易,但过了一段时间我终于放弃了这个,至少在几个小时......

我想从一组游戏中时光倒流的照片中重现这个尾随的星星图像.灵感来自于: 灵感

原作者使用VirtualDub拍摄的低分辨率视频帧并与imageJ结合使用.我想我可以轻松地重现这个过程,但是使用Python更具记忆意识的方法,所以我可以使用原始的高分辨率图像来获得更好的输出.

我的算法的想法很简单,一次合并两个图像,然后通过将得到的图像与下一个图像合并来迭代.这样做了几百次并且适当地称重它,以便每个图像对最终结果具有相同的贡献.

我对python很新(我不是专业的程序员,这很明显),但环顾四周我认为Python Imaging Library是非常标准的,所以我决定使用它(如果你认为的话,请纠正我)别的东西会更好).

这是我到目前为止所拥有的:

#program to blend many images into one
import os,Image
files = os.listdir("./")
finalimage=Image.open("./"+files[0]) #add the first image
for i in range(1,len(files)): #note that this will skip files[0] but go all the way to the last file
  currentimage=Image.open("./"+files[i])
  finalimage=Image.blend(finalimage,currentimage,1/float(i+1))#alpha is 1/i+1 so when the image is a combination of i images any adition only contributes 1/i+1.
  print "\r" + str(i+1) + "/" + str(len(files)) #lousy progress indicator
finalimage.save("allblended.jpg","JPEG")
Run Code Online (Sandbox Code Playgroud)

这样做了它应该做的但是得到的图像是黑暗的,如果我只是试图增强它,很明显由于像素的值缺乏深度而导致信息丢失.(我不确定这里适当的术语是什么,颜色深度,颜色精度,像素大小).这是使用低分辨率图像的最终结果:

分辨率低的结果

或者我用4k×2k分辨率(从另一组照片中)尝试: …

python image color-depth astronomy python-imaging-library

18
推荐指数
1
解决办法
5663
查看次数

Python,PIL; 文字到图像和字体

我有一个问题,在Python和PIL下将文本写入图像 - 我能够将文本写入png文件,但不是粗体文本.谁能提供一个如何实现这个目标的例子?

我认为最简单的解决方案可能是使用文本的粗体变体,但我无法在Windows/font文件夹中看到提供此内容的任何内容 - 这是否意味着字体类型具有'粗体属性'即T/F?:
快速查看windows下的粗体字体

我正在使用的代码:

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

# font = ImageFont.truetype("Arial-Bold.ttf",14)
font = ImageFont.truetype("Arial.ttf",14)
img=Image.new("RGBA", (500,250),(255,255,255))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
Run Code Online (Sandbox Code Playgroud)

python text image bold python-imaging-library

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

如何将SimpleGUI与Python 2.7和3.0 shell集成

我正在从Coursera学习Python.在本课程中,他们在CodeSkulptor上使用SimpleGUI模块.谁能告诉我如何将SimpleGUI与python 2.7和3.0 shell集成?

python python-imaging-library python-2.7 python-3.x codeskulptor

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

由于空EXTENSION数组,PIL的保存功能中的'未知扩展'

我对python很新,并且savePIL的Pillow fork 的功能有问题.

用这个最小的例子

import Image

im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 1667, in save
  raise KeyError(ext)  # unknown extension
KeyError: '.png'
Run Code Online (Sandbox Code Playgroud)

save函数中的相应行是

preinit()

[...]

try:
  format = EXTENSION[ext]
except KeyError:
  raise KeyError(ext)  # unknown extension
Run Code Online (Sandbox Code Playgroud)

我查看了EXTENSION数组并检测到它是空的,尽管它应该preinit()由例如初始化from PIL import PngImagePlugin.PngImagePlugin.py电话Image.register_extension("PNG", ".png").观察此函数内部或其内部的数组PngImagePlugin确实充满了文件扩展名.

print(EXTENSION)然后在try-except-block之前放置一个空EXTENSION数组.

(与函数中SAVE的几行相同的问题相同save.)

任何帮助表示赞赏.

编辑:我最近从OpenSuse 13.1升级.到13.2.它在13.1中运行良好,但在13.2中没有运行.

python python-imaging-library pillow

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

如何在Python中打印图像上的印地语句子(unicode)?

我有一个名为"hindi.txt"的文件.它的内容如下.我正在使用Python3.5.

??????? ??????? ?? ??? ??? ??? ?????? ?????????, ??????? ???? ?????: ???????
9 ??? ?? ???? ????? ??? ?????, 59000 Cr ??? ???? ??????? 36 ????? ?????
WhatsApp ?? ????? ???? ??????????? ?????????? ?? ??? ??? ???? ?? Allo ???????
???? ???? ?? 10 ??????: ????? ??? ??? 150 ???? ???? ?? ??? ?? ?? ??? ?? ?????
???? ????? ???? ?? ???? ??? ????? PAK ?? LoC ?? ??? ????? ??? ????? 
PAK ?? ????? ??? ???? ???? …
Run Code Online (Sandbox Code Playgroud)

python unicode python-imaging-library hindi pillow

18
推荐指数
1
解决办法
1630
查看次数