Skip to content

Commit 8bc0db3

Browse files
fix png imwrite
1 parent 463c2f8 commit 8bc0db3

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

src/.DS_Store

0 Bytes
Binary file not shown.

src/cellpose_omni/io.py

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -156,59 +156,59 @@ def imread(filename):
156156
return None
157157

158158

159-
def imwrite(filename, arr, **kwargs):
160-
# should transition to imagecodecs instead, faster for webp, probably others too
161-
# cv2 does not support jpeg xl, but imagecodecs does
162-
163-
ext = os.path.splitext(filename)[-1].lower()
164-
if ext in ['.tif', '.tiff']:
165-
tifffile.imwrite(filename, arr, **kwargs)
166-
elif ext == '.npy':
167-
np.save(filename, arr, **kwargs)
168-
else:
169-
if ext == '.png':
170-
compression = kwargs.pop('compression', 9)
171-
params = [cv2.IMWRITE_PNG_COMPRESSION, compression]
172-
elif ext in ['.jpg', '.jpeg', '.jp2']:
173-
quality = kwargs.pop('quality', 95)
174-
params = [cv2.IMWRITE_JPEG_QUALITY, quality]
175-
elif ext == '.webp':
176-
quality = kwargs.pop('quality', 0)
177-
# note: webp quality should be >100 for "lossless" compression, though a few pixels still differ
178-
# 0 still looks really really good, though
179-
params = [cv2.IMWRITE_WEBP_QUALITY, quality]
180-
elif ext == '.jxl':
181-
quality = kwargs.pop('quality', 95)
182-
effort = kwargs.pop('effort', 7)
183-
distance = kwargs.pop('distance', 1.0)
184-
decoding_speed = kwargs.pop('decoding_speed', 0)
185-
params = [
186-
cv2.IMWRITE_JPEGXL_QUALITY, quality,
187-
cv2.IMWRITE_JPEGXL_EFFORT, effort,
188-
cv2.IMWRITE_JPEGXL_DISTANCE, distance,
189-
cv2.IMWRITE_JPEGXL_DECODING_SPEED, decoding_speed
190-
]
159+
# def imwrite(filename, arr, **kwargs):
160+
# # should transition to imagecodecs instead, faster for webp, probably others too
161+
# # cv2 does not support jpeg xl, but imagecodecs does
162+
163+
# ext = os.path.splitext(filename)[-1].lower()
164+
# if ext in ['.tif', '.tiff']:
165+
# tifffile.imwrite(filename, arr, **kwargs)
166+
# elif ext == '.npy':
167+
# np.save(filename, arr, **kwargs)
168+
# else:
169+
# if ext == '.png':
170+
# compression = kwargs.pop('compression', 9)
171+
# params = [cv2.IMWRITE_PNG_COMPRESSION, compression]
172+
# elif ext in ['.jpg', '.jpeg', '.jp2']:
173+
# quality = kwargs.pop('quality', 95)
174+
# params = [cv2.IMWRITE_JPEG_QUALITY, quality]
175+
# elif ext == '.webp':
176+
# quality = kwargs.pop('quality', 0)
177+
# # note: webp quality should be >100 for "lossless" compression, though a few pixels still differ
178+
# # 0 still looks really really good, though
179+
# params = [cv2.IMWRITE_WEBP_QUALITY, quality]
180+
# elif ext == '.jxl':
181+
# quality = kwargs.pop('quality', 95)
182+
# effort = kwargs.pop('effort', 7)
183+
# distance = kwargs.pop('distance', 1.0)
184+
# decoding_speed = kwargs.pop('decoding_speed', 0)
185+
# params = [
186+
# cv2.IMWRITE_JPEGXL_QUALITY, quality,
187+
# cv2.IMWRITE_JPEGXL_EFFORT, effort,
188+
# cv2.IMWRITE_JPEGXL_DISTANCE, distance,
189+
# cv2.IMWRITE_JPEGXL_DECODING_SPEED, decoding_speed
190+
# ]
191191

192-
else:
193-
# For any other extension, no special parameters are set.
194-
params = []
192+
# else:
193+
# # For any other extension, no special parameters are set.
194+
# params = []
195195

196-
# Handle color conversion for cv2
197-
if len(arr.shape) > 2:
198-
if arr.shape[-1] == 3:
199-
# If the user provided an image in RGB order, convert it to BGR for OpenCV.
200-
arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
201-
elif arr.shape[-1] == 4:
202-
# For a 4-channel image, assume it is in RGBA order.
203-
# Convert RGBA to BGRA so that the alpha channel is preserved.
204-
arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
196+
# # Handle color conversion for cv2
197+
# if len(arr.shape) > 2:
198+
# if arr.shape[-1] == 3:
199+
# # If the user provided an image in RGB order, convert it to BGR for OpenCV.
200+
# arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
201+
# elif arr.shape[-1] == 4:
202+
# # For a 4-channel image, assume it is in RGBA order.
203+
# # Convert RGBA to BGRA so that the alpha channel is preserved.
204+
# arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
205205

206-
# Append any extra kwargs as key/value pairs.
207-
extra_params = []
208-
for key, value in kwargs.items():
209-
extra_params.extend([key, value])
210-
# Write the image with the combined parameters.
211-
cv2.imwrite(filename, arr, params + extra_params)
206+
# # Append any extra kwargs as key/value pairs.
207+
# extra_params = []
208+
# for key, value in kwargs.items():
209+
# extra_params.extend([key, value])
210+
# # Write the image with the combined parameters.
211+
# cv2.imwrite(filename, arr, params + extra_params)
212212

213213

214214
import os
@@ -242,8 +242,8 @@ def imwrite(filename, arr, **kwargs):
242242
encoded = None
243243
if ext == '.png':
244244
# For PNG, get 'compression'; other kwargs may be passed to the encoder.
245-
compression = kwargs.pop('compression', 9)
246-
encoded = imagecodecs.png_encode(arr, compression=compression, **kwargs)
245+
level = kwargs.pop('level', 9)
246+
encoded = imagecodecs.png_encode(arr, level=level, **kwargs)
247247
elif ext in ['.jpg', '.jpeg', '.jp2']:
248248
level = kwargs.pop('level', 95)
249249
encoded = imagecodecs.jpeg_encode(arr, level=level, **kwargs)

0 commit comments

Comments
 (0)