def resizeAndKeepExif(fullpath, storeOriginalFilename, storeExifFromOriginal, jpgHighQualityChromaSampling): '''The filenames tell us what action to take. File named a__MARKAS__50%.jpg becomes a.jpg resized by 50%.''' trace(fullpath) pathWithoutCategory, category = img_utils.getMarkFromFilename(fullpath) assertTrue(not files.exists(pathWithoutCategory), 'file already exists ' + pathWithoutCategory) if category == '100%': files.move(fullpath, pathWithoutCategory, False) needTransferTags = False fileWasMovedNotCopied = True else: # tell convertOrResizeImage not to transfer the tags yet, we'll do that ourselves. img_convert_resize.convertOrResizeImage(fullpath, pathWithoutCategory, resizeSpec=category, jpgQuality=None, doTransferMostUsefulExifTags=False) needTransferTags = True fileWasMovedNotCopied = False assertTrue(files.exists(pathWithoutCategory)) try: if storeOriginalFilename: img_utils.stampJpgWithOriginalFilename(pathWithoutCategory, files.getname(pathWithoutCategory)) if storeExifFromOriginal and needTransferTags: img_utils.transferMostUsefulExifTags(fullpath, pathWithoutCategory) except img_utils.PythonImgExifError as e: # upon exception, move it back to the original spot. trace('Exif exception occurred ' + str(e) + 'for file ' + fullpath) if fileWasMovedNotCopied: assertTrue(not files.exists(fullpath)) files.move(pathWithoutCategory, fullpath, False) else: assertTrue(files.exists(fullpath)) softDeleteFile(pathWithoutCategory)
def simpleResize(root, recursive, inputformat='png', outputformat='jpg', resizeSpec='100%', jpgQuality=None, addPrefix='', softDeleteOriginals=False): # If we didn't call list(files) to first freeze the list of files to process, we would encounter as input the files we just created. fnGetFiles = files.recursefiles if recurse else files.listfiles allfiles = list(fnGetFiles(root, allowedexts=[inputformat])) for fullpath, short in allfiles: if files.getext(fullpath) == inputformat: trace(short) outname = files.getparent(fullpath) + files.sep + addPrefix + files.splitext(short)[0] + '.' + outputformat if files.exists(outname): trace('already exists', outname) else: img_convert_resize.convertOrResizeImage(fullpath, outname, resizeSpec=resizeSpec, jpgQuality=jpgQuality) assertTrue(files.exists(outname)) if softDeleteOriginals: softDeleteFile(fullpath)
def resizeAndKeepExif(fullpath, storeOriginalFilename, storeExifFromOriginal, jpgHighQualityChromaSampling): '''The filenames tell us what action to take. File named a__MARKAS__50%.jpg becomes a.jpg resized by 50%.''' trace(fullpath) pathWithoutCategory, category = img_utils.getMarkFromFilename(fullpath) assertTrue(not files.exists(pathWithoutCategory), 'file already exists ' + pathWithoutCategory) if category == '100%': files.move(fullpath, pathWithoutCategory, False) needTransferTags = False fileWasMovedNotCopied = True else: ret = img_convert_resize.convertOrResizeImage(fullpath, pathWithoutCategory, resizeSpec=category, jpgQuality=None) needTransferTags = ret != img_convert_resize.ConvertResult.SuccessCopied fileWasMovedNotCopied = False assertTrue(files.exists(pathWithoutCategory)) try: if storeOriginalFilename: img_utils.stampJpgWithOriginalFilename(pathWithoutCategory, files.getname(pathWithoutCategory)) if storeExifFromOriginal and needTransferTags: img_utils.transferMostUsefulExifTags(fullpath, pathWithoutCategory) except img_utils.PythonImgExifError as e: # upon exception, move it back to the original spot. trace('Exif exception occurred ' + str(e) + 'for file ' + fullpath) if fileWasMovedNotCopied: assertTrue(not files.exists(fullpath)) files.move(pathWithoutCategory, fullpath, False) else: assertTrue(files.exists(fullpath)) softDeleteFile(pathWithoutCategory)
def testJpgQualities(tmpDir, testImage): # simply write several jpgs at different qualities, and make sure the file sizes are as expected. tmpDir = files.join(tmpDir, 'testJpgQuality') files.makedirs(tmpDir) testImage.save(files.join(tmpDir, 'start.bmp')) qualities = [100, 90, 60, 10] for qual in qualities: img_convert_resize.convertOrResizeImage(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'q%d.jpg'%qual), jpgQuality=qual) expectedSizes = '''q10.jpg|993 q100.jpg|15536 q60.jpg|5120 q90.jpg|9366 start.bmp|43254'''.replace('\r\n', '\n') resultSizes = '\n'.join([short + '|' + str(files.getsize(file)) for file, short in sorted(files.listfiles(tmpDir))]) assertEq(expectedSizes, resultSizes)
def testJpgQualities(tmpDir, testImage): # simply write several jpgs at different qualities, and make sure the file sizes are as expected. tmpDir = files.join(tmpDir, 'testJpgQuality') files.makedirs(tmpDir) testImage.save(files.join(tmpDir, 'start.bmp')) qualities = [100, 90, 60, 10] for qual in qualities: img_convert_resize.convertOrResizeImage(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'q%d.jpg'%qual), jpgQuality=qual) expectedSizes = '''q10.jpg|993 q100.jpg|15580 q60.jpg|5120 q90.jpg|9406 start.bmp|43254'''.replace('\r\n', '\n') resultSizes = '\n'.join([short + '|' + str(files.getsize(file)) for file, short in sorted(files.listfiles(tmpDir))]) assertEq(expectedSizes, resultSizes)
def testCombinatoricImageConversion(tmpDir, testImage): # go from each format to every other format! # note: bmp should be first in the list formats = ['bmp', 'png', 'jpg', 'webp'] jpgQuality = 100 if not getInputBool('run combinatoricImageConversionTest?'): return for format in formats: startfile = files.join(tmpDir, 'start.' + format) if format == 'bmp': testImage.save(startfile) else: img_convert_resize.convertOrResizeImage(files.join(tmpDir, 'start.bmp'), startfile, jpgQuality=jpgQuality) for outformat in formats: if outformat != format: outfile = startfile + '.' + outformat assertTrue(not files.exists(outfile)) img_convert_resize.convertOrResizeImage(startfile, outfile, jpgQuality=jpgQuality) assertTrue(files.exists(outfile)) expectedSizes = '''start.bmp|43254 start.bmp.jpg|15536 start.bmp.png|39430 start.bmp.webp|14468 start.jpg|15536 start.jpg.bmp|43254 start.jpg.png|39500 start.jpg.webp|14468 start.png|39430 start.png.bmp|43254 start.png.jpg|15536 start.png.webp|14468 start.webp|14468 start.webp.bmp|43254 start.webp.jpg|15536 start.webp.png|22366'''.replace('\r\n', '\n') resultSizes = '\n'.join([short + '|' + str(files.getsize(file)) for file, short in sorted(files.listfiles(tmpDir)) if short.startswith('start')]) assertEq(expectedSizes, resultSizes) # are bmps equivalent assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'start.png.bmp'))) assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'start.webp.bmp'))) # are jpgs equivalent assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp.jpg'), files.join(tmpDir, 'start.jpg'))) assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp.jpg'), files.join(tmpDir, 'start.png.jpg'))) assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp.jpg'), files.join(tmpDir, 'start.webp.jpg'))) # are webps equivalent assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp.webp'), files.join(tmpDir, 'start.png.webp'))) assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp.webp'), files.join(tmpDir, 'start.webp'))) # are pngs equivalent assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp.png'), files.join(tmpDir, 'start.png'))) # png written by dwebp is different, but it should still roundtrip img_convert_resize.convertOrResizeImage(files.join(tmpDir, 'start.webp.png'), files.join(tmpDir, 'start.webp.png.bmp')) assertTrue(files.fileContentsEqual(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'start.webp.png.bmp')))
def testCombinatoricImageConversion(tmpDir, testImage): # go from each format to every other format! # note: bmp should be first in the list formats = ['bmp', 'png', 'jpg', 'webp'] jpgQuality = 100 if not getInputBool('run combinatoricImageConversionTest?'): return for format in formats: startfile = files.join(tmpDir, 'start.' + format) if format == 'bmp': testImage.save(startfile) else: img_convert_resize.convertOrResizeImage(files.join( tmpDir, 'start.bmp'), startfile, jpgQuality=jpgQuality) for outformat in formats: if outformat != format: outfile = startfile + '.' + outformat assertTrue(not files.exists(outfile)) img_convert_resize.convertOrResizeImage(startfile, outfile, jpgQuality=jpgQuality) assertTrue(files.exists(outfile)) expectedSizes = '''start.bmp|43254 start.bmp.jpg|15536 start.bmp.png|39430 start.bmp.webp|14454 start.jpg|15536 start.jpg.bmp|43254 start.jpg.png|39483 start.jpg.webp|14454 start.png|39430 start.png.bmp|43254 start.png.jpg|15536 start.png.webp|14454 start.webp|14454 start.webp.bmp|43254 start.webp.jpg|15536 start.webp.png|22366'''.replace('\r\n', '\n') resultSizes = '\n'.join([ short + '|' + str(files.getsize(file)) for file, short in sorted(files.listfiles(tmpDir)) if short.startswith('start') ]) assertEq(expectedSizes, resultSizes) # are bmps equivalent assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'start.png.bmp'))) assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'start.webp.bmp'))) # are jpgs equivalent assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp.jpg'), files.join(tmpDir, 'start.jpg'))) assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp.jpg'), files.join(tmpDir, 'start.png.jpg'))) assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp.jpg'), files.join(tmpDir, 'start.webp.jpg'))) # are webps equivalent assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp.webp'), files.join(tmpDir, 'start.png.webp'))) assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp.webp'), files.join(tmpDir, 'start.webp'))) # are pngs equivalent assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp.png'), files.join(tmpDir, 'start.png'))) # png written by dwebp is different, but it should still roundtrip img_convert_resize.convertOrResizeImage( files.join(tmpDir, 'start.webp.png'), files.join(tmpDir, 'start.webp.png.bmp')) assertTrue( files.fileContentsEqual(files.join(tmpDir, 'start.bmp'), files.join(tmpDir, 'start.webp.png.bmp')))