【发布时间】:2016-01-02 23:09:17
【问题描述】:
我有一个场景,我针对特定图像生成调色板。然后我想单击任何调色板颜色 n 并选择一种新颜色,以将其放在具有先前颜色代码的所有像素上。我已经完成了,它正在运行,但我需要您帮助优化其结果和时间。使用color thief生成调色板的代码是:
function make_pallete(){
var images = document.getElementById('default_img');
colors = colorThief.getPalette(images, 8);
for (var i = 0 ; i < colors.length; i++) {
$('#swatches').append("<div class='swatch' style='background-color:rgb("+ colors[i][0]+ ","+ colors[i][1] + "," + colors[i][2] +")'></div>");
}
}
我正在执行重新着色的 Django 视图是:
def recolor_image(request):
if request.method == "POST":
pic = request.POST.get("image")
old_rgb = request.POST.getlist("old_rgb[]")
new_rgb = request.POST.getlist("new_rgb[]")
path = "D:/pysofts/flagcarpets" + pic
pic = path
picture = Image.open(pic)
# Get the size of the image
width,height = picture.size
print old_rgb
for x in range(0, width):
for y in range(0, height):
current_color = picture.getpixel((x,y))
r,g,b = current_color
if r in range(int(old_rgb[0]) - 50, int(old_rgb[0]) + 50) and g in range(int(old_rgb[1]) - 50, int(old_rgb[1]) + 50) and b in range(int(old_rgb[2]) - 50, int(old_rgb[2]) + 50):
r = int(new_rgb[0])
g = int(new_rgb[1])
b = int(new_rgb[2])
new_color = (r,g,b)
picture.putpixel((x,y), new_color)
picture.save(path)
return HttpResponse(pic)
正如我所说,它正在为图像重新着色,但效率很差(它不会为每个先前的像素添加新颜色)n 重新着色图像大约需要 30 秒。更精确的算法是什么?
【问题讨论】:
标签: python django algorithm image-processing