def clean_upload_data(data): image = data['image'] image.seek(0) try: pil_image = PIL.Image.open(image) except IOError as e: if e.errno: error_msg = force_text(e) else: error_msg = u"Invalid or unsupported image file" raise forms.ValidationError({"image": [error_msg]}) else: extension = get_image_extension(pil_image) upload_to = data['upload_to'] or None folder_path = get_upload_foldername(image.name, upload_to=upload_to) (w, h) = (orig_w, orig_h) = pil_image.size sizes = data.get('sizes') if sizes: (min_w, min_h) = get_min_size(sizes) if (orig_w < min_w or orig_h < min_h): raise forms.ValidationError({ "image": [(u"Image must be at least %(min_w)sx%(min_h)s " u"(%(min_w)s pixels wide and %(min_h)s pixels high). " u"The image you uploaded was %(orig_w)sx%(orig_h)s pixels.") % { "min_w": min_w, "min_h": min_h, "orig_w": orig_w, "orig_h": orig_h }] }) if w <= 0: raise forms.ValidationError( {"image": [u"Invalid image: width is %d" % w]}) elif h <= 0: raise forms.ValidationError( {"image": [u"Invalid image: height is %d" % h]}) # File is good, get rid of the tmp file orig_file_path = os.path.join(folder_path, 'original' + extension) image.seek(0) md5_hash = hashlib.md5() default_storage.save(orig_file_path, image) with default_storage.open(orig_file_path) as f: md5_hash.update(f.read()) f.seek(0) data['image'] = f data['md5'] = md5_hash.hexdigest() return data
def clean_upload_data(data): image = data['image'] image.seek(0) try: pil_image = PIL.Image.open(image) except IOError as e: if e.errno: error_msg = force_text(e) else: error_msg = u"Invalid or unsupported image file" raise forms.ValidationError({"image": [error_msg]}) else: extension = get_image_extension(pil_image) upload_to = data['upload_to'] or None folder_path = get_upload_foldername(image.name, upload_to=upload_to) (w, h) = (orig_w, orig_h) = pil_image.size sizes = data.get('sizes') if sizes: (min_w, min_h) = get_min_size(sizes) if (orig_w < min_w or orig_h < min_h): raise forms.ValidationError({"image": [( u"Image must be at least %(min_w)sx%(min_h)s " u"(%(min_w)s pixels wide and %(min_h)s pixels high). " u"The image you uploaded was %(orig_w)sx%(orig_h)s pixels.") % { "min_w": min_w, "min_h": min_h, "orig_w": orig_w, "orig_h": orig_h }]}) if w <= 0: raise forms.ValidationError({"image": [u"Invalid image: width is %d" % w]}) elif h <= 0: raise forms.ValidationError({"image": [u"Invalid image: height is %d" % h]}) # File is good, get rid of the tmp file orig_file_path = os.path.join(folder_path, 'original' + extension) image.seek(0) image_contents = image.read() with open(os.path.join(settings.MEDIA_ROOT, orig_file_path), 'wb+') as f: f.write(image_contents) md5_hash = hashlib.md5() md5_hash.update(image_contents) data['md5'] = md5_hash.hexdigest() data['image'] = open(os.path.join(settings.MEDIA_ROOT, orig_file_path), mode='rb') return data
def create_image(self, output_filename, width, height): from cropduster.utils import process_image, get_image_extension temp_file = tempfile.NamedTemporaryFile(suffix=get_image_extension(self.image), delete=False) temp_filename = temp_file.name with open(self.image.filename, mode='rb') as f: temp_file.write(f.read()) temp_file.seek(0) image = PIL.Image.open(temp_filename) crop_args = self.box.as_tuple() def crop_and_resize_callback(im): from cropduster.utils import smart_resize im = im.crop(crop_args) return smart_resize(im, final_w=width, final_h=height) new_image = process_image(image, output_filename, crop_and_resize_callback) new_image.crop = self temp_file.close() os.unlink(temp_filename) return new_image
def create_image(self, output_filename, width=None, height=None, max_w=None, max_h=None): from cropduster.exceptions import CropDusterResizeException from cropduster.utils import process_image, get_image_extension new_w, new_h = self.box.size if new_w < width or new_h < height: raise CropDusterResizeException( u"Crop box (%dx%d) is too small for resize to (%dx%d)" % (new_w, new_h, width, height)) # Scale our initial width and height based on the max_w and max_h max_scales = [] if max_w and max_w < width: max_scales.append(max_w / width) if max_h and max_h < height: max_scales.append(max_h / height) if max_scales: max_scale = min(max_scales) width = int(round(width * max_scale)) height = int(round(height * max_scale)) temp_file = tempfile.NamedTemporaryFile(suffix=get_image_extension(self.image), delete=False) temp_filename = temp_file.name with open(self.image.filename, mode='rb') as f: temp_file.write(f.read()) temp_file.seek(0) image = PIL.Image.open(temp_filename) crop_args = self.box.as_tuple() def crop_and_resize_callback(im): from cropduster.utils import smart_resize im = im.crop(crop_args) return smart_resize(im, final_w=width, final_h=height) new_image = process_image(image, output_filename, crop_and_resize_callback) new_image.crop = self temp_file.close() os.unlink(temp_filename) return new_image
def create_image(self, output_filename, width, height): from cropduster.utils import process_image, get_image_extension temp_file = tempfile.NamedTemporaryFile(suffix=get_image_extension(self.image), delete=False) temp_filename = temp_file.name with default_storage.open(self.image.filename, mode='rb') as f: temp_file.write(f.read()) temp_file.seek(0) image = PIL.Image.open(temp_filename) image.filename = self.image.filename crop_args = self.box.as_tuple() def crop_and_resize_callback(im): from cropduster.utils import smart_resize im = im.crop(crop_args) return smart_resize(im, final_w=width, final_h=height) new_image = process_image(image, output_filename, crop_and_resize_callback) new_image.crop = self temp_file.close() os.unlink(temp_filename) return new_image
def test_get_image_extension(self): from cropduster.utils import get_image_extension tmp_jpg_bad_ext_pdf = tempfile.NamedTemporaryFile(suffix='.pdf') tmp_png_bad_ext_jpg = tempfile.NamedTemporaryFile(suffix='.png') with open(os.path.join(self.TEST_IMG_DIR, 'img.jpg'), mode='rb') as f: tmp_jpg_bad_ext_pdf.write(f.read()) tmp_jpg_bad_ext_pdf.seek(0) with open(os.path.join(self.TEST_IMG_DIR, 'img.png'), mode='rb') as f: tmp_png_bad_ext_jpg.write(f.read()) tmp_png_bad_ext_jpg.seek(0) imgs = [ (self._get_img('img.jpg'), '.jpg'), (self._get_img('img.png'), '.png'), (self._get_img('animated.gif'), '.gif'), (Image.open(tmp_jpg_bad_ext_pdf.name), '.jpg'), (Image.open(tmp_png_bad_ext_jpg.name), '.png'), ] for img, ext in imgs: self.assertEqual(get_image_extension(img), ext)