Skip to content Skip to sidebar Skip to footer

How To Convert Array To Image Colour Channel In Python?

I need to split colour channel of the image (specifically 'Cb') in 8x8 blocks in order to modify DCT coefficients and reassemble them back later. I'm trying to do it using image.e

Solution 1:

Before calling Image.fromarray() on acb2, make sure to change the dtype to int as it was in the beginning. The image.reconstruct_from_patches_2d changes your picture values to float64, whereas the original values in cb are uint8. This is the only source of error I am getting. Other than that your code is working as expected.

Change your code from:

acb2 = image.reconstruct_from_patches_2d(patches, (500,500))
cb_n = Image.fromarray(acb2, 'L')

to:

acb2 = image.reconstruct_from_patches_2d(patches, (500,500))
#ADD THIS LINEacb2 = acb2.astype(np.uint8)
cb_n = Image.fromarray(acb2, 'L')

NOTE: (Unrelated to above)

Also make sure you are using correct dimensions in reconstruct_from_patches_2d for the image size. As you have specified (500,500), I am assuming that the width is same as height (500). But in images where height and width are different, Image module will be column-major whereas numpy (and python arrays by default) are row-major. So,

pic_size = pic.size

will report output (Width, Height), But when using in reconstruct_from_patches_2d, use (Height, Width).

Post a Comment for "How To Convert Array To Image Colour Channel In Python?"