Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] in write_image_imageio(), doesn't handle grayscale image write correctly #463

Open
ZJLi2013 opened this issue Aug 12, 2024 · 0 comments

Comments

@ZJLi2013
Copy link

python3  samples/mlp_learning_an_image_pytorch.py  # with default albert.jpg & config.json

give errors:

ValueError: Can't write images with one color channel.

looks the write_image_imageio() under common.py missing handle grayscale image correctly. added as following:

def write_image_imageio(img_file, img, quality):
	img = (np.clip(img, 0.0, 1.0) * 255.0 + 0.5).astype(np.uint8)   # 
	kwargs = {}
	if os.path.splitext(img_file)[1].lower() in [".jpg", ".jpeg"]:
		if img.ndim >= 3 and img.shape[2] > 3:
			img = img[:,:,:3]
		if img.ndim >= 3 and img.shape[2] == 1:  # grayscale 
			img = np.squeeze(img)
		kwargs["quality"] = quality
		kwargs["subsampling"] = 0
	imageio.imwrite(img_file, img, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant