From c169786e7ca67c8813536d85e29bd44bdcf20802 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sun, 28 Mar 2021 21:53:56 +0200 Subject: [PATCH] Parametrize, add node module --- .gitignore | 5 +++++ compare.sh | 4 +++- main.py | 30 ++++++++++++++++++------------ package.json | 24 ++++++++++++++++++++++++ src/index.ts | 23 +++++++++++++++++++++++ tsconfig.json | 19 +++++++++++++++++++ 6 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index c3176f2..9b45b33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ +/@types/ +/lib/ +/node_modules/ +/package-lock.json /output_im.jpg /output_pyvips.jpg +/.log/ diff --git a/compare.sh b/compare.sh index 0e065d8..fbcb605 100755 --- a/compare.sh +++ b/compare.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash set -euo pipefail -time python ./main.py +# using different transform parameters, as vips takes backward parameters, and imagemagic takes forward parameters + +time python ./main.py demo.jpg '2.39014,0.381091,-5.07071e-13,8.09745e-16,2.45629,-1.95474e-12,0.000344095,0.0000931351' output_pyvips.jpg time convert demo.jpg -virtual-pixel Transparent -distort PerspectiveProjection '0.418385,-0.064912,1.11204e-12,-1.07285e-16,0.407118,1.85136e-13,-0.000143964,-0.0000155811' output_im.jpg diff --git a/main.py b/main.py index abd69aa..585e302 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 import pyvips +import sys -image = pyvips.Image.new_from_file("demo.jpg", access="random") +print(sys.argv) + +image = pyvips.Image.new_from_file(sys.argv[1], access="random") # T = [ # 0.418385, @@ -15,16 +18,19 @@ image = pyvips.Image.new_from_file("demo.jpg", access="random") # -0.0000155811, # ] -T = [ - 2.39014, - 0.381091, - -5.07071e-13, - 8.09745e-16, - 2.45629, - -1.95474e-12, - 0.000344095, - 0.0000931351, -] +# T = [ +# 2.39014, +# 0.381091, +# -5.07071e-13, +# 8.09745e-16, +# 2.45629, +# -1.95474e-12, +# 0.000344095, +# 0.0000931351, +# ] + + +T = list(map(float, sys.argv[2].split(","))) i = pyvips.Image.xyz(image.width, image.height) @@ -35,4 +41,4 @@ m = x.bandjoin(y) image = image.mapim(m) -image.write_to_file("./output_pyvips.jpg") +image.write_to_file(sys.argv[3]) diff --git a/package.json b/package.json new file mode 100644 index 0000000..d116f77 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "perspective-shift", + "version": "0.1.0", + "description": "Shift perspective of an image using four-point perspective transform in python, vips ", + "main": "lib/index.js", + "scripts": { + "build": "tsc", + "watch": "npm run build -- --watch", + "install": "npm run build", + "prepare": "npm run build" + }, + "types": "./@types", + "dependencies": { + "abortable-promise": "git+https://git.kuba-orlik.name/kuba/abortable-promises.git#3d13153", + "async-spawner": "git+https://git.kuba-orlik.name/kuba/async-spawner.git#a3d1a2bc27a3", + "tempy": "^1.0.1" + }, + "repository": { + "type": "git", + "url": "https://git.kuba-orlik.name/kuba/perspective-shift.git" + }, + "author": "", + "license": "ISC" +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..6f40aff --- /dev/null +++ b/src/index.ts @@ -0,0 +1,23 @@ +import { resolve } from "path"; +import AbortablePromise from "abortable-promise"; +import { deferedSpawn } from "async-spawner"; +import tempy from "tempy"; + +export default function perspectiveShift( + source_image: string, + reverse_transform_parameters: number[], + output_image: string = tempy.file({ extension: "jpg" }) +): AbortablePromise { + return new AbortablePromise(async function* (await_or_abort) { + const def = await deferedSpawn("python", [ + resolve(__dirname, "../main.py"), + source_image, + reverse_transform_parameters.join(","), + output_image, + ]); + yield; + return await_or_abort(def.waitForAnswer(), () => { + def.kill(); + }); + }); +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..41ac8e9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "moduleResolution": "node", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "target": "ES2019", + "declaration": true, + "esModuleInterop": true, + "lib": ["es6", "esnext"], + "outDir": "lib", + "checkJs": true, + "allowJs": true, + "declarationDir": "@types", + "sourceMap": true + }, + "include": ["src/**/*"] +}