Skip to content

Commit f178a62

Browse files
committed
fixed bug with jpg images
1 parent a3c7699 commit f178a62

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ A collection of small scripts written in Python and implemented as command line
55
#### [Batch Resizer](https://github.com/herokunt/python-scripts/blob/main/batch-resizer.py)
66
Process images by converting them to different formats, resizing them and store them as compressed archives for easy upload to your cloud. You may provide one or more images and they'll be processed in parallel for extra speed. File metadata like GPS location, camera model, etc., is completely removed.
77

8-
- TODO: add support to embed watermark image
9-
- TODO: add support to resize to multiple sizes at once
10-
- TODO: create output directory if does not exist
11-
128
```
139
# Resize images to a specified width and height (aspect ratio preserved)
1410
$ resizer.py ~/Pictures/* --resize 600 600

batch-resizer.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
them as compressed archives for easy upload to your cloud. You may provide one
66
or more images and they'll be processed in parallel for extra speed.
77
8+
# TODO: add support to embed watermark image
9+
810
usage: batch-resizer.py [-h] [-v] [-o [OUTPUT]] [-r WIDTH HEIGHT]
911
[-f {webp,jpeg,jpg,png} [{webp,jpeg,jpg,png} ...]]
1012
[-z | -t [ARCHIVE]]
@@ -26,8 +28,6 @@
2628
-z, --zip-archive Store processed images as .zip archive (uncompressed)
2729
-t [ARCHIVE], --tar-archive [ARCHIVE]
2830
Store processed images as .tar.gz archive (compressed)
29-
30-
TODO: Option to provide an image to add as a watermark for each image
3131
"""
3232

3333
from PIL import Image
@@ -47,14 +47,18 @@ def main():
4747
images = [file for file in args.input if valid_ext.match(file)]
4848

4949
if args.verbose:
50-
logging.basicConfig(level=logging.DEBUG)
51-
print(f'Found {len(images)} images:')
50+
logging.basicConfig(level=logging.INFO)
51+
logging.info(args)
52+
logging.info(f'Found {len(images)} images:')
5253

53-
# Process each file using multi-processing, returns extensions used
54+
if not os.path.exists(args.output):
55+
os.makedirs(args.output)
56+
57+
# Process images concurrently
5458
with concurrent.futures.ProcessPoolExecutor() as executor:
5559
formats = executor.map(process_image, images, repeat(args))
5660

57-
# Creates an archive with the output images.
61+
# Creates an archive with all output images.
5862
if args.archive:
5963
make_archive(args, images, next(formats))
6064

@@ -70,7 +74,7 @@ def make_archive(args, input, formats):
7074

7175
archive_name = os.path.join(args.output, f'{args.archive}.tar.gz')
7276

73-
logging.debug(f'Building archive {archive_name}...')
77+
logging.info(f'Building archive {archive_name}...')
7478

7579
with tarfile.open(archive_name, 'x:gz') as tar:
7680
for img in images:
@@ -89,9 +93,12 @@ def process_image(image, args):
8993
formats = args.format if args.format else [ext.replace('.', '')]
9094

9195
# Append short hash to filename to avoid accidental overwrite
92-
filename = f'resized-{filename}'
96+
if args.dimensions:
97+
filename = f'{filename}-{args.dimensions[0]}'
98+
elif not args.format and not args.dimensions:
99+
filename = f'{filename}-copy'
93100

94-
logging.debug(f'Processing file {filename}')
101+
logging.info(f'Processing file {filename}')
95102

96103
with Image.open(image) as img:
97104
# Find width, height and output file format
@@ -105,7 +112,12 @@ def process_image(image, args):
105112
for f in formats:
106113
format = 'JPEG' if f.lower().endswith('jpg') else f.upper()
107114
filepath = f'{os.path.join(args.output, filename)}.{format.lower()}'
108-
img.save(filepath, format)
115+
116+
# Accounts for JPEG files without alpha channel
117+
if format == 'JPEG':
118+
img.convert('RGB').save(filepath, format)
119+
else:
120+
img.save(filepath, format)
109121

110122
return formats
111123

0 commit comments

Comments
 (0)