CMYK image color wrong when plotting - python

I'm trying to import a CMYK image into python to check the total ink coverage. I saved the image with adobe illustrator as jpg, and it looks OK in the windows viewer. When open the image in python some of the colors are distorted. In the example image, the brown is completely black. I've tried starting from a TIF file but same problem occured.
from PIL import Image
import matplotlib.pyplot as plt
imgName = 'filename.jpg' # Filename
img = Image.open(imgName)
plt.figure()
plt.imshow(img)

Related

Saving Image in tensor array as jpg or png

I am trying to detect the face using mtcnn. The main aim is to detect face, crop and save the cropped image as jpg or png file type. The code implemented is below.
from facenet_pytorch import MTCNN
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
img = Image.open("example.jpg")
mtcnn = MTCNN(margin=20, keep_all=True, post_process=False)
faces = mtcnn(img)
print(faces.shape)
This gives the shape
torch.Size([1, 3, 160, 160])
How to save this cropped portion as jpg file.
torch.save(faces, "faces.torch")
that wont be saved as an image, if you want to save it as an image:
img = Image.fromarray(faces.cpu().detach().numpy()[0])
img.save("faces.png")

How to preserve the original colors of an image after adding text to it using PIL Python?

I am trying to add some text on my image using PIL, see the code below,
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import sys
image = Image.open('image.png')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial',40)
draw.text((700, 470),'Text',(0,0,0),font=font)
img.save('out-image.png','PNG')
But I lost the original colors of the image, see below images,
Original Image
After adding text
How I can preserve the original colors.
Thank You
That looks like a bug in PIL to me. I think it is because your image is palettised and the draw.text() is messing up the palette.
For a work-around, you can convert to an RGB image when you open it to avoid palette issues. Change to this:
image = Image.open('image.png').convert('RGB')

Reading a 4 band image with rasterio

I am trying to view a tif satellite image which has 4 bands. I want to remove the last band (NIR) and view the RGB image only, so I am trying to split the NIR from the rest of the image. Here is my code
import rasterio
from rasterio.plot import show
from matplotlib import pyplot as plt
from rasterio import plot
import numpy as np
#to display RGB
dataset = rasterio.open('2.tif')
%matplotlib inline
plot.show(dataset.read([1,2,3]), cmap="gray")
#to display just the red band
%matplotlib inline
plot.show(dataset.read(4), cmap="gray")
I provided a screen shot of the code and the output I am getting
Displaying just 1 band seems fine, but any idea why I keep seeing an image with a yellow and white color scheme when I try to display RGB bands together? I thought it's a cmap issue at the beginning, but even when I add 'cmap="gray"' the color of the image remains the same.

How can I convert an image to grey scale while keeping transparency using cv2?

At the moment my code takes an image in color and converts it to grayscale. The problem is that it makes the transparent pixels white or black.
This is what I have so far:
import cv2
img = cv2.imread("watch.png",cv2.IMREAD_GRAYSCALE)
cv2.imwrite("gray_watch.png",img)
Here is the pic for reference:
Watch.png
I found that using only PIL can accomplish this:
from PIL import Image
image_file = Image.open("convert_image.png") # opens image
image_file = image_file.convert('LA') # converts to grayscale w/ alpha
image_file.save('output_image.png') # saves image result into new file
This preserves the transparency of the image as well.
"LA" is a color mode. You can find other modes here: https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes

Python PIL cut off my 16-bit grayscale image at 8-bit

I'm working on an python program to display images of stars. The images are 16-bit grayscale tiffs.
If I try to display them in an extern program, e.g. ImageMagick they are correct but if I load them in python and then use 'show()' or implement them in a canvas in Tkinter they are, unless a few pixel, totally white.
So I estimate python sets every pixel above 255 to white but I don't know why. If I load the image and then save it as tiff again, ImageMagick can show it correct.
Thanks for help.
Try to convert the image to a numpy array and display that:
import Image
import matplotlib.pyplot as plt
import numpy as np
img = Image.open('image.tiff')
arr = np.asarray(img.getdata()).reshape(img.size[1], img.size[0])
plt.imshow(arr)
plt.show()
You can change the color mapping too:
from matplotlib import cm
plt.imshow(arr, cmap=cm.gray)

Categories

Resources