Convert CGImageRef to PIL - python

How could I convert a CGImageRef to PIL without saving the image to disk on osx?
I though about getting the raw pixel data from the CGImageRef and using Image.fromstring() to make the PIL image by doing
import mss
import Quartz.CoreGraphics as CG
from PIL import Image
mss = mss.MSSMac()
for i, monitor in enumerate(mss.enum_display_monitors(0)):
imageRef = mss.get_pixels(monitor)
pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(imageRef))
img = Image.fromstring("RGB", (monitor[b'width'], monitor[b'height']), pixeldata)
img.show()
but this doesn't give me the correct image.
This is the image I expect:
and this is the image I get in PIL:

The screencapture from CG doesn't necessarily use the RGB colorspace. It may use RGBA or something else. Try changing:
img = Image.fromstring("RGB", (monitor[b'width'], monitor[b'height']), pixeldata)
to
img = Image.fromstring("RGBA", (monitor[b'width'], monitor[b'height']), pixeldata)
Here is how I detect which colorspace is actually being captured:
bpp = CG.CGImageGetBitsPerPixel(imageRef)
info = CG.CGImageGetBitmapInfo(imageRef)
pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(imageRef))
img = None
if bpp == 32:
alphaInfo = info & CG.kCGBitmapAlphaInfoMask
if alphaInfo == CG.kCGImageAlphaPremultipliedFirst or alphaInfo == CG.kCGImageAlphaFirst or alphaInfo == CG.kCGImageAlphaNoneSkipFirst:
img = Image.fromstring("RGBA", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata, "raw", "BGRA")
else:
img = Image.fromstring("RGBA", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata)
elif bpp == 24:
img = Image.fromstring("RGB", (CG.CGImageGetWidth(imageRef), CG.CGImageGetHeight(imageRef)), pixeldata)

It was a bug I fixed some time ago. Here is how to achieve what you want using the latest mss version (2.0.22):
from mss.darwin import MSS
from PIL import Image
with MSS() as mss:
for monitor in mss.enum_display_monitors(0):
pixeldata = mss.get_pixels(monitor)
img = Image.frombytes('RGB', (mss.width, mss.height), pixeldata)
img.show()
Note that pixeldata is just a reference to mss.image, you can use it directly.

Related

Masking many images from two different path opencv

Hello stackoverflow people:) I'm trying to masking many image from two different path, but I don't have an idea to do that.
This an example for just two images and what I've do so far
image = cv.imread('Dataset/IDRiD_02.jpg', cv.IMREAD_COLOR)
od = cv.imread('od/IDRiD_02_OD.jpg', cv.IMREAD_GRAYSCALE)
mask = od
other = cv.bitwise_not(mask)
masking = cv.bitwise_and(image, image, mask=other)
cv.imwrite('Output/masking/' + 'masking.jpg', masking)
Input is IDRiD_02.jpg and IDRiD_02_OD.jpg then Output is masking.jpg
Then I want to do the same but with many images
import cv2 as cv
import numpy as np
import os
import glob
import os.path
od_images = []
for directory_path in glob.glob("od/"):
for mask_path in glob.glob(os.path.join(directory_path, "*.jpg")):
mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
od_images.append(mask)
od_images = np.array(od_images)
path = "Dataset/*.jpg"
for file in glob.glob(path):
#read image
image = cv.imread(file, cv.IMREAD_COLOR)
# e.g. MyPhoto.jpg
basename = os.path.basename(file)
# e.g. MyPhoto
name = os.path.splitext(basename)[0]
mask = cv.bitwise_not(od_images)
masking = cv.bitwise_and(image, image, mask = mask)
cv.imwrite('Output/masking/' + name + '_masking.jpg', masking)
but then after I run the code, I'm getting the following error message
masking = cv.bitwise_and(image, image, mask = mask)
error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:230: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'
anyone can understand and help me? Thank you before:)
Hope it will work for you !
import cv2 as cv
import os
img_path = r"image_folder_path"
od_images = r"od_img_folder_path"
for img,od in zip(os.listdir(img_path), os.listdir(od_images)):
image = cv.imread(img_path+"\\"+img, cv.IMREAD_COLOR)
od = cv.imread(od_images+"\\"+od, cv.IMREAD_GRAYSCALE)
other = cv.bitwise_not(od)
res = cv.bitwise_and(image, image, mask=other)
cv.imwrite('Output/masking/' +img+ '_masking.jpg', res)

