def upload_from_fs(fn, profile=None, label=None): """ Saves image from fn with TMP prefix and returns img_id. """ if not os.path.isfile(fn): raise ValueError('File is not exists: {}'.format(fn)) if profile is None: profile = 'default' conf = get_profile_configs(profile) with open(fn, 'rb') as f: if not is_image(f, types=conf['TYPES']): msg = (('Format of uploaded file "%(name)s" is not allowed. ' 'Allowed formats is: %(formats)s.') % {'name': fn, 'formats': ', '.join(map(lambda t: t.upper(), conf['TYPES']))}) raise RuntimeError(msg) t = adjust_image(f, max_size=conf['MAX_SIZE'], new_format=conf['FORMAT'], jpeg_quality=conf['JPEG_QUALITY'], fill=conf['FILL'], stretch=conf['STRETCH'], return_new_image=True) img_id = generate_img_id(profile, ext=image_get_format(f), label=label, tmp=True) relative_path = get_relative_path_from_img_id(img_id) full_path = media_path(relative_path) save_file(t, full_path) for v_conf in conf['VARIANTS']: v_label = v_conf['LABEL'] if not v_label: v_label = get_variant_label(v_conf) v_t = adjust_image(t, max_size=v_conf['MAX_SIZE'], new_format=v_conf['FORMAT'], jpeg_quality=v_conf['JPEG_QUALITY'], fill=v_conf['FILL'], stretch=v_conf['STRETCH'], return_new_image=True) v_relative_path = get_relative_path_from_img_id(img_id, variant_label=v_label, ext=image_get_format(v_t)) v_full_path = media_path(v_relative_path) save_file(v_t, v_full_path) return img_id
def _custom_upload(f, profile, label, conf): t = adjust_image(f, max_size=conf['MAX_SIZE'], new_format=conf['FORMAT'], jpeg_quality=conf['JPEG_QUALITY'], fill=conf['FILL'], stretch=conf['STRETCH'], return_new_image=True) img_id = generate_img_id(profile, ext=image_get_format(f), label=label, tmp=True) relative_path = get_relative_path_from_img_id(img_id) full_path = media_path(relative_path) save_file(t, full_path) for v_conf in conf['VARIANTS']: v_label = v_conf['LABEL'] if not v_label: v_label = get_variant_label(v_conf) v_t = adjust_image(t, max_size=v_conf['MAX_SIZE'], new_format=v_conf['FORMAT'], jpeg_quality=v_conf['JPEG_QUALITY'], fill=v_conf['FILL'], stretch=v_conf['STRETCH'], return_new_image=True) v_relative_path = get_relative_path_from_img_id( img_id, variant_label=v_label, ext=image_get_format(v_t)) v_full_path = media_path(v_relative_path) save_file(v_t, v_full_path) return img_id
def test_size_not_fill_not_stretch(self): self.make_files_for_images() self.assertFalse(adjust_image(self.f_200x200_png, (200, 200))) self.assertFalse(adjust_image(self.f_300x300_png, (300, 400))) self.assertTrue(adjust_image(self.f_400x200_jpeg, (200, 200))) self.assertImageSizeIs(self.f_400x200_jpeg, (200, 100)) self.assertTrue(adjust_image(self.f_200x400_jpeg, (200, 200))) self.assertImageSizeIs(self.f_200x400_jpeg, (100, 200))
def test_size_not_fill_stretch(self): self.make_files_for_images() self.assertTrue( adjust_image(self.f_200x200_jpeg, (200, 200), stretch=True)) self.assertFalse( adjust_image(self.f_300x300_jpeg, (300, 400), stretch=True, force_jpeg_save=False)) self.assertTrue( adjust_image(self.f_400x200_jpeg, (500, 500), stretch=True)) self.assertImageSizeIs(self.f_400x200_jpeg, (500, 250))
def test_size_fill_stretch(self): self.make_files_for_images() self.assertFalse( adjust_image(self.f_200x200_png, (200, 200), fill=True, stretch=True)) self.assertTrue( adjust_image(self.f_300x300_jpeg, (400, 350), fill=True, stretch=True)) self.assertImageSizeIs(self.f_300x300_jpeg, (400, 350))
def test_autosize(self): self.make_files_for_images() self.assertTrue( adjust_image(self.f_200x200_jpeg, (150, None), fill=True, stretch=True)) self.assertImageSizeIs(self.f_200x200_jpeg, (150, 150)) self.assertTrue( adjust_image(self.f_200x400_jpeg, (None, 300), fill=True, stretch=True)) self.assertImageSizeIs(self.f_200x400_jpeg, (150, 300))
def tests(): img_200x200_cmyk = create_test_image(200, 200, c="CMYK") f_200x200_jpeg_cmyk = get_img_file(img_200x200_cmyk) t = adjust_image(f_200x200_jpeg_cmyk, return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, "RGB") img_200x200_p = create_test_image(200, 200, c="P") f_200x200_png_p = get_img_file(img_200x200_p, img_format="PNG") t = adjust_image(f_200x200_png_p, new_format="JPEG", return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, "RGB")
def tests(): img_200x200_cmyk = create_test_image(200, 200, c='CMYK') f_200x200_jpeg_cmyk = get_img_file(img_200x200_cmyk) t = adjust_image(f_200x200_jpeg_cmyk, return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, 'RGB') img_200x200_p = create_test_image(200, 200, c='P') f_200x200_png_p = get_img_file(img_200x200_p, img_format='PNG') t = adjust_image(f_200x200_png_p, new_format='JPEG', return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, 'RGB')
def test_uploaded_file(self): self.make_files_for_images() uf = UploadedFile(file=self.f_200x200_jpeg, name='200x200.jpeg', content_type='image/jpeg', size=len(self.f_200x200_jpeg.getvalue())) self.assertTrue(adjust_image(uf, (120, 120), new_format='PNG')) self.assertImageSizeIs(uf.file, (120, 120)) self.assertEqual(uf.content_type, 'image/png')
def test_uploaded_file(self): self.make_files_for_images() uf = UploadedFile( file=self.f_200x200_jpeg, name="200x200.jpeg", content_type="image/jpeg", size=len(self.f_200x200_jpeg.getvalue()), ) self.assertTrue(adjust_image(uf, (120, 120), new_format="PNG")) self.assertImageSizeIs(uf.file, (120, 120)) self.assertEqual(uf.content_type, "image/png")
def test_new_image(self): self.make_files_for_images() self.assertIsInstance( adjust_image(self.f_200x200_jpeg, return_new_image=True), (cStringIO.InputType, cStringIO.OutputType) )
return send_json(result) try: profile = request.POST.get('profile', 'default') conf = get_profile_configs(profile) except ValueError, e: result['errors'].append(unicode(e)) return send_json(result) for i in xrange(min(len(files), dju_settings.DJU_IMG_UPLOAD_MAX_FILES)): f = files[i] if not is_image(f, types=conf['TYPES']): result['errors'].append( unicode(ERROR_MESSAGES['wrong_file_format']) % {'name': f.name, 'formats': ', '.join(map(lambda t: t.upper(), conf['TYPES']))} ) continue adjust_image(f, max_size=conf['MAX_SIZE'], new_format=conf['FORMAT'], jpeg_quality=conf['JPEG_QUALITY'], fill=conf['FILL'], stretch=conf['STRETCH']) img_id = generate_img_id(profile, ext=image_get_format(f), label=request.POST.get('label'), tmp=True) relative_path = get_relative_path_from_img_id(img_id) full_path = media_path(relative_path) save_file(f, full_path) data = { 'url': settings.MEDIA_URL + relative_path, 'rel_url': relative_path, 'img_id': img_id, 'variants': {}, } for v_conf in conf['VARIANTS']: label = v_conf['LABEL'] if not label: label = get_variant_label(v_conf)
def test_adjust_image_invalid_new_format(self): self.make_files_for_images() with self.assertRaises(RuntimeError): adjust_image(adjust_image(self.f_200x200_jpeg, new_format="test"))
conf = get_profile_configs(profile) except ValueError, e: result["errors"].append(unicode(e)) return send_json(result) for i in xrange(min(len(files), dju_settings.DJU_IMG_UPLOAD_MAX_FILES)): f = files[i] if not is_image(f, types=conf["TYPES"]): result["errors"].append( unicode(ERROR_MESSAGES["wrong_file_format"]) % {"name": f.name, "formats": ", ".join(map(lambda t: t.upper(), conf["TYPES"]))} ) continue adjust_image( f, max_size=conf["MAX_SIZE"], new_format=conf["FORMAT"], jpeg_quality=conf["JPEG_QUALITY"], fill=conf["FILL"], stretch=conf["STRETCH"], ) img_id = generate_img_id(profile, ext=image_get_format(f), label=request.POST.get("label"), tmp=True) relative_path = get_relative_path_from_img_id(img_id) full_path = media_path(relative_path) save_file(f, full_path) data = {"url": settings.MEDIA_URL + relative_path, "rel_url": relative_path, "img_id": img_id, "variants": {}} for v_conf in conf["VARIANTS"]: label = v_conf["LABEL"] if not label: label = get_variant_label(v_conf) v_f = adjust_image( f, max_size=v_conf["MAX_SIZE"],
def test_size_not_fill_stretch(self): self.make_files_for_images() self.assertTrue(adjust_image(self.f_200x200_jpeg, (200, 200), stretch=True)) self.assertFalse(adjust_image(self.f_300x300_jpeg, (300, 400), stretch=True, force_jpeg_save=False)) self.assertTrue(adjust_image(self.f_400x200_jpeg, (500, 500), stretch=True)) self.assertImageSizeIs(self.f_400x200_jpeg, (500, 250))
def test_format(self): self.make_files_for_images() self.assertTrue( adjust_image(self.f_200x200_jpeg, (200, 200), new_format='PNG')) self.assertImageFormat(self.f_200x200_jpeg, 'PNG')
def test_size_fill_stretch(self): self.make_files_for_images() self.assertFalse(adjust_image(self.f_200x200_png, (200, 200), fill=True, stretch=True)) self.assertTrue(adjust_image(self.f_300x300_jpeg, (400, 350), fill=True, stretch=True)) self.assertImageSizeIs(self.f_300x300_jpeg, (400, 350))
def test_format(self): self.make_files_for_images() self.assertTrue(adjust_image(self.f_200x200_jpeg, (200, 200), new_format="PNG")) self.assertImageFormat(self.f_200x200_jpeg, "PNG")
def test_autosize(self): self.make_files_for_images() self.assertTrue(adjust_image(self.f_200x200_jpeg, (150, None), fill=True, stretch=True)) self.assertImageSizeIs(self.f_200x200_jpeg, (150, 150)) self.assertTrue(adjust_image(self.f_200x400_jpeg, (None, 300), fill=True, stretch=True)) self.assertImageSizeIs(self.f_200x400_jpeg, (150, 300))
def test_adjust_image_invalid_new_format(self): self.make_files_for_images() with self.assertRaises(RuntimeError): adjust_image(adjust_image(self.f_200x200_jpeg, new_format='test'))
def test_new_image(self): self.make_files_for_images() self.assertIsInstance( adjust_image(self.f_200x200_jpeg, return_new_image=True), (cStringIO.InputType, cStringIO.OutputType))