将 ImageMagick 直方图数据记录到 Excel/Google 张中
Recording ImageMagick histogram data into Excel/Google Sheets
我已经将一些代码放在一起,它们将通过一个文件夹,打开所有具有特定结尾的图像,并使用 ImageMagick 创建它们的直方图。我不能做的(也许这是一个概念化问题,因为我对此还很陌生)是弄清楚如何将其记录到电子表格中,最好附上文件名。 PyXl 似乎可以与 Pandas 和 Numpy 一起使用,但我无法弄清楚获取此输出并记录它的路径。
是否有获取直方图输出并将其记录在电子表格中的解决方案?
编辑:到目前为止添加我的代码。在 Windows 10 Pro 中运行,使用 VSCode.
#import necessary packages
import os
import wand
import OpenPyXl
#define color library
magick xc:AliceBlue xc:AntiqueWhite xc:aquamarine xc:brown xc:azure xc:beige xc:bisque xc:black xc:BlanchedAlmond xc:blue xc:BlueViolet xc:chocolate4 xc:firebrick4 xc:burlywood xc:cadet blue xc:chartreuse xc:chocolate xc:tan4 xc:coral xc:CornflowerBlue xc:cornsilk xc:crimson xc:blue4 xc:cyan4 xc:DarkGoldenrod xc:DarkGreen xc:DarkGray xc:DarkKhaki xc:DarkMagenta xc:DarkOliveGreen xc:DarkOrange xc:DarkOrchid xc:DarkRed xc:DarkSalmon xc:DarkSeaGreen xc:DarkSlateBlue xc:DarkSlateGray xc:DarkTurquoise xc:DarkViolet xc:DeepPink xc:DeepSkyBlue xc:DimGray xc:DodgerBlue xc:firebrick xc:FloralWhite xc:ForestGreen xc:fuchsia xc:gainsboro xc:GhostWhite xc:gold xc:goldenrod xc:fractal xc:green xc:GreenYellow xc:honeydew xc:HotPink xc:IndianRed xc:indigo xc:ivory xc:khaki xc:lavender xc:LavenderBlush xc:LawnGreen xc:LemonChiffon xc:LightBlue xc:LightCoral xc:LightCyan xc:LightGoldenrodYellow xc:LightGray xc:LightGreen xc:LightPink xc:LightSalmon xc:LightSeaGreen xc:LightSkyBlue xc:LightSlateGray xc:LightSteelBlue xc:LightYellow xc:green1 xc:LimeGreen xc:linen xc:maroon xc:aquamarine3 xc:blue3 xc:MediumOrchid xc:MediumPurple xc:MediumSeaGreen xc:MediumSlateBlue xc:MediumSpringGreen xc:MediumTurquoise xc:MediumVioletRed xc:MidnightBlue xc:MintCream xc:MistyRose xc:moccasin xc:NavajoWhite xc:navy xc:OldLace xc:olive xc:OliveDrab xc:orange xc:OrangeRed xc:orchid xc:PaleGoldenrod xc:PaleGreen xc:PaleTurquoise xc:PaleVioletRed xc:PapayaWhip xc:PeachPuff xc:peru xc:pink xc:plum xc:PowderBlue xc:purple xc:red xc:RosyBrown xc:RoyalBlue xc:chocolate4 xc:salmon xc:SandyBrown xc:SeaGreen xc:seashell xc:sienna xc:silver xc:SkyBlue xc:SlateBlue xc:SlateGray xc:snow xc:SpringGreen xc:SteelBlue xc:tan xc:teal xc:thistle xc:tomato xc:turquoise xc:violet xc:wheat xc:gray100 xc:gray96 xc:yellow xc:OliveDrab3
#define photo folder
directory = 'C:\Users\tr9800a\Downloads\Bilder'
#find files ending in -2 and remap them with defined colors
for filename in os.listdir(directory):
if filename.endswith("-2.jpg"):
magick filename +dither -remap map.jpg result.jpg
magick identify -verbose result.jpg | grep -A9 Histogram
#write results to spreadsheet
(this is where I've gotten stuck)
continue
else:
我完全接受我可能犯了一些重大错误。再一次,菜鸟在这里。
大约有13000张照片要解决,希望能从每张图片中捕捉到四个最常见的颜色值。我使用 Magick 重新映射作为一种快捷方式,将颜色的细微变化转换为我们系统中拥有的 140 种核心颜色之一。
最理想的输出是:
图像 ID、最常见的十六进制代码、像素计数、第二常见的十六进制代码、像素计数、第三十六进制代码、像素计数、第四十六进制代码、像素计数
经过反思,我想我可能会用 PIL、wand 或 OpenCV 而不是解析 ImageMagick 的输出,这有点丑陋且容易出错。我还没有找到完整的答案,但这些想法可能会让你入门:
我建议您使用无损 PNG 或 GIF 格式,而不是使用有损 JPEG 作为调色板。您可以使用如下命令制作(小)调色板文件以进行重新映射:
magick xc:AliceBlue xc:AntiqueWhite xc:aquamarine ... +append palette.gif
然后使用:
magick INPUT.JPG +dither -remap palette.gif ...
您可以在 ImageMagick 的单次调用中获得直方图,而不是使用像这样的命令两次:
magick IMAGE.JPG +dither -remap PALETTE.PNG -format %c histogram:info
示例输出
307574: (0,0,0) #000000 black
11896: (170,0,0) #AA0000 srgb(170,0,0)
您可以像这样解析 ImageMagick 直方图输出:
#!/usr/bin/env python3
import re, subprocess
# Empty list of colours
colours = []
# Get histogram from IM
res = subprocess.run(["magick","QcDsS.jpg","-remap", "palette.gif", "-format","%c","histogram:info:"], capture_output=True)
# Process IM result
for line in res.stdout.decode('utf-8').splitlines():
# Find count and hex colour with regex
match = re.match(r'\s*(\d+)[^#]*(\S+).*',line)
count = int(match.groups()[0])
hex = match.groups()[1]
print(count,hex)
colours.append((count,hex))
s = sorted(colours,reverse=True)
print('Sorted\n',s)
示例输出
297391 #000000
2389 #555555
6898 #AA0000
12 #AA00AA
30 #AA5500
480 #FF5555
Sorted
[(297391, '#000000'), (6898, '#AA0000'), (2389, '#555555'), (480, '#FF5555'), (30, '#AA5500'), (12, '#AA00AA')]
我已经将一些代码放在一起,它们将通过一个文件夹,打开所有具有特定结尾的图像,并使用 ImageMagick 创建它们的直方图。我不能做的(也许这是一个概念化问题,因为我对此还很陌生)是弄清楚如何将其记录到电子表格中,最好附上文件名。 PyXl 似乎可以与 Pandas 和 Numpy 一起使用,但我无法弄清楚获取此输出并记录它的路径。
是否有获取直方图输出并将其记录在电子表格中的解决方案?
编辑:到目前为止添加我的代码。在 Windows 10 Pro 中运行,使用 VSCode.
#import necessary packages
import os
import wand
import OpenPyXl
#define color library
magick xc:AliceBlue xc:AntiqueWhite xc:aquamarine xc:brown xc:azure xc:beige xc:bisque xc:black xc:BlanchedAlmond xc:blue xc:BlueViolet xc:chocolate4 xc:firebrick4 xc:burlywood xc:cadet blue xc:chartreuse xc:chocolate xc:tan4 xc:coral xc:CornflowerBlue xc:cornsilk xc:crimson xc:blue4 xc:cyan4 xc:DarkGoldenrod xc:DarkGreen xc:DarkGray xc:DarkKhaki xc:DarkMagenta xc:DarkOliveGreen xc:DarkOrange xc:DarkOrchid xc:DarkRed xc:DarkSalmon xc:DarkSeaGreen xc:DarkSlateBlue xc:DarkSlateGray xc:DarkTurquoise xc:DarkViolet xc:DeepPink xc:DeepSkyBlue xc:DimGray xc:DodgerBlue xc:firebrick xc:FloralWhite xc:ForestGreen xc:fuchsia xc:gainsboro xc:GhostWhite xc:gold xc:goldenrod xc:fractal xc:green xc:GreenYellow xc:honeydew xc:HotPink xc:IndianRed xc:indigo xc:ivory xc:khaki xc:lavender xc:LavenderBlush xc:LawnGreen xc:LemonChiffon xc:LightBlue xc:LightCoral xc:LightCyan xc:LightGoldenrodYellow xc:LightGray xc:LightGreen xc:LightPink xc:LightSalmon xc:LightSeaGreen xc:LightSkyBlue xc:LightSlateGray xc:LightSteelBlue xc:LightYellow xc:green1 xc:LimeGreen xc:linen xc:maroon xc:aquamarine3 xc:blue3 xc:MediumOrchid xc:MediumPurple xc:MediumSeaGreen xc:MediumSlateBlue xc:MediumSpringGreen xc:MediumTurquoise xc:MediumVioletRed xc:MidnightBlue xc:MintCream xc:MistyRose xc:moccasin xc:NavajoWhite xc:navy xc:OldLace xc:olive xc:OliveDrab xc:orange xc:OrangeRed xc:orchid xc:PaleGoldenrod xc:PaleGreen xc:PaleTurquoise xc:PaleVioletRed xc:PapayaWhip xc:PeachPuff xc:peru xc:pink xc:plum xc:PowderBlue xc:purple xc:red xc:RosyBrown xc:RoyalBlue xc:chocolate4 xc:salmon xc:SandyBrown xc:SeaGreen xc:seashell xc:sienna xc:silver xc:SkyBlue xc:SlateBlue xc:SlateGray xc:snow xc:SpringGreen xc:SteelBlue xc:tan xc:teal xc:thistle xc:tomato xc:turquoise xc:violet xc:wheat xc:gray100 xc:gray96 xc:yellow xc:OliveDrab3
#define photo folder
directory = 'C:\Users\tr9800a\Downloads\Bilder'
#find files ending in -2 and remap them with defined colors
for filename in os.listdir(directory):
if filename.endswith("-2.jpg"):
magick filename +dither -remap map.jpg result.jpg
magick identify -verbose result.jpg | grep -A9 Histogram
#write results to spreadsheet
(this is where I've gotten stuck)
continue
else:
我完全接受我可能犯了一些重大错误。再一次,菜鸟在这里。
大约有13000张照片要解决,希望能从每张图片中捕捉到四个最常见的颜色值。我使用 Magick 重新映射作为一种快捷方式,将颜色的细微变化转换为我们系统中拥有的 140 种核心颜色之一。
最理想的输出是:
图像 ID、最常见的十六进制代码、像素计数、第二常见的十六进制代码、像素计数、第三十六进制代码、像素计数、第四十六进制代码、像素计数
经过反思,我想我可能会用 PIL、wand 或 OpenCV 而不是解析 ImageMagick 的输出,这有点丑陋且容易出错。我还没有找到完整的答案,但这些想法可能会让你入门:
我建议您使用无损 PNG 或 GIF 格式,而不是使用有损 JPEG 作为调色板。您可以使用如下命令制作(小)调色板文件以进行重新映射:
magick xc:AliceBlue xc:AntiqueWhite xc:aquamarine ... +append palette.gif
然后使用:
magick INPUT.JPG +dither -remap palette.gif ...
您可以在 ImageMagick 的单次调用中获得直方图,而不是使用像这样的命令两次:
magick IMAGE.JPG +dither -remap PALETTE.PNG -format %c histogram:info
示例输出
307574: (0,0,0) #000000 black
11896: (170,0,0) #AA0000 srgb(170,0,0)
您可以像这样解析 ImageMagick 直方图输出:
#!/usr/bin/env python3
import re, subprocess
# Empty list of colours
colours = []
# Get histogram from IM
res = subprocess.run(["magick","QcDsS.jpg","-remap", "palette.gif", "-format","%c","histogram:info:"], capture_output=True)
# Process IM result
for line in res.stdout.decode('utf-8').splitlines():
# Find count and hex colour with regex
match = re.match(r'\s*(\d+)[^#]*(\S+).*',line)
count = int(match.groups()[0])
hex = match.groups()[1]
print(count,hex)
colours.append((count,hex))
s = sorted(colours,reverse=True)
print('Sorted\n',s)
示例输出
297391 #000000
2389 #555555
6898 #AA0000
12 #AA00AA
30 #AA5500
480 #FF5555
Sorted
[(297391, '#000000'), (6898, '#AA0000'), (2389, '#555555'), (480, '#FF5555'), (30, '#AA5500'), (12, '#AA00AA')]