- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在编写脚本,基本上我需要它:
使用 ImageMagick 有一种非常简单的方法来执行此操作(尽管您需要一些 Linux 实用程序来处理输出文本),但我并没有真正看到如何使用 Python 和 PIL 来执行此操作。
这是我目前所拥有的:
from PIL import Image
image_file = 'test.tiff'
image = Image.open(image_file).convert('L')
histo = image.histogram()
histo_string = ''
for i in histo:
histo_string += str(i) + "\n"
print(histo_string)
这输出了一些东西(我正在寻找结果图表),但它看起来与 ImageMagick 输出完全不同。我用它来检测扫描书籍的接缝和内容。
感谢所有提供帮助的人!
我现在有一个(看起来很讨厌的)解决方案:
from PIL import Image
import numpy
def smoothListGaussian(list,degree=5):
window=degree*2-1
weight=numpy.array([1.0]*window)
weightGauss=[]
for i in range(window):
i=i-degree+1
frac=i/float(window)
gauss=1/(numpy.exp((4*(frac))**2))
weightGauss.append(gauss)
weight=numpy.array(weightGauss)*weight
smoothed=[0.0]*(len(list)-window)
for i in range(len(smoothed)):
smoothed[i]=sum(numpy.array(list[i:i+window])*weight)/sum(weight)
return smoothed
image_file = 'verypurple.jpg'
out_file = 'out.tiff'
image = Image.open(image_file).convert('1')
image2 = image.load()
image.save(out_file)
intensities = []
for x in xrange(image.size[0]):
intensities.append([])
for y in xrange(image.size[1]):
intensities[x].append(image2[x, y] )
plot = []
for x in xrange(image.size[0]):
plot.append(0)
for y in xrange(image.size[1]):
plot[x] += intensities[x][y]
plot = smoothListGaussian(plot, 10)
plot_str = ''
for x in range(len(plot)):
plot_str += str(plot[x]) + "\n"
print(plot_str)
最佳答案
我看到您正在使用 numpy。我会先将灰度图像转换为 numpy 数组,然后使用 numpy 沿轴求和。奖励:当您将平滑函数修复为接受一维数组作为输入时,您可能会发现它运行得更快。
>>> from PIL import Image
>>> import numpy as np
>>> i = Image.open(r'C:\Pictures\pics\test.png')
>>> a = np.array(i.convert('L'))
>>> a.shape
(2000, 2000)
>>> b = a.sum(0) # or 1 depending on the axis you want to sum across
>>> b.shape
(2000,)
关于python - PIL : Create one-dimensional histogram of image color lightness?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4188104/
我正在尝试使用预先训练的模型。这就是问题发生的地方 模型不是应该接受简单的彩色图像吗?为什么它需要 4 维输入? RuntimeError T
我正在尝试实现自定义用户控件。 让我们考虑 ViewModels: public class FileViewModel { public string Name { get; set; }
我承认,我脑子有问题。我已经走得够远了,几乎可以看到隧道尽头的曙光,但我不确定要采取的下一步。 我创建了一个 SQLfiddle example here 这是 SQL 数据透视表: SET @sql
我收到 SSAS 错误“函数中指定的两个集合具有不同的维度”。 我实际上使用相同的维度,相同的层次结构(隐式),在一组中我使用“全部”成员,而在另一组中我使用叶成员。如果我更改成员标识符以显式引用(单
我有以下值,每个值都是 double 类型的标量:a1, a2, a3, a4, a5 . 我尝试使用 Numpy 连接它们,如下所示: f = np.concatenate((a1,a2,a3,a4
我有一个功能 findMaxEval我以以下方式调用:eMax0,var0=findMaxEval(np.diag(eVal0),q,bWidth=.01)哪里np.diag(eVal0)是一个形状为
我复制了spline example具有以下点(而不是随机点): var points = [[75, 15], [104, 80], [275, 80], [300, 15]]; 它工作完美。现在我
我正在尝试将值传递给后台 worker 。我之前发帖询问如何做到here 。一个答案将我引向了这段代码,但我在尝试使用它时遇到了问题。这就是我正在做的事情: Class MyParameters
问题是这样的:程序的输出是什么? #include int main() { int A[2][10]={{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,1
我可以像这样内联创建一个 jquery 对象(这段代码有效) $('#tip').qtip({ content: el.REASON, position
我正在尝试模拟一维伊辛模型。该模型包含自旋链(100 次自旋),并使用 Mont Carlo - Metropolis 在系统能量(单一)下降或小于随机数时接受自旋翻转。在正确的程序中,能量和磁化强度
我试图从 Ruby 调用的 C 函数是这样的: void foo(double *in_array, double *out_array) 哪里: in_array 是一组数组,“foo”将使用这些数
我正在尝试校准加速度计,但我无法获得校准所需的 6 个不同加速度读数的 6 个样本值。 PreliminaryW 是一个 double[6][3] 数组,用于填充这些样本值。它是 6 x 3,因为每个
我很难想出一种有效的方法来找到二维容器中给定值的所有相邻方 block 。假设我有一个容器表示为: . . . . . . G . . . . . . . . . . . . . . . . . G
例如:一个二维数组可以想象成一堵方砖砌成的砖墙,其中每 block 砖代表我们数组中的一个坐标。 3 维数组可以同样的方式可视化为一个盒子或立方体。 但是,这是棘手的部分,您如何可视化具有多个(超过
我有以下代码通过 seaborn 创建一个表格和一个条形图。 #Building a dataframe grouped by the # of Engagement Types sales_type
我在 excel 中有一系列单元格,左列有人名,顶行有某些任务(作为数字),比如两个暗淡的数组。我想做的就是能够搜索名称和编号的任务,以查看该单元格在 VBA 中是否为空白。 在 excel 表中,我
问题总结:自定义函数的backward pass中如何处理输入和输出的维度? 根据manual ,自定义函数的基本结构如下: class MyFunc(torch.autograd.Function)
我一直在用 Javascript 开发一个项目,在达到一定的开发阶段后,代码停止工作。我已将问题范围缩小到在 Javascript 中创建和索引“多维”数组。我包含的代码只是为了测试创建数组的数组、为
package arrays; import java.util.Arrays; public class Route { int cityindex; int stadtwahl; String[]
我是一名优秀的程序员,十分优秀!