def handle(self, *args, **options): from django.conf import settings from users.tasks import delete_photo, resize_photo if not os.path.isdir(settings.USERPICS_PATH): sys.exit("Can't read pics path: %s" % settings.USERPICS_PATH) converted = 0 for root, dirs, files in os.walk(settings.USERPICS_PATH): for file in files: if file[-4:] in ('.jpg', '.gif'): name, _ = os.path.splitext(file) oldfile = "%s/%s" % (root, file) newfile = "%s/%s.png" % (root, name) if os.path.isfile(newfile): delete_photo(oldfile) else: resize_photo(oldfile, newfile) converted += 1 if converted % 100 == 0: print "Converted %s images..." % converted elif file.endswith('.png') or file.endswith('__unconverted'): pass else: print "Not sure what to do with: %s" % file print "All done."
def test_resize_photo_poorly(): """If we attempt to set the src/dst, we do nothing.""" somepic = "%s/img/tab-mozilla.png" % settings.MEDIA_ROOT src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False) shutil.copyfile(somepic, src.name) src_image = Image.open(src.name) eq_(src_image.size, (82, 31)) resize_photo(src.name, src.name) # assert nothing happenned src_image = Image.open(src.name) eq_(src_image.size, (82, 31))
def test_resize_photo_poorly(): """If we attempt to set the src/dst, we do nothing.""" somepic = get_image_path('mozilla.png') src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False, dir=settings.TMP_PATH) shutil.copyfile(somepic, src.name) src_image = Image.open(src.name) eq_(src_image.size, (339, 128)) resize_photo(src.name, src.name) # assert nothing happenned src_image = Image.open(src.name) eq_(src_image.size, (339, 128))
def test_resize_photo(): somepic = "%s/img/tab-mozilla.png" % settings.MEDIA_ROOT src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False) dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png") # resize_photo removes the original shutil.copyfile(somepic, src.name) src_image = Image.open(src.name) eq_(src_image.size, (82, 31)) resize_photo(src.name, dest.name) # Image is smaller than 200x200 so it should stay the same. dest_image = Image.open(dest.name) eq_(dest_image.size, (82, 31)) assert not os.path.exists(src.name)
def test_resize_photo(): somepic = get_image_path('sunbird-small.png') src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", delete=False, dir=settings.TMP_PATH) dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png", dir=settings.TMP_PATH) # resize_photo removes the original shutil.copyfile(somepic, src.name) src_image = Image.open(src.name) eq_(src_image.size, (64, 64)) resize_photo(src.name, dest.name, locally=True) # Image is smaller than 200x200 so it should stay the same. dest_image = Image.open(dest.name) eq_(dest_image.size, (64, 64)) assert not os.path.exists(src.name)