Color inconsistency before and after resize of Python image

Source: Internet
Author: User

Today to help the elder sister to solve a bug, testing the python image resize color inconsistencies before and after the problem.

Code Snippet Execution Function: Image specified multiple super resolution, input as [0-1] float type data, output as an unlimited format image

BUG: Input image is inconsistent with output image color

First, the function fragment that produces the bug to do the separation test:
1 Importh5py2 ImportNumPy as NP3 ImportMatplotlib.pyplot as Plt4  fromPILImportImage5  fromSciPyImportMisc6 7 8 defGet_result_array ():9file_name ="./butterfly_gt.bmp"TenImg_no_expand = Misc.imread (file_name, Flatten=false, mode='YCbCr') OneImg_no_expand = img_no_expand/255.0 A     #Img_no_expand = np.uint8 (img_no_expand*255) -H, W = img_no_expand.shape[:2] -     Print(Img_no_expand.shape) theH *= 2 -W *= 2 -data =list () -  +Data.append (Misc.imresize (img_no_expand[:,:, 0], [H, W],'bicubic') [:,:, None]) -Data.append (Misc.imresize (img_no_expand[:,:, 1], [H, W],'bicubic') [:,:, None]) +Data.append (Misc.imresize (img_no_expand[:,:, 2], [H, W],'bicubic') [:,:, None]) AData_out = np.concatenate (data, axis=2) atimg = Misc.toimage (arr=data_out, mode="YCbCr") -Img.save ("out_3.jpg") -  -  - if __name__=='__main__': -Get_result_array ()

To run the code:

On the left is the input image, and the image on the right is the output. In order to facilitate the comparison, the output image is scaled to the same as the input image, the image is visible, the output color is seriously distorted.

Second, in the Pycharm, ctrl+b view the source code:

Third, the discovery can choose the mode, conjecture may be wrong mode:

Four, in the first line of implementation of the function, initialize the image class, guess initialization parameter setting error.

During the initialization of the class, the default image has a maximum value of 255, while the actual input is 0-1 of the float type data. Found the wrong place.

Carefully review the document, mode can be modified. 0-1float type data corresponds to Mode= "F":

Then, add the parameters in the code:

Eight, the final code is as follows:
1 Importh5py2 ImportNumPy as NP3 ImportMatplotlib.pyplot as Plt4  fromPILImportImage5  fromSciPyImportMisc6 7 8 defGet_result_array ():9file_name ="./butterfly_gt.bmp"TenImg_no_expand = Misc.imread (file_name, Flatten=false, mode='YCbCr') OneImg_no_expand = img_no_expand/255.0 A     #Img_no_expand = np.uint8 (img_no_expand*255) -H, W = img_no_expand.shape[:2] -     Print(Img_no_expand.shape) theH *= 2 -W *= 2 -data =list () -Data.append (Misc.imresize (img_no_expand[:,:, 0], [H, W],'bicubic', mode="F") [:,:, None]) +Data.append (Misc.imresize (img_no_expand[:,:, 1], [H, W],'bicubic', mode="F") [:,:, None]) -Data.append (Misc.imresize (img_no_expand[:,:, 2], [H, W],'bicubic', mode="F") [:,:, None]) +Data_out = np.concatenate (data, axis=2) Aimg = Misc.toimage (arr=data_out, mode="YCbCr") atImg.save ("out_4.jpg") -  -  - if __name__=='__main__': -Get_result_array ()

Nine, the actual test, the input and output comparison chart is as follows:

Well, solved a bug, waiting for the elder sister please eat, haha

Color inconsistency before and after resize of Python image

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.