Add the various filters to the build tests

This commit is contained in:
Kovid Goyal 2016-05-09 09:26:26 +05:30
parent 8dbdcd8a8e
commit 6099728e00
2 changed files with 18 additions and 5 deletions

View File

@ -131,14 +131,15 @@ def test_image_formats():
# Must be run before QApplication is constructed # Must be run before QApplication is constructed
# Test that the image formats are available without a QApplication being # Test that the image formats are available without a QApplication being
# constructed # constructed
from calibre.utils.img import image_from_data, image_to_data from calibre.utils.img import image_from_data, image_to_data, test
data = I('blank.png', allow_user_override=False, data=True) data = I('blank.png', allow_user_override=False, data=True)
img = image_from_data(data) img = image_from_data(data)
image_from_data(P('catalog/mastheadImage.gif', allow_user_override=False, data=True)) image_from_data(P('catalog/mastheadImage.gif', allow_user_override=False, data=True))
for fmt in 'png bmp jpeg'.split(): for fmt in 'png bmp jpeg'.split():
d = image_to_data(img, fmt=fmt) d = image_to_data(img, fmt=fmt)
image_from_data(d) image_from_data(d)
# Run the imaging tests
test()
def test_qt(): def test_qt():
test_image_formats() test_image_formats()

View File

@ -287,6 +287,8 @@ def quantize(img, num_of_colors=256, dither=True):
img = blend_image(img) img = blend_image(img)
return imageops.quantize(img, num_of_colors, dither) return imageops.quantize(img, num_of_colors, dither)
# Optimization of images {{{
def run_optimizer(file_path, cmd, as_filter=False, input_data=None): def run_optimizer(file_path, cmd, as_filter=False, input_data=None):
file_path = os.path.abspath(file_path) file_path = os.path.abspath(file_path)
cwd = os.path.dirname(file_path) cwd = os.path.dirname(file_path)
@ -379,13 +381,14 @@ def encode_jpeg(file_path, quality=80):
if not img.save(buf, 'PPM'): if not img.save(buf, 'PPM'):
raise ValueError('Failed to export image to PPM') raise ValueError('Failed to export image to PPM')
return run_optimizer(file_path, cmd, as_filter=True, input_data=ReadOnlyFileBuffer(ba.data())) return run_optimizer(file_path, cmd, as_filter=True, input_data=ReadOnlyFileBuffer(ba.data()))
# }}}
def test(): def test(): # {{{
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre import CurrentDir from calibre import CurrentDir
from glob import glob from glob import glob
with TemporaryDirectory() as tdir, CurrentDir(tdir): with TemporaryDirectory() as tdir, CurrentDir(tdir):
shutil.copyfile(I('devices/kindle.jpg'), 'test.jpg') shutil.copyfile(I('devices/kindle.jpg', allow_user_override=False), 'test.jpg')
ret = optimize_jpeg('test.jpg') ret = optimize_jpeg('test.jpg')
if ret is not None: if ret is not None:
raise SystemExit('optimize_jpeg failed: %s' % ret) raise SystemExit('optimize_jpeg failed: %s' % ret)
@ -398,8 +401,16 @@ def test():
raise SystemExit('optimize_png failed: %s' % ret) raise SystemExit('optimize_png failed: %s' % ret)
if glob('*.bak'): if glob('*.bak'):
raise SystemExit('Spurious .bak files left behind') raise SystemExit('Spurious .bak files left behind')
img = image_from_data(I('devices/kindle.jpg', data=True, allow_user_override=False))
quantize(img)
oil_paint(img)
gaussian_sharpen(img)
gaussian_blur(img)
despeckle(img)
remove_borders(img)
# }}}
if __name__ == '__main__': if __name__ == '__main__': # {{{
args = sys.argv[1:] args = sys.argv[1:]
infile = args.pop(0) infile = args.pop(0)
img = image_from_data(lopen(infile, 'rb').read()) img = image_from_data(lopen(infile, 'rb').read())
@ -429,3 +440,4 @@ if __name__ == '__main__':
img = func(img, **kw) img = func(img, **kw)
with lopen(outf, 'wb') as f: with lopen(outf, 'wb') as f:
f.write(image_to_data(img, fmt=outf.rpartition('.')[-1])) f.write(image_to_data(img, fmt=outf.rpartition('.')[-1]))
# }}}