def uploadRestore(request): img = request.POST.get('img') mask = request.POST.get('mask') fname = datetime.datetime.now().strftime("%Y%m%d%H%M%S") user_folder = 'media' src = f'{user_folder}/input/{fname}.png' maskdir = f'{user_folder}/input/m-{fname}.png' dest = f'{user_folder}/output/o-{fname}.png' uri = DataURI(img) fd = open(src, 'wb') fd.write(uri.data) fd.close() uri = DataURI(mask) fd = open(maskdir, 'wb') fd.write(uri.data) fd.close() cp.saveCompressed(src) rs.restore(src, maskdir, dest) output_uri = b64encode(DataURI.from_file(dest).data) os.remove(src) os.remove(maskdir) os.remove(dest) return HttpResponse(output_uri)
def RecipeAdd(request): if request.method=='POST': try: body = json.loads(request.body.decode('utf-8')) details = body['data']['details'] primaryType = PrimaryType.objects.get(name=details['primaryType']) secondaryType = SecondaryType.objects.get(name=details['secondaryType']) season = None if details['season'] : season = Season.objects.get(name=season) # create the recipe recipe = Recipe.objects.create(name=details['name'],description=details['description'],prepTime=details['prepTime'],cookTime=details['cookTime'],primaryType=primaryType,secondaryType=secondaryType,season=season) recipe.filename = '{0:08d}'.format(recipe.id) recipe.save() # save the image if 'pdf' in details: image = DataURI(details['image']) if(len(image.data) <= 1000000): with open(BASE_DIR+'/build/assets/resources/'+recipe.filename+'.jpg', 'wb+') as file: file.write(image.data) with open(BASE_DIR+'/public/assets/resources/'+recipe.filename+'.jpg', 'wb+') as file: file.write(image.data) # save the pdf if 'pdf' in details: pdf = DataURI(details['pdf']) if(len(pdf.data) <= 1000000): with open(BASE_DIR+'/build/assets/resources/'+recipe.filename+'.pdf', 'wb+') as file: file.write(pdf.data) with open(BASE_DIR+'/public/assets/resources/'+recipe.filename+'.pdf', 'wb+') as file: file.write(pdf.data) # grant access to this new recipe in this account only account = AccountUser.objects.get(user=request.user).account AccountRecipe.objects.create(account=account,recipe=recipe) # create ingredients for ingredient in body['data']['ingredients']: try: accing = AccountIngredient.objects.get(account=account,ingredient__name=ingredient['ingredient']) accing.fresh = ingredient['fresh'] accing.save() ing = Ingredient.objects.get(accountingredient__account=account,name=ingredient['ingredient']) except ObjectDoesNotExist: ing,created = Ingredient.objects.get_or_create(name=ingredient['ingredient']) AccountIngredient.objects.create(account=account,ingredient=ing,fresh=ingredient['fresh'],supermarketCategory=ing.supermarketCategory,pantryCategory=ing.pantryCategory,stockcode=ing.stockcode,cost=ing.cost) mea = Measure.objects.get(name=ingredient['measurement']) RecipeIngredient.objects.create(recipe=recipe,quantity=ingredient['two'],quantity4=ingredient['four'],quantityMeasure=mea,ingredient=ing) return JsonResponse(dict(id=recipe.id)) except Exception as e: raise e return JsonResponse({'status':'false','message':str(e)}, status=500) response = HttpResponse() response['allow'] = "post, options" return response
def test_parse_invalid_datauri(self): t = 'data:*garbled*;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu' with self.assertRaises(exceptions.InvalidDataURI): DataURI(t) t = 'data:text/plain;charset=*garbled*;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu' with self.assertRaises(exceptions.InvalidDataURI): DataURI(t)
def test_url_is_valid(self): content = BytesIO(DataURI(DATA_URI).data) # mock the response with patch('requests.get') as m: content = BytesIO(DataURI(DATA_URI).data).read() m.return_value.content = content m.return_value.status_code = 200 self.assertTrue( is_uri_valid_favicon('http://example.com.com/favicon.ico'))
def get(self, request, pk, pointer, model_class=None, field_name=None): """ :param request: :param pk: the pk of the instance :param pointer: the json pointer of the image. Ex: "people/image", "people/0/image", etc. :param model_class: the model that holds the JSON field :param field_name: the JSON-field name :return: HttpResponse with the image Base view to serve an URI-stored (in a JSON field) image as a file. Example usage in urls.py: re_path( r"some/path/(?P<pk>\d+)/(?P<pointer>.+)/", URIImageAsFileView.as_view(), kwargs={"model_class": MyModel, "field_name": "my_json_field"}, ), """ obj = get_object_or_404(model_class or self.model_class, pk=pk) data = getattr(obj, field_name or self.field_name) pointer = "/{}".format(pointer) try: data = resolve_pointer(data, pointer) uri = DataURI(data) return HttpResponse(uri.data, content_type=uri.mimetype) except (InvalidDataURI, JsonPointerException): raise Http404("Image not found")
def download_or_create_favicon(favicon: str, domain: str) -> Image: """ :param favicon: :param domain: :return: """ file_path = os.path.dirname(os.path.realpath(__file__)) generic_favicon = "{}/generic_favicon.png".format(file_path) # handle the case for data: URIs if favicon.startswith('data:image'): uri = DataURI(favicon) data = BytesIO(uri.data) # Favicon was determined to be missing, so use a generic icon elif favicon == 'missing': data = generic_favicon # Looks like a normal favicon url, lets go get it elif favicon.startswith('http'): r = download_remote_favicon(favicon) data = BytesIO(r) if r else generic_favicon # return make_image(domain) img = Image.open(data) img = img.convert('RGB') if img.mode == 'CMYK' else img return img
def test_repr(self): t = 'data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu' uri = DataURI(t) self.assertEqual( repr(uri), "DataURI('data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu')" )
def dynamic(command): if flag == 1: postData = request.form if command == "tts": response = ttsService.synthesize( postData["text"], accept='audio/mp3', voice=postData["lang"]).get_result() return jsonify({ "blobB64": "data:audio/mpeg;base64,{}".format( base64.b64encode(response.content).decode("utf-8")) }) elif command == "langlist": voices = ttsService.list_voices().get_result() result = [] for voice in voices["voices"]: result.append({ "text": voice["description"], "value": voice["name"] }) return jsonify(result) elif command == "modellist": models = sttService.list_models().get_result() result = [] for model in models["models"]: result.append({ "text": model["description"], "value": model["name"] }) return jsonify(result) elif command == "stt": uri = DataURI(postData["blobB64"]) response = sttService.recognize( audio=uri.data, content_type=uri.mimetype, word_alternatives_threshold=0.9, keywords=[ 'yes', 'no', 'option', 'one', 'two', 'three', 'four' ], keywords_threshold=0.9).get_result() if len(response['results']) > 0: return jsonify({ "text": response['results'][0]["alternatives"][0]["transcript"] }) else: return jsonify({"text": "nothing"}) elif command == "storylist": stories = glob.glob("stories/*.json") result = [] for story in stories: with open(story, "rb") as storyFile: storyJSON = json.load(storyFile) result.append({ "text": storyJSON["header"]["title"]["text"], "value": os.path.basename(story) }) return jsonify(result) else: return jsonify({"error": "keyError"})
def test_valid_request(self): json_data = { 'device_token': self.tokenDevice, 'files': ["test_file.txt"], 'conversation_id': self.conversation.id } response = self.api.post('/device/message/send', data=json.dumps(json_data), content_type='application/json') response_json = json.loads(response.data) assert response.status_code == 200 assert response_json['success'] is True assert len(response_json['media_list']) == 1 headers = { 'content-type': 'multipart/form-data', 'Authorization': self.tokenDevice } data = {'file': (io.BytesIO(b"device sent a file"), 'test_file.txt')} media_id = response_json['media_list'][0]['id'] response = self.api.post('/media/upload/' + str(response_json['media_list'][0]['id']), data=data, headers=headers) response_json = json.loads(response.data) assert response_json["success"] is True assert os.path.exists('user_files' + os.path.sep + 'conversation_' + str(self.conversation.id) + os.path.sep) json_data = {"device_token": self.tokenDevice, "media_id": media_id} response = self.api.post('/media/retrieve', data=json.dumps(json_data), content_type='application/json') response_json = json.loads(response.data) response_file = DataURI(response_json['data']) assert response_json['success'] assert response_file.data == b"device sent a file"
def test_wrap(self): t = 'data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu' parsed = DataURI(t) self.assertEqual( parsed.wrap(), """data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3Z lciB0aGUgbGF6eSBkb2cu""")
def _localize_data_uri(self, locout, source, dest): """Localizes an entry where the source is a data: URI""" try: uri = DataURI(source) except ValueError: locout.write('Could not parse "{}" as a data URI: {}\n'.format( source, str(e))) return False try: with open(dest, 'w+', buffering=1) as destout: try: uri_data = uri.data.decode() except AttributeError: uri_data = uri.data destout.write(uri_data) locout.write('{}: wrote {} bytes\n'.format( dest, len(uri_data))) except IOError as e: locout.write('{}: I/O error({0}): {1}\n'.format( dest, e.errno, e.strerror)) return False except: locout.write('{}: unexpected error: {}\n'.format( sys.exc_info()[0])) return False return True
def get_speaker_image(self, data): image_b64 = data.get('image', None) if not image_b64: return None image = DataURI(image_b64) return NamedBlobImage(data=image.data, filename=image.name, contentType=image.mimetype)
def post_sourceimage(request): data = json.loads(request.body.decode('utf-8') or "{}") uri = DataURI(data.pop('src')) f = ContentFile(uri.data, name=uri.name) si = SourceImage(uploaded_by=request.user) si.src.save(f.name, f) si.save() FILES = {'src': f} return JsonResponse({'sourceimage_id': si.id})
def test_url_is_valid(self): url = 'http://example.com.com/favicon.ico' # mock the response with patch('requests.get') as m: content = DataURI(DATA_URI).data m.return_value.content = content m.return_value.status_code = 200 image = self.favicon.download_remote_favicon(url) self.assertEqual(image, content)
def transcribe(uri): # Parses a URI and gets the encoded data string out data = DataURI(uri).data image = types.Image(content=data) response = client.document_text_detection(image) if response.text_annotations: return response.text_annotations[0].description.replace('\n', ' ') else: return "No Text"
def imageTransition(): id = request.form["id"] filter = request.form["filter"] image_length = int(request.form["image_length"]) input_images_dataURI = [ DataURI(request.form[f"image{i}"]).data for i in range(image_length) ] # # FIXME: uuid-인덱스 로 이미지 저장 user_uuid = uuid.uuid4() os.makedirs(f'./{user_uuid}/before') os.makedirs(f'./{user_uuid}/after') for idx in range(image_length): im = Image.open(BytesIO(input_images_dataURI[idx])) im = im.convert('RGB') im.save(f'{user_uuid}/before/image{idx}.jpg') # TODO: 이미지 변환 from whitebox import cartoonize model_path = 'saved_models' location = f'{user_uuid}' cartoonize.cartoonize(location, model_path) from model import gallery output_images_dict = {} for idx in range(image_length): # FIXME: 이미지를 datauri로 변경 translated_image = DataURI.from_file( f'./{user_uuid}/after/image{idx}.jpg') # FIXME: 회원일 경우 이미지를 개인 갤러리에 저장 if id != 'Not User': gallery.imageSave(id, filter, translated_image) # FIXME: 변환된 이미지를 클라이언트에게 보내기 위해.. output_images_dict[f'image{idx}'] = translated_image os.remove(f'./{user_uuid}/before/image{idx}.jpg') os.remove(f'./{user_uuid}/after/image{idx}.jpg') # FIXME: 저장된 이미지 삭제 os.rmdir(f'./{user_uuid}/before') os.rmdir(f'./{user_uuid}/after') os.removedirs(f'./{user_uuid}') # FIXME: 이미지들 전송 return jsonify( result="OK", imageLength=image_length, cartoonImages=output_images_dict, )
def __init__(self, uri: PlasterURL): self.uri = uri self.data_uri = DataURI("data:" + uri.path) self.parser = DataUriConfigParser(self.data_uri) # if self.data_uri.co if self.data_uri.mimetype == "application/x-gzip": self.parser.read_string( gzip.decompress(self.data_uri.data).decode('utf-8')) else: self.parser.read_string( self.data_uri.data.decode(self.data_uri.charset or 'utf-8'))
def parameters(self, line): self.driver.implicitly_wait(5) self.findbyxpath( '/html/body/div[2]/div/div[1]/ul/li[8]/a') # xpath para RAW button self.findbyxpath( '//*[@id="s1"]') # xpath para definir tamanho e cor button txtarea = self.driver.find_element_by_id('text') txtarea.clear() txtarea.send_keys(str(line).strip( )) # line: string - enviada a linha do arquivo texto para o campo # para geração do qr code na web tamanho_imagem = self.driver.find_element_by_id('qrsi') self.driver.execute_script("arguments[0].setAttribute('value','200')", tamanho_imagem) self.findbyxpath( '//*[@id="borderr"]/label[2]') # xpath para tamanho da fronteira self.findbyxpath( '//*[@id="errr"]/label[4]') # xpath para correcao de cor self.findbyxpath( '//*[@id="radiusr"]/label[2]') # xpath para tamanho do raio self.findbyxpath('//*[@id="s2"]') # xpath para definir logotipo self.findbyxpath( '//*[@id="labelmode"]/label[3]') # xpath para definir foto sel_photo = self.driver.find_element_by_id('image') sel_photo_path = 'C:/dev/qrcode/img/tabelionato_xisto.jpeg' sel_photo.send_keys(sel_photo_path) self.findbyxpath( '//*[@id="fontsize"]/label[4]') # xpath para dimensoes self.driver.implicitly_wait(5) imagem = (line.split('=')[1].strip() ) + '.png' # separando a URL e o numero para # o nome do arquivo de imagem(PNG) param = "arguments[0].setAttribute('download','" + imagem + "')" self.driver.implicitly_wait(5) baixa_qrcode = self.driver.find_element_by_id('dlbtn') self.driver.execute_script(param, baixa_qrcode) sleep(2) baixa_qrcode.click() """ Salva imagem com URI Basecode64.""" url = baixa_qrcode.get_property('href') uri = DataURI(url) with open("C:/dev/qrcode/qrcodes_imgs/" + imagem, 'wb') as f: f.write(uri.data)
def login(): if request.method == 'POST': img_data = request.json['data'] uri = DataURI(img_data) fd = open("tmp.png", 'wb') fd.write(uri.data) fd.close() res = pre('tmp.png') print(res) return jsonify({'p': str(res[0]), 'np': str(res[1])}) else: return "GET Eight"
def write_files(self, ignition_files): """writes the given files to disk.""" for file_info in ignition_files: path = self.base_dir + file_info["path"] mode = file_info["mode"] # convert the data to the actual content content = DataURI(file_info["contents"]['source']) logging.info("Creating dir: %s", os.path.dirname(path)) os.makedirs(os.path.dirname(path), exist_ok=True) logging.info("Opening file: %s", path) with open(path, "w") as out_file: logging.debug("Writing content: %s", content.text) out_file.write(content.text) logging.info("Running chmod permissions: %s, path: %s", mode, path) os.chmod(path, mode)
def is_uri_valid_favicon(uri: str) -> bool: """ :param uri: :return: """ if uri.startswith('http') is False and uri.startswith('data:image') is False: return False if uri.startswith('http'): data = download_remote_favicon(uri) if uri.startswith('data:image'): data = DataURI(uri).data return is_bytes_valid_favicon(BytesIO(data)) if data else False
def test_valid_create(self): json_data = { 'device_token': self.tokenDevice, 'medias': [{ "purpose": "testing", "name": "test_file.txt" }, { "purpose": "picture", "name": "test_file2.txt" }], } response = self.api.post('/media/create', data=json.dumps(json_data), content_type='application/json') response_json = json.loads(response.data) assert response.status_code == 200 assert response_json['success'] is True assert len(response_json['media_list']) == 2 headers = { 'content-type': 'multipart/form-data', 'Authorization': self.tokenDevice } data = { 'file': (io.BytesIO(b"user1 sent a file 225"), 'test_file.txt') } mid = response_json['media_list'][0]['id'] response = self.api.post('/media/upload/' + str(response_json['media_list'][0]['id']), data=data, headers=headers) response_json = json.loads(response.data) assert response_json["success"] is True assert os.path.exists('user_files' + os.path.sep + 'device_' + str(self.device.id) + os.path.sep) json_data = {"device_token": self.tokenDevice, "media_id": mid} response = self.api.post('/media/retrieve', data=json.dumps(json_data), content_type='application/json') response_json = json.loads(response.data) response_file = DataURI(response_json['data']) assert response_json['success'] assert response_file.data == b"user1 sent a file 225"
def classify_image(): """Classify an image""" url = request.json['url'] uri = DataURI(url) tmp_image_path = UPLOAD_DIRECTORY + '/temp_image.png' with open(tmp_image_path, 'wb') as f: f.write(uri.data) model = torch.jit.load('model.pt') probs, classes = predict(tmp_image_path, model, topk=1, category_names='cat_to_name.json') # Return 201 CREATED return jsonify( { 'class': probs[0], 'confidence_score': classes[0] } )
def value_from_datadict(self, data, files, name): """ Given a dictionary of data and this widget's name, returns the value of this widget. Returns None if it's not provided. """ if name in files: return files[name] elif '{}_data'.format(name) in data: try: file = DataURI(data['{}_data'.format(name)]) except (ValueError, TypeError, binascii.Error): return None # file, field_name, name, content_type, size, charset, return InMemoryUploadedFile(file=io.BytesIO(file.data), field_name=name, name='{}{}'.format(hashlib.sha1(file.data).hexdigest(), mimetypes.guess_extension(file.mimetype)), content_type=file.mimetype, size=len(file.data), charset=file.charset)
def set_entries(req, **kwargs): """ :kwargs: file """ data = DataURI(kwargs['file']) file = BytesIO(data.data) if data.mimetype != 'text/csv': return {'status': False, 'data': 'Accepts CSV.'} else: df = pd.read_csv(file) try: tups = list(zip(df.entry, df.level)) with db.atomic(): Entry.insert_many(tups, fields=[Entry.entry, Entry.level]).execute() except Exception as e: return {'status': False, 'data': repr(e)} return {'status': True, 'data': ''}
def get_pred(): """ POST / Params: images (list): List of IDs that points to images in redis. Returns: Prediction in list of object, each object specifies the the two corners of the bounding box, the confidence level and the prediction. """ # get image list if 'images' in request.json: images = request.json['images'] else: return jsonify([]) # Convert keys to DataURIs through redis images = list(map(lambda s: r.get(s).decode(), images)) # Convert strings to DataURIs images = list(map(lambda b: DataURI(b), images)) # Filter out non-images images = list(filter(lambda uri: uri.mimetype[:5] == 'image', images)) # Early return if no images are in list if len(images) == 0: return jsonify([]) # Decode DataURI into np.ndarray images = list( map( lambda uri: cv2.imdecode(np.frombuffer(uri.data, np.uint8), -1) [:, :, :3], images)) # Detect object in images with torch.no_grad(): json = detect(images) return jsonify(json)
def download_remote_favicon(self, favicon_url: str) -> bytes: if favicon_url.startswith('data:image'): return DataURI(favicon_url).data try: # need to set headers here to fool sites like cafepress.com... h = requests.get(favicon_url, allow_redirects=True, timeout=TIMEOUT, headers=HEADERS, verify=False) except (requests.exceptions.Timeout, requests.exceptions.ConnectionError, requests.exceptions.InvalidSchema, requests.exceptions.MissingSchema, requests.exceptions.InvalidURL) as e: raise FavIconException(e) try: h.raise_for_status() except HTTPError as e: raise FavIconException(f'HTTP Error on favicon url: {favicon_url}') if len(h.content) == 0: raise FavIconException(f'download_remove_favicon: Zero Length favicon: {favicon_url}') # is the returning file SVG? If so, we have to convert it to bitmap (png) # content = cairosvg.svg2png(bytestring=h.content) if 'SVG' in from_buffer(h.content) else h.content if 'SVG' in from_buffer(h.content) or favicon_url.endswith('.svg'): image = cairosvg.svg2png(bytestring=h.content) else: image = h.content if not self.is_bytes_valid_favicon(image): raise FavIconException(f'download_remove_favicon: Downloaded icon was not valid image: {favicon_url}') return image
def addStory(updateDat): assert 'story' in updateDat story = updateDat['story'] assert 'name' in story assert 'auth' in story assert 'fname' in story assert 'file' in story assert 'desc' in story assert 'tags' in story data = DataURI(story['file']) dathash = getHash(data.data).lower() have = Story.query.filter(Story.hash == dathash).scalar() if have: # print("Have file already!") return getResponse( "A file with that MD5 hash already exists! Are you accidentally adding a duplicate?", True) have = Story.query.filter(Story.title == story['name']).scalar() if have: orig_name = story['name'] loop = 2 while have: print("Have story with that name ('%s')!" % story['name']) story['name'] = orig_name + " (%s)" % loop have = Story.query.filter(Story.title == story['name']).scalar() loop += 1 print("Story added with number in name: '%s'" % story['name']) if len(story['name']) > 80: return getResponse("Maximum story title length is 80 characters!", True) if len(story['name']) < 3: return getResponse("Minimum story title length is 3 characters!", True) if len(story['auth']) < 5: return getResponse("Minimum story author name length is 5 characters!", True) if len(story['auth']) > 60: return getResponse( "Maximum story author name length is 60 characters!", True) if len(story['desc']) < 30: return getResponse( "Minimum story description length is 30 characters!", True) if len(story['desc']) > 500: return getResponse( "Maximum story description length is 500 characters!", True) fspath = saveFile(data.data, story['fname']) stags = ["-".join(itm_tags.split(" ")) for itm_tags in story['tags']] stags = [bleach.clean(tag, tags=[], strip=True) for tag in stags] # print("Author: ", story['auth']) # print("stags: ", story['tags']) # print("stags: ", stags) post_date = datetime.datetime.now() if 'ul_date' in story and isinstance(story['ul_date'], datetime.datetime): post_date = story['ul_date'] new = Story( title=bleach.clean(story['name'], tags=[], strip=True), srcfname=story['fname'], description=markdown.markdown(bleach.clean(story['desc'], strip=True)), fspath=fspath, hash=dathash, # author = [story['auth']], # tags = stags, pub_date=post_date) [new.tags.append(Tags(tag=tag)) for tag in stags] new.author.append( Author(name=bleach.clean(story['auth'], tags=[], strip=True))) db.session.add(new) db.session.commit() flash('Your story has been added! Thanks for posting your content!') return getResponse("Story added", error=False)
def test_parse_name_no_charset(self): t = 'data:text/plain;name=file.txt;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu' parsed = DataURI(t) self.assertEqual(parsed.name, 'file.txt')
def test_urlencoded(self): t = "data:text/plain;name=file%201%20('final'!)%20*~.txt;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu" parsed = DataURI(t) self.assertEqual(parsed.name, "file 1 ('final'!) *~.txt")