def test_iiif_info_json(): img1 = IIIFImageClient("http://image.server/path/", "myimgid") img2 = IIIFImageClient("http://image.server/path/", "myimgid2") imgs = [{"image": img1}, {"image": img2}] json_ids = corpus_extras.iiif_info_json(imgs) # should contain the same ids but with /info.json appended assert "http://image.server/path/myimgid/info.json" in json_ids assert "http://image.server/path/myimgid2/info.json" in json_ids
def loris_info(image_id): # instantiate IIIFImageClient ic = IIIFImageClient(api_endpoint=localConfig.LORIS_API_ENDPOINT,image_id=image_id) # debug url info_url = ic.info() print "loris info url: %s" % info_url r = requests.get(info_url).json() return jsonify(r)
def test_iiif_image(): # copied from mep_django myimg = IIIFImageClient("http://image.server/path/", "myimgid") # check expected behavior assert str(corpus_extras.iiif_image(myimg, "size:width=250")) == str( myimg.size(width=250)) assert str(corpus_extras.iiif_image(myimg, "size:width=250,height=300")) == str( myimg.size(width=250, height=300)) assert str(corpus_extras.iiif_image(myimg, "format:png")) == str( myimg.format("png")) # check that errors don't raise exceptions assert corpus_extras.iiif_image(myimg, "bogus") == "" assert corpus_extras.iiif_image(myimg, "size:bogus") == "" assert corpus_extras.iiif_image(myimg, "size:bogus=1") == ""
def iiif_client(self): if not settings.IIIF_IMAGE_BASE_URL or not self.jp2_abs_filename: return None else: return IIIFImageClient( settings.IIIF_IMAGE_BASE_URL, quote(os.path.join(self.issue.batch.name, "data", self.jp2_filename), safe=""), )
def generate_deep_zoom(self, jekyll_site_dir): self.log_status('Downloading deep zoom images') imagedir = os.path.join(jekyll_site_dir, self.image_dir) # If exporting to GitHub, add the repo name to the path. _prefix = os.path.join(os.sep, self.github_repo, self.image_dir) if self.github_repo else os.path.join(os.sep, self.image_dir) staticgen = IIIFStatic(dst=imagedir, prefix=_prefix) for teipage in self.tei.page_list: for graphic in teipage.graphics: if graphic.rend == 'full': imgsrc = os.path.join(jekyll_site_dir, graphic.url) iiif_img = IIIFImageClient.init_from_url(graphic.url) staticgen.generate(imgsrc, identifier=iiif_img.image_id)
def generate_deep_zoom(self, jekyll_site_dir): self.log_status('Downloading deep zoom images') imagedir = os.path.join(jekyll_site_dir, self.image_dir) # If exporting to GitHub, add the repo name to the path. _prefix = os.path.join( os.sep, self.github_repo, self.image_dir) if self.github_repo else os.path.join( os.sep, self.image_dir) staticgen = IIIFStatic(dst=imagedir, prefix=_prefix) for teipage in self.tei.page_list: for graphic in teipage.graphics: if graphic.rend == 'full': imgsrc = os.path.join(jekyll_site_dir, graphic.url) iiif_img = IIIFImageClient.init_from_url(graphic.url) staticgen.generate(imgsrc, identifier=iiif_img.image_id)
def get_image_url_from_iiif_url(iiif_url, input_size=224): try: image = IIIFImageClient.init_from_url(iiif_url) except ParseError: raise ValueError(f"{iiif_url} is not a valid iiif URL") if "dlcs" in image.api_endpoint: # DLCS provides a thumbnails service which only serves certain sizes of # image. Requests for these don't touch the image server and so, as # we're performing lots of requests, we use 400x400 thumbnails and # resize them ourselves later on. image.api_endpoint = image.api_endpoint.replace("iiif-img", "thumbs") # `exact=True` equates to a `/!400,400/` image request # https://iiif.io/api/image/1.0/#4-2-size return str(image.size(width=400, height=400, exact=True)) return str(image.size(width=input_size, height=input_size, exact=False))
def image_tile(request, path, width, height, x1, y1, x2, y2): width, height = map(int, (width, height)) x1, y1, x2, y2 = map(int, (x1, y1, x2, y2)) if settings.REDIRECT_IMAGES_TO_IIIF: iiif_client = IIIFImageClient(settings.IIIF_IMAGE_BASE_URL, quote(path, safe="")) return redirect_to_iiif(request, iiif_client, width, height, x1, y1, x2, y2) else: try: p = os.path.join(settings.BATCH_STORAGE, path) im = Image.open(p) return serve_image_tile(request, im, width, height, x1, y1, x2, y2) except EnvironmentError as e: logging.exception("Unable to create image tile for %s", path) return HttpResponseServerError("Unable to create image tile: %s" % e)
def iiif_url_to_local_path(self, imgurl): '''Convert an IIIF Image API url to a local path that can be referenced within the jekyll site. ''' # check if we have an info url, so that it can be duplicated is_info = imgurl.endswith('info.json') iiif_img = IIIFImageClient.init_from_url(imgurl) # convert api endpoint to local path iiif_img.api_endpoint = self.image_dir # simplify image id: strip out iiif id prefix and pidspace # prefix, if possible # (leaving any id suffix, since those are likely needed to # guarantee uniqueness) iiif_img.image_id = iiif_img.image_id \ .replace(settings.IIIF_ID_PREFIX, '') \ .replace('%s:' % settings.FEDORA_PIDSPACE, '') # serialize updated iiif image url for use as local image path if is_info: return unicode(iiif_img.info()) return unicode(iiif_img)
def index_traversing(size, index_name): # run for 1 time and get a last_result es = create_es_client() #####document this after first run # # create a new index # es.indices.create( # index='lab_clustering_hex', # ) ##### response = es.search( index=os.environ['INDEX_NAME'], body={ "query": { "match_all": {} }, "size": size, "from": 0, "sort": { "_id": "desc", # document ID. }, }) # post the first 20 dictionaries for ID_url_dict in Extracting_url_ID(response): image_url = str(IIIFImageClient().init_from_url( ID_url_dict['url']).size(width=50)) cluster_centers_list, cluster_number, cluster_size, cluster_proportion, lab_centers_list = clustering_api_request( image_url) # Clustering in lab space. cluster_centers_list_hex = rgblist2hexlist(cluster_centers_list) searchstr = labcenters2searchterm(cluster_number, lab_centers_list) # update the document es.index(index=index_name, id=ID_url_dict['id'], body={ "iiif_url": ID_url_dict['url'], "image_url": image_url, "cluster_info": { "cluster_centers_list": cluster_centers_list, "cluster_centers_list_hex": cluster_centers_list_hex, "cluster_number": cluster_number, "cluster_size": cluster_size, "cluster_proportion": cluster_proportion, "lab_centers_list": lab_centers_list, "searchstr": searchstr, } }) print(ID_url_dict['id']) last_result_id = response['hits']['hits'][size - 1]['_id'] counter = size # just to make the waiting time less painful while len(response['hits']['hits']) == size: # get in the loop! response = es.search( index=os.environ['INDEX_NAME'], body={ "query": { "match_all": {} }, "size": size, "from": 0, "sort": { "_id": "desc", # document ID. }, "search_after": [last_result_id], }) for ID_url_dict in Extracting_url_ID( response): # post the dictionaries image_url = str(IIIFImageClient().init_from_url( ID_url_dict['url']).size(width=50)) cluster_centers_list, cluster_number, cluster_size, cluster_proportion, lab_centers_list = clustering_api_request( image_url) cluster_centers_list_hex = rgblist2hexlist(cluster_centers_list) searchstr = labcenters2searchterm(cluster_number, lab_centers_list) es.index(index=index_name, id=ID_url_dict['id'], body={ "iiif_url": ID_url_dict['url'], "image_url": image_url, "cluster_info": { "cluster_centers_list": cluster_centers_list, "cluster_centers_list_hex": cluster_centers_list_hex, "cluster_number": cluster_number, "cluster_size": cluster_size, "cluster_proportion": cluster_proportion, "lab_centers_list": lab_centers_list, "searchstr": searchstr, } }) print(ID_url_dict['id'] ) # just to make the waiting time less painful last_result_id = ID_url_dict['id'] # update the last ID counter += size print(counter)