def upload_ajax(request): if request.method == 'POST': if not request.session.get('id'): return HttpResponse("请先登录后再上传") file_obj = request.FILES.get('file') type_list = ['.jpg', '.png'] try: title = request.POST['title'] except KeyError: title = "无标题" if os.path.splitext(file_obj.name)[1].lower() in type_list: savename = "%s_im_%s" % (random.randint(10000, 99999), file_obj.name) f = open(os.path.join(BASE_DIR, 'static', 'images', savename), 'wb') for chunk in file_obj.chunks(): f.write(chunk) f.close() user = User.objects.get(id=request.session.get('id')) if os.path.getsize("%s/%s" % (os.path.join(BASE_DIR, 'static', 'images'), savename)) > 1024000: img = Image.open('%s/%s' % (os.path.join(BASE_DIR, 'static', 'images'), savename)) w, h = img.size img.thumbnail((w / 2, h / 2), Image.ANTIALIAS) savename = "%s_%s" % ('new', savename) img.save("%s/%s" % (os.path.join(BASE_DIR, 'static', 'images'), savename), "JPEG") p = Picture(user=user, filename=savename, title=title, haveher=False, good=0, show=False) p.save() return HttpResponse('OK') return HttpResponse("错误上传类型")
def upload(): form = PictureForm() if form.validate_on_submit(): if 'file' not in request.files: flash('No File!') return redirect(url_for('upload')) if form.file.data.filename == '': flash('No Selected File!') return redirect(url_for('upload')) if form.file and allowed_file(form.file.data.filename): filename = secure_filename(form.file.data.filename) extension = filename.rsplit('.', 1)[1].lower() picture = Picture(extension=extension, description=form.description.data, shot_time=form.shot_time.data, place=form.place.data, tags=form.tags.data) picture.save() unified_filename = str(picture.id) + '.' + extension f = form.file.data try: picture_handler(f, unified_filename) except: print("picture file may not be updated") flash('New picture uploaded!') return redirect(url_for('admin.upload')) return render_template('admin/upload.html', title='Upload', form=form)
def _start(): if face.faceai(filepath + "/" + filename): p = Picture(filename=filename, title="default", haveher=True, good=0) p.save() print("Upload Picture")
def save_to_graph(self, photos): """ parameters ---------- photos: array - see the return of retrieve_photos_from_flickr() for the structure """ self.stdout.write("* Saving photos information in the graph", ending='\r') # list of tags from all photos without duplicates (several photos may have the same tag) # used to cache the Interest objects and create the nodes if they don't exist # while reducing the number of queries on the database interests = {} index = 1 for photo in photos: url = photo['url'] try: picture = Picture(pictureURL=url) picture.save() except UniqueProperty: # a picture node with this url already exists in the graph # we will only update the interests it is RELATED_TO picture = Picture.nodes.get(pictureURL=url) for tag in photo['tags']: if tag == self.SEARCH_TAG: # we don't add the 'couple' tag to the database because of course our app is about couples continue # if the tag hasn't been added yet to the list of interests if tag not in interests: try: interest = Interest(label=tag) interest.save() except UniqueProperty: # an interest node with this label already exists in the graph interest = Interest.nodes.get(label=tag) interests[tag] = interest else: # the interest is already in the list, get it and avoid a useless query to the db interest = interests[tag] picture.tags.connect(interest) self.stdout.write('* Photos saved: {} '.format(index), ending='\r') index += 1 self.stdout.write('')
def classifier(request, classifier_name): classifier = Classifier.objects.get(name=classifier_name) if request.method == 'GET': context = { 'name': classifier.name, 'class0': classifier.class0, 'class1': classifier.class1, 'algorithm': classifier.algorithm, 'description': classifier.description } return render(request, 'classifier.html', context) else: pictureFiles = request.FILES.getlist('img') newPicsList = [] for pic in pictureFiles: newPic = Picture(picture = pic) newPic.save() newPicsList.append(newPic) return classificationResult(request, classifier, newPicsList)
def upload(request): if request.method == "POST": print(request.FILES.get("file"), type(request.FILES.get("file"))) temp_file = request.FILES.get("file") if request.POST.get("type") == "picture": img = Picture(file=temp_file, name=temp_file.name, size=temp_file.size, point=request.POST.get("point", None), title=request.POST.get("title", None), type=request.POST.get("type_picture", None), album=PhotoAlbum.objects.filter( id=request.POST.get("album_id")).first(), date=datetime.datetime.now().strftime('%Y-%m-%d')) img.save() obj = PhotoAlbum.objects.filter(id=request.POST.get("album_id")) length = Picture.objects.filter(album_id=obj) print(len(length), type(length)) obj.update(count=len(length)) return HttpResponse(json.dumps( {"data": { "e": 0, "code": "success" }}), content_type="application/json") else: media = Media(file=temp_file, name=temp_file.name, size=temp_file.size, point=request.POST.get("point", None), title=request.POST.get("title", None), date=datetime.datetime.now().strftime('%Y-%m-%d')) media.save() return HttpResponse(json.dumps( {"data": { "e": 0, "code": "success" }}), content_type="application/json") return HttpResponse(json.dumps({ "data": { "e": -1, "code": "{} method don't allow".format(request.method) } }), content_type="application/json")
def img_upload_ui(): img_obj = request.files.get("file") filename = img_obj.filename img_type = filename[filename.rfind(".") + 1:] from app.models import Picture # pic = Picture(image=img_obj) pic = Picture(family="swipers", creator="user open id", img_type=img_type) # 添加图片,并且 追加一个 content_type 属性进去,回头返回的时候,也好填写 Conten-Type 啊 pic.image.put(img_obj, content_type=img_obj.content_type) rs = pic.save() # rs.id 是 objectId: Object of type 'ObjectId' is not JSON serializable,所以这里把它转成字符串str return jsonify({"pic_id": str(rs.id)})
def img_upload(): # for attr in dir(request): # print(attr, getattr(request, attr)) # print("request.files", request.files) img_obj = request.files.get("img") filename = img_obj.filename img_type = filename[filename.rfind(".") + 1:] from app.models import Picture # pic = Picture(image=img_obj) pic = Picture(family="avatar", creator="user open id", img_type=img_type) # 添加图片,并且 追加一个 content_type 属性进去,回头返回的时候,也好填写 Conten-Type 啊 pic.image.put(img_obj, content_type=img_obj.content_type) rs = pic.save() print(rs.to_mongo()) print(rs.id) return jsonify(rs)