def assert_resize_image(self, filename, width, height, mode, expected_width=None, expected_height=None): if expected_width == None: expected_width = width if expected_height == None: expected_height = height filename = self.get_test_image_path(filename) ext = get_ext(filename) dst_filename = os.path.join(tempfile.gettempdir(), 'resized_image.%s' % ext) resize_image(filename, dst_filename, width, height, mode) self.assertTrue(os.path.isfile(dst_filename), 'file exists') self.assertTrue(is_image(dst_filename), 'is image file') # check image size size = get_image_size(dst_filename) self.assertEqual( size[0], expected_width, 'expected image width of %d, but was %d.' % (expected_width, size[0])) self.assertEqual( size[1], expected_height, 'expected image height of %d, but was %d.' % (expected_height, size[1]))
def test_should_return_image_size_in_pixels(self): for image in TEST_IMAGES: filename = self.get_test_image_path(image.filename) if get_ext(filename) != 'svg': image_size = get_image_size(filename) self.assertEqual(image_size[0], image.width, '%s: width should be %d but was %d' % (image.filename, image.width, image_size[0])) self.assertEqual(image_size[1], image.height, '%s: height should be %d but was %d' % (image.filename, image.height, image_size[1]))
def _image_from_media(media, width, height): """ Return image meta data from given media object. """ src = 'file:///C:/fake/%s' % media.filename ext = get_ext(media.filename) with open(media.original_path, 'rb') as f: content = f.read() return _image(src, ext, content, width, height)
def resize_image_copy_if_too_wide(self, filename): filename = self.get_test_image_path(filename) ext = get_ext(filename) dst_filename = os.path.join(tempfile.gettempdir(), 'resized_image_copy.%s' % ext) shutil.copyfile(filename, dst_filename) resized = resize_image_if_too_wide(dst_filename) width, height = get_image_size(dst_filename) os.remove(dst_filename) return (resized, width, height)
def _image_from_url(url, width, height): """ Return image meta information from given (external) image url. """ # download content from url content = requests.get(url, timeout=1000) if content == None: return None if content.status_code != 200: return None # generate filename based on given url url_parts = urlparse.urlparse(url) path = url_parts.path filename = os.path.basename(path) ext = get_ext(filename) src = 'file:///C:/fake/%s' % filename return _image(src, ext, content.content, width, height)
def handle(self, *args, **options): """ Run command. """ # determine images to work with process_documents = False if options.get('documents'): # documents only images = Media.objects.filter( is_image=False, has_preview=True ) process_documents = True elif options.get('images'): # images only images = Media.objects.filter( is_image=True ) else: # images and docuements images = Media.objects.filter( Q(is_image=True) | Q(has_preview=True) ) process_documents = True # specific pk? if options.get('pk'): images = images.filter(pk=options.get('pk')) # continuation if options.get('continue'): continue_filename = options.get('continue') else: continue_filename = None # specific shape? shape = options.get('shape') if shape: shapes = Media.get_shapes().keys() if shape not in shapes: print 'ERROR: Invalid shape name \'%s\'. Available shapes: %s.' % ( shape, ', '.join(['\'%s\'' % s for s in shapes]) ) return # re-evaluate PDF files for media in Media.objects.filter(is_image=False, has_preview=False): if get_ext(media.filename) == 'pdf': media.has_preview = True media.save() # delete deprecated image shapes and sizes self.delete_deprecated_image_sizes() self.delete_deprecated_image_shapes() # determine count of images to process... n = images.count() # process images print 'Processing images...Please Wait...' process_item = continue_filename is None for i, image in enumerate(images, start=1): # continuation? if continue_filename: if image.filename == continue_filename: process_item = True # generate image versions and re-generate meta data if process_item: image.upload(shape=shape) # verbose output print '%-70s [%d%%]' % ( image.filename, int(float(i) / float(n) * 100.0) ) print '%d images processed.' % n
def test_should_return_empty_extension_if_no_extension_is_present(self): self.assertEqual(get_ext('/home/cubane/'), '')
def test_should_return_last_extension_part_without_dot(self): self.assertEqual(get_ext('/home/cubane/test.backup.txt'), 'txt')
self.filename = filename self.width = width self.height = height TEST_IMAGES = [ TestImage('test.jpg', 512, 512), TestImage('test.png', 512, 512), TestImage('test-transparent.png', 589, 150), TestImage('test.tiff', 512, 512), TestImage('test.gif', 512, 512), TestImage('test.bmp', 512, 512), TestImage('test.svg', None, None) ] TEST_IMAGES_NO_SVG = [f for f in TEST_IMAGES if get_ext(f.filename) != 'svg'] class LibImageGetImageFittingTestCase(CubaneTestCase): """ cubane.lib.image.get_image_fitting() """ def test_landscape_fit(self): self.assertEqual((450, 300), get_image_fitting(600, 400, 1024, 300)) def test_portrait_fit(self): self.assertEqual((300, 200), get_image_fitting(600, 400, 300, 1024)) def test_zero_height_landscape_fit(self): self.assertEqual((1024, 0), get_image_fitting(600, 0, 1024, 300))