def get_pil_img_from_url(cls, url, cache=False, grey=False): ''' Returns a PIL image instance from an image resource at the given URL cache=True to cache subsequent calls grey=True to return a greyscale image ''' #print url ret = None from django.template.defaultfilters import slugify disk_path = os.path.join(settings.IMAGE_CACHE_ROOT, '%s.jpg' % slugify(url)) if not cache or not os.path.exists(disk_path): from digipal.management.commands.utils import web_fetch, write_file #print '\tDL: %s' % url res = web_fetch(url) if res['body']: write_file(disk_path, res['body']) if os.path.exists(disk_path): from PIL import Image ret = Image.open(disk_path) if grey: ret = ret.convert('L') # We resize the image to the requested size asked = [] import re for d in [u'WID', u'HEI']: l = re.sub(ur'.*' + re.escape(d) + ur'=(\d+).*', ur'\1', url) if l != url: asked.append(int(l)) if len(asked) == 2: import PIL ret = ret.resize(asked, PIL.Image.ANTIALIAS) return ret
def get_img_size(self): ''' Returns the size of the full res. image as a list: [width, height] ''' return self.dimensions() cached_sizes = getattr(self, 'cached_sizes', {}) url = self.iipimage.full_base_url+'&OBJ=Max-size' if url not in cached_sizes: cached_sizes[url] = [0, 0] from digipal.management.commands.utils import web_fetch res = web_fetch(url) if not res['error']: import re matches = re.match(r'^.*?Max-size:(\d+)\s+(\d+).*?$', res['body'].strip()) if matches: cached_sizes[url] = [int(matches.group(1)), int(matches.group(2))] self.cached_sizes = cached_sizes return cached_sizes[url]
def get_img_size(self): ''' Returns the size of the full res. image as a list: [width, height] ''' return self.dimensions() cached_sizes = getattr(self, 'cached_sizes', {}) url = self.iipimage.full_base_url + '&OBJ=Max-size' if url not in cached_sizes: cached_sizes[url] = [0, 0] from digipal.management.commands.utils import web_fetch res = web_fetch(url) if not res['error']: import re matches = re.match(r'^.*?Max-size:(\d+)\s+(\d+).*?$', res['body'].strip()) if matches: cached_sizes[url] = [ int(matches.group(1)), int(matches.group(2)) ] self.cached_sizes = cached_sizes return cached_sizes[url]