Skip to content

Commit

Permalink
Remove --folder script argument and add --path
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverMendel committed Mar 14, 2023
1 parent f89e13d commit bf50f61
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ RUN mkdir -p out
# Install heif-convert
RUN pip3 install .

ENTRYPOINT ["heif-convert", "--folder", "out"]
ENTRYPOINT ["./entrypoint.sh"]
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ heif-convert *.heic -f jpg
Convert an HEIF image to PNG using the Docker image:

```bash
docker run -it -v $(pwd):/usr/app/out --rm nevermendel/heif-convert input.heic -f jpg
docker run -v $(pwd):/usr/app/out --rm nevermendel/heif-convert input.heic -f jpg
```

## Arguments

```
usage: heif-convert [-h] [--folder FOLDER] [-o OUTPUT]
usage: heif-convert [-h] [-o OUTPUT] [-p PATH]
[-f {jpg,png,webp,gif,tiff,bmp,ico}] [-q QUALITY] [-v] [-vv]
[-V]
input [input ...]
Expand All @@ -85,10 +85,11 @@ positional arguments:
options:
-h, --help show this help message and exit
--folder FOLDER working directory (default: '.')
-o OUTPUT, --output OUTPUT
output file name excluding its extension
output file name
defaults to original file name (default: '{name}')
-p PATH, --path PATH output file path
defaults to original file path (default: '{path}')
-f {jpg,png,webp,gif,tiff,bmp,ico}, --format {jpg,png,webp,gif,tiff,bmp,ico}
output format (default: jpg)
-q QUALITY, --quality QUALITY
Expand Down
5 changes: 5 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

cd out

heif-convert "$@"
38 changes: 23 additions & 15 deletions heif_convert/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ def parse_args():
),
)
parser.add_argument("input", type=str, nargs="+", help="HEIF input file(s)")
parser.add_argument(
"--folder", type=str, default=".", help="working directory (default: '.')"
)
parser.add_argument(
"-o",
"--output",
type=str,
default="{name}",
help="output file name excluding its extension\n"
help="output file name\n"
+ "defaults to original file name (default: '{name}')",
)
parser.add_argument(
"-p",
"--path",
type=str,
default="{path}",
help="output file path\n"
+ "defaults to original file path (default: '{path}')",
)
parser.add_argument(
"-f",
"--format",
Expand Down Expand Up @@ -65,9 +70,8 @@ def parse_args():
args = parser.parse_args()

for input_file in args.input:
input_filepath = os.path.join(args.folder, input_file)
if not os.path.isfile(input_filepath):
parser.error(f"Input file '{input_filepath}' does not exist")
if not os.path.isfile(input_file):
parser.error(f"Input file '{input_file}' does not exist")

return args

Expand Down Expand Up @@ -98,18 +102,22 @@ def main():
logging.debug("Registering HEIF opener")
register_heif_opener()

if not os.path.isdir(args.folder):
logging.info("Output folder not found, creating it")
os.mkdir(args.folder)
os.chdir(args.folder)

for input_file in args.input:
logging.info(f"Reading {input_file}")
logging.info(f"Reading {os.path.abspath(input_file)}")
image = Image.open(input_file)
output_filename = (
args.output.format(name=input_file.split(".")[0]) + "." + args.format
args.output.format(
name=os.path.splitext(os.path.basename(input_file))[0],
)
+ "."
+ args.format
)
output_filepath = os.path.join(
args.path.format(
path=os.path.dirname(os.path.abspath(input_file)),
),
output_filename,
)
output_filepath = os.path.join(output_filename)
logging.info(f"Writing {output_filepath}")
image.save(output_filepath, quality=args.quality)
print(f"Wrote {output_filepath}")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
with open("heif_convert/_version.py", "r") as version_file:
__version__ = version_file.read().split("=")[1].strip().strip('"')

with open("README.md", "r") as f:
with open("README.md", "r", encoding="UTF-8") as f:
long_description = f.read()

setup(
Expand Down

0 comments on commit bf50f61

Please sign in to comment.