def list(request): # Handle file upload if request.method == 'POST': form = ImageForm(request.POST, request.FILES) if form.is_valid(): for img in request.FILES.getlist('name'): from django.utils import timezone newdoc = Image(name = img, pub_date = timezone.now()) newdoc.save() # Redirect to the document list after POST return HttpResponseRedirect(reverse('image.views.list')) else: form = ImageForm() # A empty, unbound form # Load documents for the list page documents_list = Image.objects.all() # Pagination from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger limit = 16 # limit photos each page paginator = Paginator(documents_list, limit) # Render list page with the documents and the form return render_to_response( 'image/list.html', {'paginator': paginator}, context_instance=RequestContext(request) )
def post(self, request): data = json.loads(request.body) try: images = dict() for index, image in enumerate(data['images']): images[index] = image image = Image( image_1 = images.get(0), image_2 = images.get(1), image_3 = images.get(2), image_4 = images.get(3), image_5 = images.get(4) ) image.save() product = Product( seller = request.user, title = data['title'], content = data['content'], price = data['price'], places = data['places'], image = image, category = self.categories.get(id = data['category']) ) product.save() return JsonResponse({'product_id':product.id}, status = 201) except KeyError: return JsonResponse({'message':'INVALID_KEYS'}, status = 400)
def add_image(self, uploadedfile, description, product): u''' add new image ''' img = Image(product=product, description=description) img.image = uploadedfile img.save()
def step_07_fourth_clustering_by_geography_distance(self): for threshold in xrange(1, CLUSTERING_MIN_DISTANCE_THRESHOLD + 1): #print('threshold = %d' % threshold) prev_group_cnt = 0 for iteration in xrange(10): group_cnt = 0 for group1 in self.result: img = Image() img.lonLat = group1.lonLat m_value = Group() m_value.members = [Group()] m_value.members[0].members = [img] cluster = Clustering(group1, threshold, distance_geography_group, m_value, False) cluster.run() group1.members = [ Group( sum([group3.members for group3 in group2.members], [])) for group2 in cluster.result ] group_cnt += len(group1.members) #print('step_07:group_cnt == %d' % group_cnt) if prev_group_cnt == group_cnt: break else: prev_group_cnt = group_cnt return True
def add(request): # Check login if (request.user.is_authenticated() == False): return HttpResponseRedirect(reverse('image.views.index')) # Handle file upload responseData = [] response_data = {} response_data['result'] = 'failed' response_data['files'] = 'abc' for img in request.FILES.getlist('files[]'): # import pdb; pdb.set_trace(); # Check file is image or not file_type = img.content_type.split('/')[0] if file_type != 'image': messages.error(request, "Your file is not image.") HttpResponseRedirect(reverse('image.views.list')) # Insert photos newdoc = Image(name = img, pub_date = timezone.now(), user_id = request.user.id) newdoc.save() responseData.append(img) # Add to couple story from django.db.models import Q couple = Couple.objects.get(Q(male=request.user.id) | Q(female=request.user.id)) content = "Added " + str(len(request.FILES.getlist('files[]'))) + " Photos" story = Story(couple_id=couple.id, content=content, pub_date=timezone.now()) story.save() return HttpResponse(json.dumps(response_data), content_type="application/json")
def test_json(self): test_data = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' img, is_created = Image.get_or_create_smart(test_data) img.summarize() self.assertIn('uuid', img.json) self.assertIn('content', img.json) self.assertNotIn('note', img.json) self.assertNotIn('timestamp', img.json) self.assertIn('summary', img.json) img.timestamp = get_timestamp() inote = ImageNote(content='img note') inote.save() img.note = inote self.assertIn('uuid', img.json) self.assertIn('content', img.json) self.assertIn('note', img.json) #self.assertIn('timestamp', img.json) self.assertIn('summary', img.json) self.assertIn('uuid', img.json['note']) self.assertIn('content', img.json['note']) self.assertNotIn('timestamp', img.json['note']) inote.timestamp = get_timestamp() self.assertIn('uuid', img.json['note']) self.assertIn('content', img.json['note']) self.assertIn('timestamp', img.json['note']) saved = Image.get_from_json(img.json) self.assertEqual(saved, img) self.assertEqual(saved.note, img.note)
def upload(request): if request.method == 'GET': return render_to_response('upload.html', {'url': request.GET.get('url', ''),}, context_instance=RequestContext(request)) elif request.method == 'POST': tmp = tempfile.mkstemp() md5 = hashlib.md5() fext = "" orig = "" if request.POST['upload_type'] == 'file': file = request.FILES['upload_file'] fext = file.name[-3:] orig = file.name f = os.fdopen(tmp[0], "wb+") for chunk in file.chunks(): f.write(chunk) md5.update(chunk) f.close() elif request.POST['upload_type'] == 'url': remote_image = urllib2.urlopen(request.POST['upload_url']) data = remote_image.read() md5.update(data) fext = request.POST['upload_url'][-3:] orig = request.POST['upload_url'] f = os.fdopen(tmp[0], "wb+") f.write(data) f.close() img = Image() try: next_id = Image.objects.order_by('-id')[0].id + 1 except IndexError: next_id = settings.IMAGE_ID_OFFSET + 1 img.base62 = base62(next_id) img.filename = base62(next_id) + "." + fext.lower() img.orig_filename = orig img.type = '' # todo img.description = '' # not implemented yet. img.uploader = request.user img.md5sum = md5.hexdigest() image_file = os.path.join(settings.MEDIA_ROOT,img.filename) thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename) try: img.save() except IntegrityError: os.unlink(tmp[1]) # delete the uploaded file if it already exists return HttpResponseRedirect( settings.MEDIA_URL + Image.objects.get(md5sum=img.md5sum).filename) shutil.move(tmp[1], image_file) os.system("/usr/bin/convert %s -thumbnail 150x150 %s" % (image_file, thumbnail)) return HttpResponseRedirect(settings.MEDIA_URL + img.filename)
def save_all_image(slef, form_set, product): for item in form_set: image = item.cleaned_data.get('image', None) description = item.cleaned_data.get('img_description', None) if image is None or description is None: continue img_obj = Image(product=product, image=image, description=description) img_obj.save()
def save_images(request): try: for image in request.FILES.values(): im = Image(image=image) im.save() return HttpResponse() except ValidationError as e: return HttpResponse(content=e, status=500) except ValueError as e: return HttpResponse(content=e, status=415)
def setUp(self): super(ImageViewsetTest, self).setUp() response = self.client.post('/users/register/') self.auth_user_token = json_loads(response.content)['auth_user_token'] self.client.post('/users/login/', {'auth_user_token': self.auth_user_token}) response = self.client.post('/vds/register/', dict(email='*****@*****.**')) self.auth_vd_token = json_loads(response.content)['auth_vd_token'] self.client.post('/vds/login/', {'auth_vd_token': self.auth_vd_token}) self.img = Image() self.img.content = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' self.img.save() self.content2 = 'http://blogpfthumb.phinf.naver.net/20100110_16/mardukas_1263055491560_VI01Ic_JPG/DSCN1968.JPG'
def handle(self, *args, **options): # search url, credit_url = search('cat') # wget filename = '/dev/shm/' + hashlib.md5(str( random.random()).encode()).hexdigest() + '.gif' try: subprocess.getoutput('wget %s -q --timeout 20 -t 2 -O %s' % (url, filename)) except: self.stderr.write( self.style.ERROR('Fail to download. URL: %s' % url)) sys.exit() # thumbnail thumbnail_filename = convert(filename) if thumbnail_filename != -1: pass else: self.stderr.write(self.style.ERROR('Fail to convert.')) sys.exit() # upload2weibo original_url = upload(filename) if original_url != -1: pass else: self.stderr.write(self.style.ERROR('Fail to upload gif.')) sys.exit() thumbnail_url = upload(thumbnail_filename) if thumbnail_url != -1: pass else: self.stderr.write(self.style.ERROR('Fail to upload jpg.')) sys.exit() new_cat = Image(name='CatHub', original_url=original_url, thumbnail_url=thumbnail_url, pub_date=timezone.now(), oo_num=0, xx_num=0, comment_num=0, legal=True, credit_url=credit_url) new_cat.save() subprocess.getoutput('rm ' + filename) subprocess.getoutput('rm ' + thumbnail_filename) self.stdout.write(self.style.SUCCESS('Successfully add a cat.'))
def dump_similars(self): rfs1 = RawFile.objects.filter(vd=self.source).filter( same=None).exclude(mhash=None) similars = Image.objects.filter(rf__in=rfs1).exclude( similar=None).order_by('content') print('similars: %d' % len(similars)) for img in similars: d_p = Image.hamming_distance(img.phash, img.similar.phash) d_d = Image.hamming_distance(img.dhash, img.similar.dhash) dist = img.pd_hamming_distance(img.similar) if dist == IMG_PD_HDIST_THREASHOLD - 1: print('(%02d, %02d, %02d) : %s == %s' % (dist, d_p, d_d, img.content, img.similar.content))
def test_dhash(self): rf = RawFile() rf.file = self.uploadFile('test.jpg') rf.save() rf2 = RawFile() rf2.file = self.uploadFile('test_480.jpg') rf2.save() img, is_created = Image.get_or_create_smart(rf.url) img2, is_created = Image.get_or_create_smart(rf2.url) self.assertNotEqual(img.dhash, None) self.assertNotEqual(img2.dhash, None) self.assertEqual(img.dhash, img2.dhash)
def create(self, validated_data): # create an image object image = Image(**validated_data) if validated_data.get("base_64", None): file_type = get_file_type(validated_data["base_64"]) if not file_type: raise serializers.ValidationError( {"error": "Image cannot be decoded"}) else: image.extension = file_type if validated_data.get("remote_location", None): image_check = valid_remote_image(validated_data["remote_location"]) if not image_check: raise serializers.ValidationError( {"error": "Image cannot be downloaded"}) else: success, file_type, base_64 = image_check image.base_64 = base_64 image.extension = file_type validated_data["base_64"] = image.base_64 validated_data["extension"] = image.extension image.save() # return the correct uuid for the API response validated_data["id"] = image.id f_data = ContentFile(base64.b64decode(image.base_64)) image.raw_file.save(f"file_name.{image.extension}", f_data) validated_data["raw_file"] = image.raw_file convert.delay(image.id) return Image(**validated_data)
def download_blog_images(group_id, group_key, blog_ct, writer_ct): """ 非同期でブログ画像をダウンロード、旧imgScraper.update() :param group_id: :param group_key: :param blog_ct: :param writer_ct: :return: """ try: blog = Blog.objects.get(publishing_group__group_id=group_id, blog_ct=blog_ct) img_urls = otapick.BlogImageCrawler().crawl(group_key=group_key, blog_ct=blog_ct) # crawl error if img_urls is None: pass # image not found elif len(img_urls) == 0: pass else: # clear halfway remaining images Image.objects.filter(publisher=blog).delete() order = 0 for i, img_url in enumerate(img_urls): media = otapick.BlogImageDownloader().download( img_url, group_id, blog_ct, writer_ct) if media == 'not_image': # exclude gif pass elif media is not None: if not Image.objects.filter(order=i, publisher=blog).exists(): image = Image( order=order, picture=media, publisher=blog, ) image.save() otapick.compress_blog_image(image) order += 1 # image download failed else: import traceback traceback.print_exc() # timeout(60s)後の処理 except SoftTimeLimitExceeded: pass
def create(self, request, *args, **kwargs): if 'content' not in request.data or not request.data['content']: return Response(status=status.HTTP_400_BAD_REQUEST) img, is_created = Image.get_or_create_smart(request.data['content']) dirty = False if 'lon' in request.data and request.data[ 'lon'] and 'lat' in request.data and request.data['lat']: lon = float(request.data['lon']) lat = float(request.data['lat']) point = GEOSGeometry('POINT(%f %f)' % (lon, lat), srid=4326) img.lonLat = point dirty = True if 'local_datetime' in request.data and request.data['local_datetime']: dt = datetime.strptime(request.data['local_datetime'], '%Y:%m:%d %H:%M:%S') # TODO : VD.timezone 을 참조하여 변환 d = Delorean(dt, timezone='Asia/Seoul') img.timestamp = int(round(d.epoch * 1000)) dirty = True if dirty: img.save() serializer = self.get_serializer(img) return Response(serializer.data, status=status.HTTP_201_CREATED)
def test_image_cache(self): self.assertEqual(RawFile.objects.count(), 0) self.assertEqual(Image.objects.count(), 0) rf = RawFile() rf.file = self.uploadFile('test.JPEG') self.assertEqual(rf.ext, 'jpg') rf.save() self.assertEqual(rf.ext, 'jpg') self.assertEqual(RawFile.objects.count(), 1) self.assertEqual(Image.objects.count(), 0) img, is_created = Image.get_or_create_smart(rf.url) self.assertValidLocalFile(img.path_accessed) self.assertValidInternetUrl(img.url_accessed) f = Path(img.path_accessed) self.assertEqual(f.is_symlink(), True) self.assertEqual(Image.objects.count(), 1) img.save() self.assertEqual(Image.objects.count(), 1) self.assertValidInternetUrl(img.url_accessed) self.assertValidInternetUrl(img.url_summarized) self.assertEqual(img, Image.objects.first()) self.assertEqual(rf.img, img) self.assertEqual(img.rf, rf) img = Image.objects.first() rf = RawFile.objects.first() self.assertEqual(rf.img, img) self.assertEqual(img.rf, rf)
def paste(request, uplace_id): vd = vd_login_for_browser(request) uplace = UserPlace.objects.get(vd_id__in=vd.realOwner_vd_ids, id=uplace_id) if request.method == 'POST': pb = PostBase() pb.uuid = uplace.uuid if 'note' in request.POST and request.POST['note']: note, is_created = PlaceNote.get_or_create_smart( request.POST['note']) pb.notes.append(note) if request.FILES: for file in request.FILES.itervalues(): rf = RawFile.objects.create(vd=vd, file=file) rf.start() img, is_created = Image.get_or_create_smart(rf.url) pb.images.append(img) if 'lp' in request.POST and request.POST['lp']: lp, is_created = LegacyPlace.get_or_create_smart( request.POST['lp']) pb.lps.append(lp) pb_MAMMA = pb.pb_MAMMA if pb_MAMMA: place, is_created = Place.get_or_create_smart(pb_MAMMA, vd) uplace.placed(place) if pb.ujson: pp = PostPiece.create_smart(uplace, pb) if uplace.is_empty: uplace.is_empty = False uplace.save() uplace._clearCache() return render(request, 'ui/paste.html', context=dict(uplace=uplace))
def get_image_w_h(image): """ imageのwidth・heightを取得しdictで返す :return: {'width': 100, 'height': 200} """ pil_image = Image.open(image.picture.path) return pil_image.size
def test_transpose(self): rf = RawFile() rf.file = self.uploadFile('test_transpose.jpg') rf.save() img, is_created = Image.get_or_create_smart(rf.url) timestamp = img.timestamp lonLat = img.lonLat self.assertNotEqual(lonLat, None) self.assertNotEqual(timestamp, None)
def uploadImage(request): global is_error, myfile is_error = 0 if request.user.is_authenticated(): if request.user.is_superuser: if request.POST: libelle = request.POST["libelle"] type = request.POST["type"] description = request.POST["descript"] save_plus = request.POST.getlist('save_and') if libelle == "": error_libelle = "veuillez remplir ce champs" is_error = 1 if int(type) == 0: error_type = "veuillez selectionner un type" is_error = 1 try: myfile = request.FILES['image'] except: error_logo = "veuillez selectionner une image" is_error = 1 if is_error != 0: return render(request, 'uploadImage.html', locals()) else: save_path = settings.MEDIA_ROOT last = myfile.name[myfile.name.rfind("."):len(myfile.name)] fs = FileSystemStorage() currentId = 0 try: image = Image.objects.last() currentId = image.idImage + 1 except: currentId = 1 save_name = "teranga-food-upload-0" + str(currentId) + last fs.save(settings.MEDIA_ROOT + save_name, myfile) myImage = Image() myImage.description = description myImage.libelle = libelle myImage.type = int(type) myImage.saveName = "/media/" + save_name myImage.save() try: _next = int(save_plus[0]) except: _next = 0 if _next == 0: return redirect(listeImage) else: libelle = "" description = "" return render(request, 'uploadImage.html', locals()) else: return render(request, 'uploadImage.html', locals())
def test_create3(self): with open('image/samples/gps_test.jpg') as f: response = self.client.post('/rfs/', dict(file=f)) img_url = json_loads(response.content)['url'] response = self.client.post('/imgs/', dict(content=img_url, test=True)) self.assertEqual(response.status_code, status.HTTP_201_CREATED) uuid = json_loads(response.content)['uuid'] img = Image.get_from_json('{"uuid": "%s"}' % uuid) self.assertEqual(img.lonLat, GEOSGeometry('POINT(%f %f)' % (127.103744, 37.399731), srid=4326)) self.assertEqual(img.timestamp, 1459149534000)
def test_create4(self): with open('image/samples/gps_test.jpg') as f: response = self.client.post('/rfs/', dict(file=f)) img_url = json_loads(response.content)['url'] response = self.client.post('/imgs/', dict(content=img_url, lon=127.0, lat=37.0, local_datetime='2015:04:22 11:54:19', test=True)) self.assertEqual(response.status_code, status.HTTP_201_CREATED) uuid = json_loads(response.content)['uuid'] img = Image.get_from_json('{"uuid": "%s"}' % uuid) self.assertEqual(img.lonLat, GEOSGeometry('POINT(%f %f)' % (127.0, 37.0), srid=4326)) self.assertEqual(img.timestamp, 1429671259000)
def save_social_profile_image(user, img_url = None, platform='facebook'): if platform == 'facebook': img_url = "http://graph.facebook.com/{id}/picture?width=9999".format(id=user.facebook_id) elif platform == 'twitter': img_url = img_url.replace('_normal', '') try: img = urlopen(img_url) suffix = '_fb' except HTTPError: pass if img: imgFormat = 'png' if img.headers['content-type'] == 'image/png' else 'jpg' img_obj = Image(user=user) img_obj.image.save(slugify(user.username + '_'+ platform) + '.' + imgFormat, ContentFile(img.read())) img_obj.save() user.image = img_obj user.save()
def test_task(self): _rf = RawFile() _rf.file = self.uploadFile('no_exif_test.jpg') _rf.save() img, is_created = Image.get_or_create_smart(_rf.url) self.assertEqual(Image.objects.count(), 1) saved = Image.objects.first() img.task() self.assertEqual(img.similar, None) saved.task() self.assertEqual(saved.phash, img.phash) self.assertEqual(saved.dhash, img.dhash) self.assertEqual(saved.similar, img.similar) _rf2 = RawFile() _rf2.file = self.uploadFile('test.jpg') _rf2.save() img2, is_created = Image.get_or_create_smart(_rf2.url) img2.task() self.assertEqual(img.similar, None) self.assertEqual(img2.similar, None) _rf3 = RawFile() _rf3.file = self.uploadFile('test_480.jpg') _rf3.save() img3, is_created = Image.get_or_create_smart(_rf3.url) img3.lonLat = None img3.timestamp = None #img3.save() self.assertEqual(img3.lonLat, None) self.assertEqual(img3.timestamp, None) img3.task() self.assertEqual(img.similar, None) self.assertEqual(img2.similar, None) self.assertEqual(img3.similar, img2) self.assertEqual(img2.similars.count(), 1) self.assertEqual(img2.similars.first(), img3) self.assertEqual(img3.lonLat, img2.lonLat) self.assertEqual(img3.timestamp, img2.timestamp)
def test_img_task(self): test_data = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' img, is_created = Image.get_or_create_smart(test_data) img = Image.objects.get(id=img.id) self.assertEqual(img.dhash, 11973959L) self.assertEqual(img.phash, UUID('0017c0a8-a6ed-a0c5-1230-8ef6eb5176fe')) self.assertNotEqual(img.azure, None) if not DISABLE_NO_FREE_API: self.assertEqual(img.azure.is_success_analyze, True) self.assertEqual(len(img.ctags.tags), 8)
def test_save_and_retreive(self): img = Image() test_data = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' img.content = test_data img.save() saved = Image.objects.first() self.assertEqual(saved, img) saved2 = Image.get_from_json('{"uuid": "%s", "content": null}' % img.uuid) self.assertEqual(saved2, img) saved3 = Image.get_from_json('{"uuid": "%s", "content": null, "note": {"uuid": null, "content": null}}' % img.uuid) self.assertEqual(saved3, img) saved4 = Image.get_from_json('{"uuid": null, "content": "%s"}' % img.content) self.assertEqual(saved4, img)
def handle(self, *args, **options): filename = options['filename&name'][0] name = options['filename&name'][1] # generate thumbnail thumbnail_filename = convert(filename) if thumbnail_filename != -1: pass else: self.stderr.write(self.style.ERROR('Fail to convert.')) raise CommandError('无法解析该图片') # upload2weibo original_url = upload(filename) if original_url != -1: pass else: self.stderr.write(self.style.ERROR('Fail to upload gif.')) raise CommandError('无法上传原图到微博图床') thumbnail_url = upload(thumbnail_filename) if thumbnail_url != -1: pass else: self.stderr.write(self.style.ERROR('Fail to upload jpg.')) raise CommandError('无法上传缩略图到微博图床') new_cat = Image(name=name, original_url=original_url, thumbnail_url=thumbnail_url, pub_date=timezone.now(), oo_num=0, xx_num=0, comment_num=0) new_cat.save() subprocess.getoutput('rm ' + filename) subprocess.getoutput('rm ' + thumbnail_filename) self.stdout.write(self.style.SUCCESS('.%s.' % new_cat.id))
def save_avatar(backend, user, response, details, is_new=False,*args,**kwargs): if is_new or not user.image: img = None # FACEBOOK if backend.name == 'facebook': try: img_url = "http://graph.facebook.com/{id}/picture?type=large".format(id=response["id"]) img = urlopen(img_url) suffix = '_fb' except HTTPError: pass # TWITTER if backend.name == 'twitter': try: img = urlopen(response['profile_image_url'].replace('_normal', '')) suffix = '_tw' except HTTPError: pass # GOOGLE if backend.name == 'google-oauth2': try: img = urlopen(response['picture']) suffix = '_g' except HTTPError: pass if img: format = 'png' if img.headers['content-type'] == 'image/png' else 'jpg' img_obj = Image(user=user) img_obj.image.save(slugify(user.username + suffix) + '.' + format, ContentFile(img.read())) img_obj.save() user.image = img_obj user.save()
def test_exif_timestamp(self): exif = exif_lib.get_exif_data(PIL_Image.open('image/samples/gps_test.jpg')) timestamp = exif_lib.get_timestamp(exif) self.assertEqual(timestamp, 1459149534000) rf = RawFile() rf.file = self.uploadFile('gps_test.jpg') rf.save() img, is_created = Image.get_or_create_smart(rf.url) saved = Image.objects.first() self.assertEqual(img.timestamp, timestamp) self.assertEqual(saved.timestamp, timestamp)
def test_dhash_hamming_dist(self): id_640 = Image.compute_dhash(PIL_Image.open('image/samples/test.jpg')) id_256 = Image.compute_dhash(PIL_Image.open('image/samples/test_256.jpg')) id_480 = Image.compute_dhash(PIL_Image.open('image/samples/test_480.jpg')) #id_1200 = Image.compute_dhash(PIL_Image.open('image/samples/test_1200.jpg')) #id_org = Image.compute_dhash(PIL_Image.open('image/samples/test_org.jpg')) id2 = Image.compute_dhash(PIL_Image.open('image/samples/no_exif_test.jpg')) self.assertLessEqual(Image.hamming_distance(id_640, id_256), 0) self.assertLessEqual(Image.hamming_distance(id_640, id_480), 0) #self.assertLessEqual(Image.hamming_distance(id_640, id_1200), 0) #self.assertLessEqual(Image.hamming_distance(id_640, id_org), 0) self.assertGreater(Image.hamming_distance(id_640, id2), 3) # distance = 15
def create_images(): Image.objects.all().delete() for pk, fields in imagenes.iteritems(): im = Image(pk=pk) im.order = fields['order'] im.user_id = get_user(fields['user']) im.width = fields['width'] im.height = fields['height'] file_name = fields['image'].split('/')[-1] #result = urllib.urlretrieve('http://datea.pe/media/'+fields['image']) im.image.save(file_name, File(open('impdata/'+fields['image']))) im.client_domain = datea im.save()
def save_image(request): """ Save DateaImage instance for specified object FK or M2M-Field post parameters must include: image: image file input order: the order of the image (optional) """ key_auth = ApiKeyAuthentication() if not key_auth.is_authenticated(request): return HttpResponse("<h1>Login required</h1>") form = ImageUploadForm(request.POST, request.FILES) if form.is_valid(): image_data = request.FILES['image'] image_instance = Image(image=image_data, user=request.user) if 'order' in form.cleaned_data: image_instance.order = form.cleaned_data['order'] image_instance.save() # create image resource ir = ImageResource() im_bundle = ir.build_bundle(obj=image_instance) im_bundle = ir.full_dehydrate(im_bundle) data = {'ok': True, 'message':'ok', 'resource': im_bundle.data} status=201 else: data = {'ok': False, 'message': form.errors} status = 400 resp = JsonResponse(data) resp.status_code = status return resp
def load_additional_info(self): # 이미지가 없는 경우, URL 에서 이미지 가져오기 if not self.images and self.urls and self.urls[0]: url = self.urls[0] try: url.access() pq = PyQuery(url.content_accessed) img_url = pq('meta[property="og:image"]').attr('content') if img_url: img = Image.get_from_json('{"content": "%s"}' % img_url) if img: img.summarize() self.images.append(img) except Timeout: pass
def step_06_third_clustering_by_hamming_distance(self): from uuid import UUID def distance_hamming_group(g1, g2): distances = [ img1.pd_hamming_distance(img2) for img1 in g1.members for img2 in g2.members ] return min(distances) group_cnt = 0 for group1 in self.result: img = Image() img.phash = UUID(b'0' * 32) img.dhash = 0 m_value = Group() m_value.members = [img] cluster = Clustering(group1, IMG_PD_HDIST_THREASHOLD, distance_hamming_group, m_value, False) cluster.run() group1.members = cluster.result group_cnt += len(cluster.result) print('step_06:group_cnt == %d' % group_cnt) return True
def test_exif_gps(self): exif = exif_lib.get_exif_data(PIL_Image.open('image/samples/gps_test.jpg')) lonLat = exif_lib.get_lon_lat(exif) point = GEOSGeometry('POINT(%f %f)' % lonLat, srid=4326) self.assertEqual(point.x, 127.103744) # lon(경도) self.assertEqual(point.y, 37.399731) # lat(위도) rf = RawFile() rf.file = self.uploadFile('gps_test.jpg') rf.save() img, is_created = Image.get_or_create_smart(rf.url) saved = Image.objects.first() self.assertEqual(img.lonLat, point) self.assertEqual(saved.lonLat, point)
def index(request): if request.POST: image_form = ImageForm(request.POST, request.FILES) if image_form.is_valid(): image = Image() image.image = image_form.cleaned_data['image'] image.filename = image_form.cleaned_data['image'].name image.user_id = request.user.id image.save() else: image_form = ImageForm() images = Image.objects.filter(user_id=request.user.id) return render(request, 'image/index.html', {'image_form': image_form, 'images': images})
def test_analyze(self): if DISABLE_NO_FREE_API: return img_url = 'http://pds.joins.com/news/component/starnews/201607/14/2016071408355459431_1.jpg' img, is_created = Image.get_or_create_smart(img_url) azure, is_created = AzurePrediction.objects.get_or_create(img=img) r = azure.predict() #print(r) self.assertNotEqual(r, None) self.assertEqual(img.azure, azure) self.assertEqual(azure.is_success_analyze, True) self.assertNotEqual(azure.result_analyze, None) imgTags = ImageTags.objects.first() #imgTags.dump() self.assertEqual(imgTags.img, img) self.assertEqual(img.ctags, imgTags) self.assertNotEqual(imgTags, None) self.assertNotEqual(imgTags.tags, None) self.assertEqual(len(imgTags.tags), 11)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) try: shared_post = Post.objects.get(id=self.request.data['shared_post']) new_post = serializer.save(posted_by=self.request.user.profile, shared_post=shared_post) if request.FILES: for file in request.FILES.getlist('image'): new_file = Image(image=file, post=new_post) new_file.save() except MultiValueDictKeyError: new_post = serializer.save(posted_by=self.request.user.profile) if request.FILES: for file in request.FILES.getlist('image'): new_file = Image(image=file, post=new_post) new_file.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def create_image(cp, xml, itype, name, version, snap, desc='', enable=True): i = Image() i.cephpool = cp i.name = name i.version = version i.snap = snap i.desc = desc i.xml = xml i.type = itype i.enable = enable i.save() return i
def image_handler(files, request): tmp = tempfile.mkstemp() md5 = hashlib.md5() if request.POST['upload_type'] == 'file': orig = files.name fext = orig[orig.rfind('.')+1:] f = os.fdopen(tmp[0], "wb+") for chunk in files.chunks(): f.write(chunk) md5.update(chunk) f.close() elif request.POST['upload_type'] == 'url': md5.update(files) fext = request.POST['upload_url'][-3:] orig = request.POST['upload_url'] f = os.fdopen(tmp[0], "wb+") f.write(files) f.close() img = Image() try: next_id = Image.objects.order_by('-id')[0].id + 1 except IndexError: next_id = settings.IMAGE_ID_OFFSET + 1 img.id = next_id img.base62 = base62(next_id) img.filename = base62(next_id) + "." + fext.lower() img.orig_filename = orig img.type = '' # todo img.description = '' # not implemented yet. img.uploader = request.user img.md5sum = md5.hexdigest() image_file = os.path.join(settings.MEDIA_ROOT,img.filename) thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename) try: img.save() except IntegrityError, e: os.unlink(tmp[1]) # delete the uploaded file if it already exists return HttpResponseRedirect( settings.MEDIA_URL + Image.objects.get(md5sum=img.md5sum).filename)
def update_image(cls, engines): for engine in engines: data = {'exec': 'images()'} response = requests.post(engine.url(), data=data) results = response.json()['results'] for result in results: image = Image() image.create_at = datetime.fromtimestamp(result['Created']) image.uid = result['Id'] image.size = result['Size'] image.virtualsize = result['VirtualSize'] image.tag = json.dumps(result['RepoTags']) image.name = result['RepoTags'][0].split(':')[0] image.engine = engine if image.name == '<none>': image.order = 99 image.save()