ImageMagick:不同文件格式的处理速度



我正在使用ImageMagick(版本:ImageMagick 6.9.7-4 Q16 x86_64 20170114(来创建一种特殊类型的合成叠加图像。使用Python作为控制结构,我实际上运行了一系列shell命令(convertcomposite(来执行以下操作:

create an initial (transparent) working image W
for n = 1 to numberOfOverlappingShapesWanted
use convert to create a transparent overlay image (O) with a random shape at a random place
use composite to merge down W (on top) with O (beneath) and produce WO
replace W with WO
use convert to change W into any desired final image format

目前,我以PNG格式创建W、O和WO。我的问题只涉及速度。。。使用我所拥有的非常粗糙的结构,是否有一种图像格式的选择会导致该过程比我目前选择的PNG格式运行得更快。

根据请求编辑以获取真实示例

#!/usr/bin/python3
import os
import random
# The overall algorithm here is to create a blank working image
# called w.png and then to drop a disc onto a new blank image
# calle l.png . The old working image is then combined with
# the newly dropped disc into an image called wl.png .
# And wl.png then becomes the new working image
# Python is used only for the control structure. The real
# work is done by ImageMagick from Python os.system commands. 
nDroppingDiscs = 6
discRadius = 64
imageX = 2048
imageY = 2048
# Names for the three images
workingImage = 'w.png'
discImage = 'o.png'
combinedImage = 'wo.png'
# Create the first part of the ImageMagick 'convert' command
# of the form:
#   convert -size 2048x2048 xc:transparent
baseWorkingCommand = 'convert -size ' + 
str( imageX ) + 'x' + str( imageY ) + ' ' + 
'xc:transparent '
# Create initial blank working image
#     convert -size 2048x2048 xc:transparent w.png
os.system( baseWorkingCommand + workingImage )
for n in range( nDroppingDiscs ) :
# Create the initial portion of the ImageMagick 'convert'
# command for dropping a disc onto a transparent canvas
baseDiscCommand = 'convert -size ' + 
str( imageX ) + 'x' + str( imageY ) + ' ' + 
'xc:transparent +antialias -fill '
# Ensure that each disc is a different colour
discNAsColourString = "#%06x" % n
baseDiscCommand = baseDiscCommand + " '" + 
discNAsColourString + "' " 
# Determine the drop-point for the disc
discDropPointX =  random.randint(1, imageX)
discDropPointY =  random.randint(1, imageY) 
discRadiusIndicatorX = discDropPointX
discRadiusIndicatorY = discDropPointY + discRadius
# Put the pieces of the 'convert' command together
baseDiscCommand = baseDiscCommand + 
" -draw 'circle " + 
str( discDropPointX ) + "," + 
str( discDropPointY ) + " " + 
str( discRadiusIndicatorX ) + "," + 
str( discRadiusIndicatorY ) + "'"
# Use ImageMagick to create the new randomly dropped disc
os.system( baseDiscCommand + " "  + discImage )
# Overlay the existing image onto the newly created dropped disc
# to produce a combined image
os.system('composite ' + workingImage + " " + discImage + " " + combinedImage )
# The combined image is now the new working image
os.system('mv ' + combinedImage  + ' ' + workingImage )
# Final conversion. Convert the working image from whatever format
# I was using earlier to a final PNG format.
os.system('convert ' +  workingImage + ' final.png')

注意,常数CCD_ 3通常大约为4000。此外,值得补充的是,我的最终目的是探索关于这类模式的图像统计的一些事情。

对于这样的迭代过程来说,通过文件系统将非常缓慢。我想你可能会更好的与numpy这样的东西。

我很快就爱上了蟒蛇,因为我很清楚:

#!/usr/bin/python3
import random
import pyvips
n_dropping_discs = 1000
disc_radius = 64
image_width = 2048
image_height = 2048
image = pyvips.Image.black(image_width, image_height, bands=4) 
.copy(interpretation="srgb") 
for i in range(n_dropping_discs):
ink = [(i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff, 0xff]
x = random.randint(0, image_width) - disc_radius
y = random.randint(0, image_height) - disc_radius
image = image.draw_circle(ink, x, y, disc_radius, fill=True)
image.write_to_file("final.png")

在这台笔记本电脑上,我看到:

$ time ./disc2.py 
real    0m6.086s
user    0m12.182s
sys 0m1.656s

因此,大约6秒内可播放1000张光盘。pyvips不太适合这种任务——numpy会更快。

我花了一点时间尝试了John的建议,并将他的代码重新转换为NumpyOpenCV,如下所示。它似乎比我的机器上的蟒蛇快40倍左右:

#!/usr/bin/env python3
import random
import cv2
n_dropping_discs = 1000
disc_radius = 64
image_width = 2048
image_height = 2048
def numpyidea():
image = np.zeros((image_height,image_width,4), dtype=np.uint8)
for i in range(n_dropping_discs):
ink = [i & 0xff, (i >> 8) & 0xff, (i >> 16) & 0xff, 0xff]
x = random.randint(0, image_width) - disc_radius
y = random.randint(0, image_height) - disc_radius
cv2.circle(image, (x,y), disc_radius, ink, cv2.FILLED)
cv2.imwrite("numpy.png", image)
return

最新更新