Esempio n. 1
0
 def __call__(self, data, **metadata):
     image_type = imghdr.what('', h=data)
     if image_type == 'png':
         f = "PNG"
         parts = image_parser.parse_png(data)
         fmt = base.format_dict(multidict.MultiDict(parts))
         return "%s image" % f, fmt
     elif image_type == 'gif':
         f = "GIF"
         parts = image_parser.parse_gif(data)
         fmt = base.format_dict(multidict.MultiDict(parts))
         return "%s image" % f, fmt
     elif image_type == 'jpeg':
         f = "JPEG"
         parts = image_parser.parse_jpeg(data)
         fmt = base.format_dict(multidict.MultiDict(parts))
         return "%s image" % f, fmt
     try:
         img = Image.open(io.BytesIO(data))
     except IOError:
         return None
     parts = [
         ("Format", str(img.format_description)),
         ("Size", "%s x %s px" % img.size),
         ("Mode", str(img.mode)),
     ]
     for i in sorted(img.info.keys()):
         if i != "exif":
             parts.append((str(i), str(img.info[i])))
     fmt = base.format_dict(multidict.MultiDict(parts))
     return "%s image" % img.format, fmt
Esempio n. 2
0
 def __call__(self, data, **metadata):
     if imghdr.what('', h=data) == 'png':
         f = "PNG"
         parts = image_parser.parse_png(data)
         fmt = base.format_dict(multidict.MultiDict(parts))
         return "%s image" % f, fmt
     try:
         img = Image.open(io.BytesIO(data))
     except IOError:
         return None
     parts = [
         ("Format", str(img.format_description)),
         ("Size", "%s x %s px" % img.size),
         ("Mode", str(img.mode)),
     ]
     for i in sorted(img.info.keys()):
         if i != "exif":
             parts.append((str(i), str(img.info[i])))
     if hasattr(img, "_getexif"):
         ex = img._getexif()
         if ex:
             for i in sorted(ex.keys()):
                 tag = ExifTags.TAGS.get(i, i)
                 parts.append((str(tag), str(ex[i])))
     fmt = base.format_dict(multidict.MultiDict(parts))
     return "%s image" % img.format, fmt
Esempio n. 3
0
def test_format_dict():
    d = {"one": "two", "three": "four"}
    f_d = base.format_dict(d)
    assert next(f_d)

    d = {"adsfa": ""}
    f_d = base.format_dict(d)
    assert next(f_d)

    d = {}
    f_d = base.format_dict(d)
    with pytest.raises(StopIteration):
        next(f_d)
Esempio n. 4
0
 def __call__(self, data, **metadata):
     image_type = imghdr.what('', h=data)
     if image_type == 'png':
         image_metadata = image_parser.parse_png(data)
     elif image_type == 'gif':
         image_metadata = image_parser.parse_gif(data)
     elif image_type == 'jpeg':
         image_metadata = image_parser.parse_jpeg(data)
     else:
         image_metadata = [("Image Format", image_type or "unknown")]
     if image_type:
         view_name = "{} Image".format(image_type.upper())
     else:
         view_name = "Unknown Image"
     return view_name, base.format_dict(multidict.MultiDict(image_metadata))
Esempio n. 5
0
 def __call__(self, data, **metadata):
     image_type = imghdr.what('', h=data)
     if image_type == 'png':
         image_metadata = image_parser.parse_png(data)
     elif image_type == 'gif':
         image_metadata = image_parser.parse_gif(data)
     elif image_type == 'jpeg':
         image_metadata = image_parser.parse_jpeg(data)
     else:
         image_metadata = [
             ("Image Format", image_type or "unknown")
         ]
     if image_type:
         view_name = "{} Image".format(image_type.upper())
     else:
         view_name = "Unknown Image"
     return view_name, base.format_dict(multidict.MultiDict(image_metadata))