This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #270, function to zoom_in + cli
- Loading branch information
Showing
7 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
@brief test tree node (time=7s) | ||
""" | ||
|
||
import sys | ||
import os | ||
import unittest | ||
import warnings | ||
from io import StringIO | ||
|
||
from pyquickhelper.loghelper import fLOG, BufferedPrint | ||
from pyquickhelper.pycode import ExtTestCase, get_temp_folder | ||
from pyquickhelper.__main__ import main | ||
|
||
|
||
class TestCliImgHelper(ExtTestCase): | ||
|
||
def test_zoom_img_help(self): | ||
st = BufferedPrint() | ||
main(args=['zoom_img', '--help'], fLOG=st.fprint) | ||
res = str(st) | ||
self.assertIn("zoom_img [-h]", res) | ||
|
||
def test_zoom_img_do(self): | ||
temp = get_temp_folder(__file__, 'temp_img_zoom') | ||
dest = os.path.join(temp, '{}') | ||
data = os.path.join(temp, '..', 'data', '*.png') | ||
st = BufferedPrint() | ||
win = main(args=['zoom_img', '-f', '0.5', '--img', | ||
data, '-o', dest], fLOG=st.fprint) | ||
res = str(st) | ||
self.assertNotIn("zoom_img [-h]", res) | ||
self.assertIn("Writing '", res) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
@brief test log(time=7s) | ||
skip this test for regular run | ||
""" | ||
|
||
import sys | ||
import os | ||
import unittest | ||
|
||
from pyquickhelper.loghelper import fLOG | ||
from pyquickhelper.pycode import get_temp_folder, ExtTestCase | ||
from pyquickhelper.imghelper.img_helper import zoom_img | ||
|
||
|
||
class TestImgHelper(ExtTestCase): | ||
|
||
def test_img_zoom(self): | ||
temp = get_temp_folder(__file__, "temp_img_zoom") | ||
data = os.path.join(temp, "..", "data", "image.png") | ||
dest = os.path.join(temp, "image2.png") | ||
obj = zoom_img(data, factor=0.5, out_file=dest) | ||
self.assertExists(dest) | ||
self.assertNotEmpty(obj) | ||
dest = os.path.join(temp, "image3.png") | ||
obj = zoom_img(data, max_dim=10, out_file=dest) | ||
self.assertExists(dest) | ||
self.assertNotEmpty(obj) | ||
|
||
def test_img_zoom_folder(self): | ||
temp = get_temp_folder(__file__, "temp_img_zoom_folder") | ||
data = os.path.join(temp, "..", "data", "*.png") | ||
dest = os.path.join(temp, "{}") | ||
obj = zoom_img(data, factor=0.5, out_file=dest) | ||
dest = os.path.join(temp, "image.png") | ||
self.assertExists(dest) | ||
self.assertNotEmpty(obj) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
@file | ||
@brief Helpers around images. | ||
.. versionadded:: 1.9 | ||
""" | ||
import os | ||
import glob | ||
|
||
|
||
def zoom_img(img, factor=1., max_dim=None, out_file=None, fLOG=None): | ||
""" | ||
Zooms an image. | ||
@param img image or filename or pattern | ||
@param factor multiplies the image by this factor if not None | ||
@param max_dim modifies the image, the highest dimension | ||
should below this number | ||
@param out_file stores the image into this file if not None | ||
@param fLOG logging function | ||
@return image | ||
""" | ||
if isinstance(img, str): | ||
if '*' in img: | ||
found = glob.glob(img) | ||
res = [] | ||
for im in found: | ||
if out_file is None: | ||
i = zoom_img(im, factor=factor, max_dim=max_dim, fLOG=fLOG) | ||
else: | ||
of = out_file.format(os.path.split(im)[-1]) | ||
i = zoom_img(im, factor=factor, max_dim=max_dim, | ||
out_file=of, fLOG=fLOG) | ||
res.append(i) | ||
return res | ||
from PIL import Image | ||
obj = Image.open(img) | ||
elif hasattr(obj, 'size'): | ||
obj = img | ||
else: | ||
raise TypeError( | ||
"Image should be a string or an image not {}.".format(type(img))) | ||
dx, dy = obj.size | ||
if max_dim is not None: | ||
if not isinstance(max_dim, int): | ||
max_dim = int(max_dim) | ||
facx = dx * 1. / max_dim | ||
facy = dy * 1. / max_dim | ||
factor = min(facx, facy) | ||
if factor is not None: | ||
if not isinstance(factor, float): | ||
factor = int(factor) | ||
dx = int(dx * factor + 0.5) | ||
dy = int(dy * factor + 0.5) | ||
obj = obj.resize((dx, dy)) | ||
if out_file is not None: | ||
if fLOG is not None: | ||
fLOG("Writing '{}' dim=({},{}).".format(out_file, dx, dy)) | ||
obj.save(out_file) | ||
return obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters