diff --git a/README.md b/README.md index 5127bb4..da20230 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,9 @@ réduit la page : visibles ; - ou *Appareil distant* : saisir l'adresse IP de la machine qui porte la caméra (par exemple `192.168.1.20`) et choisir *nouvelle photo* ou - *dernière photo*. + *dernière photo* ; *Rotation de l'image* (0°, 90°, 180° ou 270° dans le + sens horaire) remet droite l'image d'une caméra montée de côté ou à + l'envers. 4. *Appliquer*. La page prend la taille du nid d'abeille, avec la photo redressée et le diff --git a/board_photo_import.inx b/board_photo_import.inx index 1485b9d..b449ca1 100644 --- a/board_photo_import.inx +++ b/board_photo_import.inx @@ -68,6 +68,14 @@ + + + + + + 20 diff --git a/board_photo_import.py b/board_photo_import.py index 5ec8bbf..9f1df0c 100644 --- a/board_photo_import.py +++ b/board_photo_import.py @@ -62,6 +62,7 @@ class BoardPhotoImport(inkex.EffectExtension): pars.add_argument("--remote_host", default="") pars.add_argument("--remote_port", type=int, default=5000) pars.add_argument("--remote_endpoint", default="capture") + pars.add_argument("--remote_rotation", type=int, default=0) pars.add_argument("--remote_timeout", type=int, default=20) pars.add_argument("--resolution", type=float, default=4.0) pars.add_argument("--jpeg_quality", type=int, default=85) @@ -175,6 +176,9 @@ class BoardPhotoImport(inkex.EffectExtension): inkex.errormsg(_("The capture server did not return a readable image " "({}).").format(url)) return None + # Camera montee de travers : on tourne la photo avant la detection, + # pour que l'image inseree en cas d'echec soit deja dans le bon sens. + photo = vision.rotate_photo(photo, opt.remote_rotation) self.notes.append(_("Photo received from {} ({} x {} px).").format( url, photo.width, photo.height)) return photo diff --git a/board_photo_vision.py b/board_photo_vision.py index bfb3d73..4b2399b 100644 --- a/board_photo_vision.py +++ b/board_photo_vision.py @@ -60,6 +60,21 @@ def load_photo(source): return image.convert("RGB") +def rotate_photo(image, angle): + """Photo tournee de `angle` degres (multiple de 90) dans le sens horaire. + + PIL tourne dans le sens trigonometrique : d'ou l'angle negatif. Pour un + quart de tour, rotate() passe par transpose (sans perte ni + interpolation) et `expand` echange largeur et hauteur. + """ + angle = int(angle) % 360 + if angle % 90: + raise ValueError("rotation must be a multiple of 90 degrees: {}".format(angle)) + if not angle: + return image + return image.rotate(-angle, expand=True) + + def gray_array(image): """Niveaux de gris (0-255) en tableau numpy uint8.""" return np.asarray(image.convert("L"), dtype=np.uint8) diff --git a/locale/en/LC_MESSAGES/board_photo.mo b/locale/en/LC_MESSAGES/board_photo.mo index dd4b3bf..3533adf 100644 Binary files a/locale/en/LC_MESSAGES/board_photo.mo and b/locale/en/LC_MESSAGES/board_photo.mo differ diff --git a/locale/fr/LC_MESSAGES/board_photo.mo b/locale/fr/LC_MESSAGES/board_photo.mo index c65d3d4..cc09158 100644 Binary files a/locale/fr/LC_MESSAGES/board_photo.mo and b/locale/fr/LC_MESSAGES/board_photo.mo differ diff --git a/po/board_photo.pot b/po/board_photo.pot index cd9ae48..0c59832 100644 --- a/po/board_photo.pot +++ b/po/board_photo.pot @@ -135,6 +135,30 @@ msgstr "" msgid "Use the last photo taken (/old)" msgstr "" +#: board_photo_import.inx +msgid "Rotation of the picture:" +msgstr "" + +#: board_photo_import.inx +msgid "Turns the received picture clockwise, for a camera mounted sideways or upside down" +msgstr "" + +#: board_photo_import.inx +msgid "None (0°)" +msgstr "" + +#: board_photo_import.inx +msgid "90° clockwise" +msgstr "" + +#: board_photo_import.inx +msgid "180°" +msgstr "" + +#: board_photo_import.inx +msgid "270° clockwise (90° anticlockwise)" +msgstr "" + #: board_photo_import.inx msgid "Timeout (s):" msgstr "" diff --git a/po/en.po b/po/en.po index ea50eab..055d1c5 100644 --- a/po/en.po +++ b/po/en.po @@ -137,6 +137,30 @@ msgstr "Take a new photo (/capture)" msgid "Use the last photo taken (/old)" msgstr "Use the last photo taken (/old)" +#: board_photo_import.inx +msgid "Rotation of the picture:" +msgstr "Rotation of the picture:" + +#: board_photo_import.inx +msgid "Turns the received picture clockwise, for a camera mounted sideways or upside down" +msgstr "Turns the received picture clockwise, for a camera mounted sideways or upside down" + +#: board_photo_import.inx +msgid "None (0°)" +msgstr "None (0°)" + +#: board_photo_import.inx +msgid "90° clockwise" +msgstr "90° clockwise" + +#: board_photo_import.inx +msgid "180°" +msgstr "180°" + +#: board_photo_import.inx +msgid "270° clockwise (90° anticlockwise)" +msgstr "270° clockwise (90° anticlockwise)" + #: board_photo_import.inx msgid "Timeout (s):" msgstr "Timeout (s):" diff --git a/po/fr.po b/po/fr.po index a0d1a5a..a395a24 100644 --- a/po/fr.po +++ b/po/fr.po @@ -137,6 +137,30 @@ msgstr "Prendre une nouvelle photo (/capture)" msgid "Use the last photo taken (/old)" msgstr "Reprendre la dernière photo prise (/old)" +#: board_photo_import.inx +msgid "Rotation of the picture:" +msgstr "Rotation de l'image :" + +#: board_photo_import.inx +msgid "Turns the received picture clockwise, for a camera mounted sideways or upside down" +msgstr "Tourne l'image reçue dans le sens horaire, pour une caméra montée de côté ou à l'envers" + +#: board_photo_import.inx +msgid "None (0°)" +msgstr "Aucune (0°)" + +#: board_photo_import.inx +msgid "90° clockwise" +msgstr "90° sens horaire" + +#: board_photo_import.inx +msgid "180°" +msgstr "180°" + +#: board_photo_import.inx +msgid "270° clockwise (90° anticlockwise)" +msgstr "270° sens horaire (90° sens antihoraire)" + #: board_photo_import.inx msgid "Timeout (s):" msgstr "Délai d'attente (s) :" diff --git a/test_board_photo.py b/test_board_photo.py index 690bb1e..c91f67d 100644 --- a/test_board_photo.py +++ b/test_board_photo.py @@ -719,6 +719,34 @@ def test_import_remote_last_photo(tmp_path, camera): assert camera.calls[-1] == "/old" +def test_rotate_photo(): + from PIL import Image + image = Image.new("RGB", (4, 2), (0, 0, 0)) + image.putpixel((0, 0), (255, 0, 0)) # coin haut gauche marque + assert vision.rotate_photo(image, 0) is image + turned = vision.rotate_photo(image, 90) # sens horaire + assert turned.size == (2, 4) and turned.getpixel((1, 0)) == (255, 0, 0) + turned = vision.rotate_photo(image, 180) + assert turned.size == (4, 2) and turned.getpixel((3, 1)) == (255, 0, 0) + turned = vision.rotate_photo(image, 270) + assert turned.size == (2, 4) and turned.getpixel((0, 3)) == (255, 0, 0) + with pytest.raises(ValueError): + vision.rotate_photo(image, 45) + + +@pytest.mark.parametrize("rotation", [90, 180, 270]) +def test_import_remote_rotated(tmp_path, camera, capsys, rotation): + # La photo tournee reste calibree : les reperes donnent l'orientation. + svg = run(tmp_path, "BoardPhotoImport", "board_photo_import", EMPTY_MM, + "--photo_source=remote", "--remote_host=127.0.0.1", + "--remote_port={}".format(camera.port), "--remote_endpoint=old", + "--remote_rotation={}".format(rotation)) + width, height = syn.PHOTO_SIZE + size = (height, width) if rotation in (90, 270) else (width, height) + assert "({} x {} px)".format(*size) in capsys.readouterr().err + assert_holes_match(outline_rings(svg)[1:]) + + def test_import_remote_errors(tmp_path, camera, capsys): port = camera.port run(tmp_path, "BoardPhotoImport", "board_photo_import", EMPTY_MM,