Printing one color using imshow [closed]

2024/10/12 11:24:14

I want to print a color on the screen using RGB values and the output should be just a single color. For example if I give RGB values of red, I want the output to show me a red color. But when I try this code, it isn't working. What am I missing?

import matplotlib.pyplot as plt
plt.imshow([(255, 0, 0)])
plt.show()

The output is:plot output

Answer

The issue is that you are trying to display a 2D color array with 1 row and 3 columns. The pixel values from left to right are 255, 0and 0. As @Ben K. correctly pointed out in the comments, by doing so the intensity values are scaled to the range 0..1 and displayed using the current colormap. That's why your code displays one yellow pixel and two violet pixels.

If you wish to specify the RGB values you should create a 3D array of m rows, n columns and 3 color channels (one chromatic channel for each RGB component).

Demo

The snippet below generates a random array of indices of a color palette and displays the result:

In [14]: import numpy as npIn [15]: import matplotlib.pyplot as pltIn [16]: from skimage import ioIn [17]: palette = np.array([[255,   0,   0], # index 0: red...:                     [  0, 255,   0], # index 1: green...:                     [  0,   0, 255], # index 2: blue...:                     [255, 255, 255], # index 3: white...:                     [  0,   0,   0], # index 4: black...:                     [255, 255,   0], # index 5: yellow...:                     ], dtype=np.uint8)...: In [18]: m, n = 4, 6In [19]: indices = np.random.randint(0, len(palette), size=(4, 6))In [20]: indices
Out[20]: 
array([[2, 4, 0, 1, 4, 2],[1, 1, 5, 5, 2, 0],[4, 4, 3, 3, 0, 4],[2, 5, 0, 5, 2, 3]])In [21]: io.imshow(palette[indices])
Out[21]: <matplotlib.image.AxesImage at 0xdbb8ac8>

color pixels

You could also generate a random color pattern rather than using a color palette:

In [24]: random_colors = np.uint8(np.random.randint(0, 255, size=(m, n, 3)))In [24]: random_colors
Out[27]: 
array([[[137,  40,  84],[ 42, 142,  25],[ 48, 240,  90],[ 22,  27, 205],[253, 130,  22],[137,  33, 252]],[[144,  67, 156],[155, 208, 130],[187, 243, 200],[ 88, 171, 116],[ 51,  15, 157],[ 39,  64, 235]],[[ 76,  56, 135],[ 20,  38,  46],[216,   4, 102],[142,  60, 118],[ 93, 222, 117],[ 53, 138,  39]],[[246,  88,  20],[219, 114, 172],[208,  76, 247],[  1, 163,  65],[ 76,  83,   8],[191,  46,  53]]], dtype=uint8)In [26]: io.imshow(random_colors)
Out[26]: <matplotlib.image.AxesImage at 0xe6c6a90>

random color pattern

https://en.xdnf.cn/q/69657.html

Related Q&A

Send headers along in python [duplicate]

This question already has answers here:Changing user agent on urllib2.urlopen(9 answers)Closed 9 years ago.I have the following python script and I would like to send "fake" header informatio…

How to download image to memory using python requests?

Like here How to download image using requests , but to memory, using http://docs.python-requests.org.

Accessing NumPy record array columns in Cython

Im a relatively experienced Python programmer, but havent written any C in a very long time and am attempting to understand Cython. Im trying to write a Cython function that will operate on a column o…

scipy append all rows of one sparse matrix to another

I have a numpy matrix and want to append another matrix to that.The two matrices have the shapes:m1.shape = (2777, 5902) m2.shape = (695, 5902)I want to append m2 to m1 so that the new matrix is of sh…

Add argparse arguments from external modules

Im trying to write a Python program that could be extended by third parties. The program will be run from the command line with whatever arguments are supplied.In order to allow third parties to creat…

Cosine similarity for very large dataset

I am having trouble with calculating cosine similarity between large list of 100-dimensional vectors. When I use from sklearn.metrics.pairwise import cosine_similarity, I get MemoryError on my 16 GB ma…

What exactly are the csv modules Dialect settings for excel-tab?

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the formatpreferred by Excel,” or “read data from this file which wa…

Python: how to make a recursive generator function

I have been working on generating all possible submodels for a biological problem. I have a working recursion for generating a big list of all the submodels I want. However, the lists get unmanageably …

Change default options in pandas

Im wondering if theres any way to change the default display options for pandas. Id like to change the display formatting as well as the display width each time I run python, eg:pandas.options.display.…

python-messaging Failed to handle HTTP request

I am using the code below to try to send an MMS message with python-messaging https://github.com/pmarti/python-messaging/blob/master/doc/tutorial/mms.rst Although the connection seems to go smoothly I …