Display Dicom image using PIL(PILLOW) Python Library

I am trying to read and display DICOM(.dcm) images using below code:-
import pydicom as dicom
import numpy as np
from PIL import Image, ImageEnhance, ImageOps
from PIL.ImageQt import ImageQt
def display_dicom_images(self, folder_Path):
try:
# Image parameters
image_width = 382
image_height = 382
image_depth = 3
self.total_images_in_folder = len(glob.glob1(folder_Path,"*"))
# Select the center image for display
self.current_image_number = round(self.total_images_in_folder / 2)
self.display_number = self.current_image_number
image_dtype = np.uint8
pixel_array = np.ndarray([self.total_images_in_folder, image_height, image_width, image_depth]).astype(image_dtype)
# load images here, once better MR images are acquired
for image_index in range(0, self.total_images_in_folder):
# for DICOM
image_path = folder_Path + "/" + str(image_index) + ".dcm"
scan_image = dicom.dcmread(image_path)
scan_image = scan_image.pixel_array.astype(image_dtype)
pixel_array[image_index, :scan_image.shape[0], :scan_image.shape[1], :scan_image.shape[2]] = scan_image
return pixel_array
But getting error:-
IndexError('tuple index out of range',)
i am using pillow python library for image.
How do you know scan_image.shape is of length 3? MR images should only be monochrome, which would make image_depth = 1 and the length of scan_image.shape equal to 2.
C.8.3.1.1.3 Photometric Interpretation
Enumerated Values:
MONOCHROME1
MONOCHROME2

is there a frame size limitation in opencv VideoWriter?

