[Python] Generate image back after changing blocks with view_as_blocks in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to change a given block of the following image, and then rebuilding image back.

    [​IMG]

    I would like to divide and change pixels from 224x224x3 blocks from such an image. As the image is 1024x1024x3, there will not be an integer number of 224x224x3 blocks, numpyso, I have to select a sub-area of the image to fit the block shape, like as follows:

    >>> block_shape = np.array((224, 224, 3))
    >>> nblocks = np.array(img.shape) // block_shape # integer division
    >>> crop_r, crop_c, crop_ch = nblocks * block_shape
    >>> cropped_img = img[:crop_r, :crop_c, :crop_ch]


    The cropped image is, therefore, a sub-area of the image that will select non-overlapping blocks of 224x224x3 pixels

    >>> cropped_img.shape
    (896, 896, 3)
    >>> Blocks = view_as_blocks(cropped_img, block_shape=(224, 224, 3))
    >>> Blocks.shape
    (4, 4, 1, 224, 224, 3)


    Now, just suppose I want to change the pixels of a given block, like zeroing all pixels of the first block:

    Blocks[0,0,:,:,:,:]=Blocks[0,0,:,:,:,:]*0


    Now I have the blocks with one block changed. Then, I need to recover cropped_img with that block changed (by converting variable Blocks back to be only one image/NumPy array), and finally, save the image. How to do that in Python?

    P.s= I checked a similar thread (Reverse skimage view_as_blocks() with numpy.reshape()) but the image considered has a different shape, channels and also the order of channels is different.

    Continue reading...

Compartilhe esta Página