Strange image produced by matplotlib - python

I have a png image as follows:
I wrote the following script to read into matplotlib.
import numpy as np, matplotlib.pyplot as plt
from PIL import Image
fi = "map.png"
data = np.array(Image.open(fi))
print data.shape
plt.imshow(data)
plt.show()
But the image looks different.
How to make it look similiar to the first one?
I mean, the colors.

Matplotlib handles images much more transparently if you use the mpimg package:
import numpy as np, matplotlib.pyplot as plt
import matplotlib.image as mpimg
fi = "map.png"
data = mpimg.imread(fi)
print(data.shape)
plt.imshow(data)
plt.show()

The image you have provided is not a Grayscale image
import matplotlib.pyplot as plot
import matplotlib.image as image_rgb
image = image_rgb.imread("image.png")
plot.imshow(image)
plot.show()
I have not used the PIL library and it produces apt results.

Related

ROI for image dataset

I'm trying to find out ROI for an image dataset. I'm able to find ROI for a single image, but when it comes to the whole dataset or multiple images I can't do it.
I tried to use different code from the internet, but nothing worked.
`
import cv2
import numpy as np
import os
import random
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
img_folder=r'Shrimp/train/Resized_Dataset'
img_dir = r'Cropped_shrimp'
for i in range(30):
file = random.choice(os.listdir(img_folder))
image_path= os.path.join(img_folder, file)
img=mpimg.imread(image_path)
ax=plt.subplot(1,30,i+1)
ax.title.set_text(file)
plt.imshow(img)
roi = cv2.selectROI(img_folder)
ROI_image = img_folder[int(r[1]):int(r[1]+r[3]),
int(r[0]):int(r[0]+r[2])]
cv2.imshow("ROI_image", ROI_image)
cv2.waitKey(0)
os.chdir(img_dir)
cv2.imwrite("ROI_image", JPG)
`
This is my last approach. I think there're lots of mistake because I'm trying this for the first time.

VsCode matplotlib doesnt show figure window

import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("Desktop\sudoku.jpg", 0)
img = np.float32(img)
plt.figure()
plt.plot()
plt.imshow(img), plt.axis("off")
cv2.waitKey(0)
code doesn't give error but figure window is not opening
I think you are mixing cv2 and matplotlib. If you just want to display the image you can do it like so:
Using cv2:
import cv2
I = cv2.imread('path/to/img')
cv2.imshow('window_name', I)
cv2.waitKey(0)
or you can use matplotlib by simply adding plt.show() at the end as #JeruLuke pointed out. Just remember that OpenCV reads the image as BGR and this would make the colors show up weird when you display the image. You can use cv2.imread('path/to/image.jpg', COLOR_BGR2RGB) if you are set on using matplotlib.

What's the difference between mpimg.imread and plt.imread when I read an image with Python?

I'm trying to read an image, and I found that both following sentences are okay, so what's the difference?
import matplotlib.pyplot as plt
img = plt.imread("1.jpg")
or
import matplotlib.image as mpimg
img = mpimg.imread('1.jpg')
Based off of the source for matplotlib.pyplot.imread, it's just an alias for matplotlib.image.imread.

VS CODE not showing Matplotlib images

I have a simple script that reads and shows images in Matplotlib.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
baseimg = '/home/ahmad/Downloads/mnist/trainingSample/img_0.jpg'
img = mpimg.imread(baseimg)
plt.imshow(img)
plt.show()
It is working on Jupyter Notebook but when I run it in VS Code, it runs but does not show any image. Is there some problem in my code or how to fix it. Thanks

rgb2gray not working, creating rainbow image

I'm trying to do data analysis on a series of photos, and when all photos are "changed" to grayscale from RGB, they are coming up as, well look:
The standard Astronaut image as my system says it's grayscale
Here's the code I'm using:
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray)
plt.show()
I also tried converting as hsv to rgb then grayscale, but it still produces a similar, non-grayscale image.
The problem is that matplotlib shows the image 2D with its default colormap. Change your code to
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray, cmap='gray')
plt.show()
You can also use
from skimage import io
io.imshow(img_gray)
which will handle grayscale images automatically

Categories