When using opencv to save a bunch of pictures to a video file the file won't open unless I resize the image:
(Windows error - "This item is in a format we don't support. 0xc00d36b4")
My code looks like this:
import cv2
import os
input_path = "input_images_folder_path"
imgs_lst = os.listdir(r"C:\Users\...\input_path")
outvid_path = r"C:\Users\...\output.avi"
image0 = input_path +"\\"+ imgs_lst[0]
img0 = cv2.imread(image)
size = (int(img0.shape[1]), int(img0.shape[0]))
fps = 12.0
is_color = True
fourcc = cv2.VideoWriter_fourcc(*"XVID")
vid = cv2.VideoWriter(outvid_path, fourcc, 10.0, size, True)
for i in range(0,50):# int(len(imgs_lst))):
image = input_path +"\\"+ imgs_lst[i]
img = cv2.imread(image,1)
img = cv2.resize(img, size) #tried to comment this out... wont work either
cv2.imshow("img", img)
cv2.waitKey(1)
vid.write(img)
vid.release()
if I resize -
size = (int(img0.shape[1]/2), int(img0.shape[0]/2))
all works well.
My question is whether or not there is a limit on the frame size. I found some other answers regarding the output file size limitation, but nothing about the single frame shape.
(changing fps, format didn't work either)

Passing an image as an argument to a function in python

How can I create a function that takes an image file (not image filename) in python. Simply, like the following:
FaceController.py
import cv2
from Computer_Vision import Face_Detector as FD
def detectface():
img = cv2.imread('DSC_1902.JPG')
FD.detect(img)
detectface()
Face_Detector.py
import cv2
def detect(img):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('messigray.png', img)
return img
Error:
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
You can pass a pointer pointing to the image instead of the image or the filename of the image
EDIT
def image_function(imagePointer):
#DO SOMETHING WITH THE IMAGE
#HERE IS THE IMAGE POINTER
image = open('your_image.png')
#CALLING THE FUNCTION
image_function(image)
Sorry, I don't know opencv so I can not help in your code :(
You can actually pass the image as a tensor.
with cv2.imread() and torch.
Which is easy, useful.
short answer:
load with cv2.imread()
transform to tensor with
img = torch.Tensor(img)/255.
That works for my application.
Yours might be a little different.
Code answer:
from Computer_Vision import Face_Detector as FD
def detectface():
import cv2
import torch
folder = r"This Folder/"
image_file = folder+"image.png"
# or
# file = r"image.png"
# image_file = os.path.join(folder, file)
img = imread(image_file)
img = torch.Tensor(img)/255. # THE KEY LINE HERE.
FDdetect(img):
"""Do stuff with object detection..."""
result =
return result

Image Conversion - Cannot write mode RGBA as JPEG

I'm trying to resize & reduce quality of image before upload in project. Here's what I tried,
def save(self):
im = Image.open(self.image)
output = BytesIO()
im = im.resize(240, 240)
im.save(output, format='JPEG', quality=95)
output.seek(0)
self.image = InMemoryUploadedFile(output, 'ImageField', "%s.jpg" % self.image.name.split('.')[0], 'image/jpeg', sys.getsizeof(output), None)
super(Model, self).save()
It's working fine if I upload a jpg image but if I upload a png or any other image type, it's not working it's raising errors like cannot write mode RGBA as JPEG & cannot write mode P as JPEG etc.
How can we fix that? Thank You!
If your image.mode is "P" or "RGBA" and you want to convert it to jpeg then you need to first convert the image.mode because the previous modes aren't supported for jpeg
if im.mode in ("RGBA", "P"):
im = im.convert("RGB")
https://github.com/python-pillow/Pillow/issues/2609
Summary timop and 2:
backgroud
JPG not support alpha = transparency
RGBA, P has alpha = transparency
RGBA= Red Green Blue Alpha
result
cannot write mode RGBA as JPEG
cannot write mode P as JPEG
solution
before save to JPG, discard alpha = transparency
such as: convert Image to RGB
then save to JPG
your code
if im.mode == "JPEG":
im.save(output, format='JPEG', quality=95)
elif im.mode in ["RGBA", "P"]:
im = im.convert("RGB")
im.save(output, format='JPEG', quality=95)
More for you:
about resize & reduce quality of image, I have implement a function, for you (and others) to refer:
from PIL import Image, ImageDraw
cfgDefaultImageResample = Image.BICUBIC # Image.LANCZOS
def resizeImage(inputImage,
newSize,
resample=cfgDefaultImageResample,
outputFormat=None,
outputImageFile=None
):
"""
resize input image
resize normally means become smaller, reduce size
:param inputImage: image file object(fp) / filename / binary bytes
:param newSize: (width, height)
:param resample: PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC, or PIL.Image.LANCZOS
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.thumbnail
:param outputFormat: PNG/JPEG/BMP/GIF/TIFF/WebP/..., more refer:
https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html
if input image is filename with suffix, can omit this -> will infer from filename suffix
:param outputImageFile: output image file filename
:return:
input image file filename: output resized image to outputImageFile
input image binary bytes: resized image binary bytes
"""
openableImage = None
if isinstance(inputImage, str):
openableImage = inputImage
elif CommonUtils.isFileObject(inputImage):
openableImage = inputImage
elif isinstance(inputImage, bytes):
inputImageLen = len(inputImage)
openableImage = io.BytesIO(inputImage)
if openableImage:
imageFile = Image.open(openableImage)
elif isinstance(inputImage, Image.Image):
imageFile = inputImage
# <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=3543x3543 at 0x1065F7A20>
imageFile.thumbnail(newSize, resample)
if outputImageFile:
# save to file
imageFile.save(outputImageFile)
imageFile.close()
else:
# save and return binary byte
imageOutput = io.BytesIO()
# imageFile.save(imageOutput)
outputImageFormat = None
if outputFormat:
outputImageFormat = outputFormat
elif imageFile.format:
outputImageFormat = imageFile.format
imageFile.save(imageOutput, outputImageFormat)
imageFile.close()
compressedImageBytes = imageOutput.getvalue()
compressedImageLen = len(compressedImageBytes)
compressRatio = float(compressedImageLen)/float(inputImageLen)
print("%s -> %s, resize ratio: %d%%" % (inputImageLen, compressedImageLen, int(compressRatio * 100)))
return compressedImageBytes
latest code can found here:
https://github.com/crifan/crifanLibPython/blob/master/crifanLib/crifanMultimedia.py

Categories