def handleHeader(self, key, value): if key == "Content-Type" and value in [ "image/jpeg", "image/gif", "image/png" ]: self.image_parser = Parser() if key == "Content-Length" and self.image_parser: pass else: ProxyClient.handleHeader(self, key, value)
def invert_images(self): if self.reply.response_headers.get('Content-Type', None) \ in ["image/jpeg", "image/gif", "image/png"]: image_parser = Parser() image_parser.feed(self.reply.data) image = image_parser.close() try: format = image.format image = image.rotate(180) s = StringIO() image.save(s, format) self.reply.data = s.getvalue() s.close() print 'Image!' except Exception as e: print e
def handleHeader(self, key, value): if key.lower() == "content-type": if value in ["image/jpeg", "image/gif", "image/png"]: self.image_parser = Parser() self.ctype_received = True # content-type must be known before handling header if self.clength != 0 and not self.image_parser: # processes a content-length, if that was a prior header print "Header: Content-Length ", self.clength ProxyClient.handleHeader(self, "Content-Length", self.clength) if key.lower() == "content-length": print "Handling content-length" if self.image_parser: # don't set content-length yet if an image-file. print "CHeader1: ", key, " ", value # LEAVES CONTENT-LENGTH UNSET! elif self.ctype_received: # a bit ugly. Just pass the header forward, if not an image c-length print "CHeader2: ", key, " ", value ProxyClient.handleHeader(self, key, value) else: # if content-type has not been processed yet print "old-clength ", value self.clength = value else: print "Header: ", key, " ", value ProxyClient.handleHeader(self, key, value)
def handleHeader(self, key, value): if key == "Content-Type" and value in ["image/jpeg", "image/gif", "image/png"]: self.image_parser = Parser() if key == "Content-Length" and self.image_parser: pass else: ProxyClient.handleHeader(self, key, value)
def _apply(self, source, destination): parser = ImageFileParser() parser.feed(source.file.read()) source_image = parser.close() crop_box = ImageResize.cropbox(source_image.size, (self.width, self.height)) image = source_image.crop(crop_box) image = image.resize((self.width, self.height), Image.BICUBIC) buffer = StringIO() image.save(buffer, self.format) buffer.reset() if destination.file: write = destination.file.replace else: write = destination.file.put write(buffer, content_type="image/%s" % self.format) destination.save() return destination
class InterceptingProxyClient(ProxyClient): def __init__(self, *args, **kwargs): ProxyClient.__init__(self, *args, **kwargs) self.image_parser = None def handleHeader(self, key, value): if key == "Content-Type" and value in [ "image/jpeg", "image/gif", "image/png" ]: self.image_parser = Parser() if key == "Content-Length" and self.image_parser: pass else: ProxyClient.handleHeader(self, key, value) def handleEndHeaders(self): if self.image_parser: pass #Need to calculate and send Content-Length first else: ProxyClient.handleEndHeaders(self) def handleResponsePart(self, buffer): if self.image_parser: self.image_parser.feed(buffer) else: ProxyClient.handleResponsePart(self, buffer) def handleResponseEnd(self): if self.image_parser: image = self.image_parser.close() try: format = image.format image = image.rotate(180) s = StringIO() image.save(s, format) buffer = s.getvalue() except: buffer = "" ProxyClient.handleHeader(self, "Content-Length", len(buffer)) ProxyClient.handleEndHeaders(self) ProxyClient.handleResponsePart(self, buffer) ProxyClient.handleResponseEnd(self)
def _apply(self, source, destination): parser = ImageFileParser() parser.feed(source.file.read()) source_image = parser.close() crop_box = self._get_crop_box(source_image.size, (self.width, self.height)) image = source_image.crop(crop_box) image = image.resize((self.width, self.height), Image.ANTIALIAS) buffer = StringIO() image.save(buffer, self.format) buffer.reset() if destination.file: write = destination.file.replace else: write = destination.file.put write(buffer, content_type = 'image/%s' % self.format) destination.save() return destination
class InterceptingProxyClient(ProxyClient): def __init__(self, *args, **kwargs): ProxyClient.__init__(self, *args, **kwargs) self.image_parser = None def handleHeader(self, key, value): if key == "Content-Type" and value in ["image/jpeg", "image/gif", "image/png"]: self.image_parser = Parser() if key == "Content-Length" and self.image_parser: pass else: ProxyClient.handleHeader(self, key, value) def handleEndHeaders(self): if self.image_parser: pass #Need to calculate and send Content-Length first else: ProxyClient.handleEndHeaders(self) def handleResponsePart(self, buffer): if self.image_parser: self.image_parser.feed(buffer) else: ProxyClient.handleResponsePart(self, buffer) def handleResponseEnd(self): if self.image_parser: image = self.image_parser.close() try: format = image.format image = image.rotate(180) s = StringIO() image.save(s, format) buffer = s.getvalue() except: buffer = "" ProxyClient.handleHeader(self, "Content-Length", len(buffer)) ProxyClient.handleEndHeaders(self) ProxyClient.handleResponsePart(self, buffer) ProxyClient.handleResponseEnd(self)
def to_python(self, data): """ Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports). """ f = super(ImageField, self).to_python(data) if f is None: return None buffer = StringIO() for chunk in data.chunks(): buffer.write(chunk) buffer.reset() try: parser = ImageFileParser() parser.feed(buffer.read()) parser.close() except Exception: raise ValidationError(self.error_messages['invalid_image']) self.buffer = buffer self.content_type = f.content_type return data
def to_python(self, data): """ Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports). """ f = super(ImageField, self).to_python(data) if f is None: return None buffer = StringIO() for chunk in data.chunks(): buffer.write(chunk) buffer.reset() try: parser = ImageFileParser() parser.feed(buffer.read()) parser.close() except Exception: raise ValidationError(self.error_messages["invalid_image"]) self.buffer = buffer self.content_type = f.content_type return data
def read_image_file(file): parser = ImageFileParser() parser.feed(file.read()) return parser.close()
class InterceptingProxyClient(ProxyClient): def __init__(self, *args, **kwargs): ProxyClient.__init__(self, *args, **kwargs) self.image_parser = None self.ctype_received = False self.clength = 0 def handleHeader(self, key, value): if key.lower() == "content-type": if value in ["image/jpeg", "image/gif", "image/png"]: self.image_parser = Parser() self.ctype_received = True # content-type must be known before handling header if self.clength != 0 and not self.image_parser: # processes a content-length, if that was a prior header print "Header: Content-Length ", self.clength ProxyClient.handleHeader(self, "Content-Length", self.clength) if key.lower() == "content-length": print "Handling content-length" if self.image_parser: # don't set content-length yet if an image-file. print "CHeader1: ", key, " ", value # LEAVES CONTENT-LENGTH UNSET! elif self.ctype_received: # a bit ugly. Just pass the header forward, if not an image c-length print "CHeader2: ", key, " ", value ProxyClient.handleHeader(self, key, value) else: # if content-type has not been processed yet print "old-clength ", value self.clength = value else: print "Header: ", key, " ", value ProxyClient.handleHeader(self, key, value) def handleEndHeaders(self): if self.image_parser: pass #Need to calculate and send Content-Length first else: ProxyClient.handleEndHeaders(self) def handleResponsePart(self, buffer): if self.image_parser: self.image_parser.feed(buffer) else: ProxyClient.handleResponsePart(self, buffer) def handleResponseEnd(self): if self.image_parser: print "imageparsing ", compress_rate try: image = self.image_parser.close() except IOError as (error): print "I/O error: ", error print self.image_parser try: # do the compression format = image.format newsize = int(image.size[0] * compress_rate) , int(image.size[1] * compress_rate) image.thumbnail(newsize) # likely, image sizing like this does not work s = StringIO() image.save(s, format) buffer = s.getvalue() except NameError as (error): print "in exception: ", error # send Original C-length if just queryed for header buffer = " " *(int(self.clength)) print "Header: Content-Lengths ", len(buffer) ProxyClient.handleHeader(self, "Content-Length", len(buffer)) # all headers received and processed ProxyClient.handleEndHeaders(self) ProxyClient.handleResponsePart(self, buffer) ProxyClient.handleResponseEnd(self)