I'm trying to create some pixelart using a matrix in pycharm. The problem is that I have never used this program. It's supposed to work just by simply selecting if you're working with the RGB model, but it doesn't.
import cv2
import numpy as np
from matplotlib import pyplot as plt
pixels = ([0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0])
([0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0])
([0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0])
([0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0])
print (pixels[2][4])
cv2.waitKey()
You need to save pixels as a numpy array with type uint8 and then let cv2 display it. If you pass 0 to waitKey the window will stay open until you close it manually.
import cv2
import numpy as np
pixels = np.array([[0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0],[0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0],[0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0],[0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0]], np.uint8)
cv2.imshow("My Image", pixels)
cv2.waitKey(0)
You can use the Pillow library.
from PIL import Image
import numpy as np
pixels = np.array(pixels).astype(np.uint8) # converts pixels to a numpy array
image = Image.fromarray(pixels)
# now you can save your image using
image.save('newimage.png')
I have changed the format of the images to png also..but of no use. Does cv2 / imshow decrease the resolution automatically?
import numpy as np
import cv2
from matplotlib import pyplot as plt
imgL = cv2.imread('image.png',0)
imgR = cv2.imread('2.png',0)
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgR,imgL)
plt.imshow(disparity, 'gray')
plt.show()
My main aim is to generate the final image with the resolution as was the supplied images.
You're using imshow from matplotlib which might be the cause of different showing behaviour.
Instead try:
cv2.imshow("Res", disparity)
cv2.waitKey(0)
cv2.destroyAllWindows()
If that is still not good, please edit the question and include the resulting image and the input 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
This is my code in python to read TIF and save it as png file.
import numpy as np
from PIL import Image
import matplotlib.image as mpimg
#im.show()
from skimage import io
import matplotlib.pyplot as plt
import matplotlib
import imageio
I = plt.imread('Shoes.TIF')
im= Image.fromarray(I)
print im.size
imageio.imsave('ShoesTest.png', I)
input_filename = "Shoes.TIF"
img =mpimg.imread(input_filename)
imgg= Image.fromarray(img)
print img
imgg.show()
However, when the new image is saved. I get an issue. When I try to check the resolution of the image in photoshop, I get this result.
Original : 240 pixels / inch
Saved Image : 96 pixels / inch
How do I save image in python that retains the resolution of the image? That is why I noticed visual changes in the images. Please help.