How to change the catalog product image size by product id?

You can use the single_product_archive_thumbnail_size filter to change which image size is used for a particular product:

function wpse_287488_product_thumbnail_size( $size ) {
    global $product;

    if ( $product->get_id() === 123 ) {

    return $size;
add_filter( 'single_product_archive_thumbnail_size', 'wpse_287488_product_thumbnail_size' );

Just replace medium with the registered image size you want to use. See this codex article for an overview of image sizes and how to register your own.