def insertColorNameEn(request): filename = '%s/tools/scrape/color_names_en.json' % BASE_DIR f = open(filename) contents = f.read() json = simplejson.loads(contents) for row in json: name = row['name'] try: rgb = hex_to_rgb(row['hex']) try: color_name = ColorNameEn.objects.get(name=name, red=rgb[0], green=rgb[1], blue=rgb[2]) except: color_name = ColorNameEn() color_name.name = name color_name.red = rgb[0] color_name.green = rgb[1] color_name.blue = rgb[2] color_name.save() except: pass return HttpResponse('done.')
def find(request, color_id): rgb = hex_to_rgb('#%s' % color_id) try: image_list = ImageRGB.objects.select_related() image_list = image_list.extra( select = { 'difference': 'POW((%d-red),2) + POW((%d-green),2) + POW((%d-blue),2)' % rgb }, order_by = {'difference'} )[:1000] except: raise Http404 image_result = [] ignore_id = [] for image_rgb in image_list: if image_rgb.upload_file_id not in ignore_id and image_rgb.difference < 2000: image_result.append(UploadFiles.objects.get(pk=image_rgb.upload_file_id)) ignore_id.append(image_rgb.upload_file_id) params = {'image_list': image_result} ctxt = RequestContext(request, params) t = get_template('list.html') html = t.render(ctxt) return HttpResponse(html)
def findColorName(request, color_id): try: rgb = hex_to_rgb('#%s' % color_id) except: raise Http404 name_list_ja = None try: name_list_ja = ColorNameJa.objects.select_related() name_list_ja = name_list_ja.extra( select = { 'difference': 'POW((%d-red),2) + POW((%d-green),2) + POW((%d-blue),2)' % rgb }, order_by = {'difference'} )[:1000] except: pass name_list_en = None try: name_list_en = ColorNameEn.objects.select_related() name_list_en = name_list_en.extra( select = { 'difference': 'POW((%d-red),2) + POW((%d-green),2) + POW((%d-blue),2)' % rgb }, order_by = {'difference'} )[:1000] except: pass name_result = [] for row in name_list_ja: if row.difference < 2000: name_result.append({ 'name': '%s(%s)' % (row.name, row.name_yomi), 'hex': rgb_to_hex((row.red, row.green, row.blue)), 'lang': 'ja_JP' }) for row in name_list_en: if row.difference < 2000: name_result.append({ 'name': row.name, 'hex': rgb_to_hex((row.red, row.green, row.blue)), 'lang': 'en_US' }) return HttpResponse(simplejson.dumps(name_result))