Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ...
- incomming_image = self.request.get("img")
- image = db.Blob(incomming_image)
- ...
- Start Marker | JFIF Marker | Header Length | Identifier
- 0xff, 0xd8 | 0xff, 0xe0 | 2-bytes | "JFIF "
- def is_jpg(filename):
- data = open(filename,'rb').read(11)
- if data[:4] != 'xffxd8xffxe0': return False
- if data[6:] != 'JFIF ': return False
- return True
- from PIL import Image
- def is_jpg(filename):
- try:
- i=Image.open(filename)
- return i.format =='JPEG'
- except IOError:
- return False
- import imghdr
- image_type = imghdr.what(filename)
- if not image_type:
- print "error"
- else:
- print image_type
- image_type = imghdr.what(filename, incomming_image)
- ${h.form(h.url_for(action="save_image"), multipart=True)}
- Upload file: ${h.file("upload_file")} <br />
- ${h.submit("Submit", "Submit")}
- ${h.end_form()}
- def save_image(self):
- upload_file = request.POST["upload_file"]
- image_type = imghdr.what(upload_file.filename, upload_file.value)
- if not image_type:
- return "error"
- else:
- return image_type
- >>> import Image
- >>> im = Image.open("lena.ppm")
- >>> print im.format, im.size, im.mode
- import magic
- ms = magic.open(magic.MAGIC_NONE)
- ms.load()
- type = ms.file("/path/to/some/file")
- print type
- f = file("/path/to/some/file", "r")
- buffer = f.read(4096)
- f.close()
- type = ms.buffer(buffer)
- print type
- ms.close()
Add Comment
Please, Sign In to add comment