I have an RGB tiff file containing a Z stack of 44 images. I want to open the file, convert it to grayscale, and save the resulting image as a new file.
The code I have right now is:
from PIL import Image
myimg = Image.open('1R - C1.tif')
myimg = myimg.convert('L')
myimg.save('1R - C1 BW.tif')
When I open the saved file, it contains only the first one of the 44 images in the stack. How do I manipulate and save the entire stack?
Related
I have a MR image volume in *.mhd format, and I do not have access to the original DICOM file. The image is not square and I need to crop the image and save a new *.mhd file again. I have a *.json file which contains all information of the original DICOM header.
Can anyone help me here?
Here's a example that shows how to crop an image in SimpleITK to get a square sub-image:
import random
import SimpleITK as sitk
img = sitk.Image(random.randrange(100,200), random.randrange(100,200), sitk.sitkUInt8)
size = img.GetSize()
print(size)
min_size = min(size[0], size[1])
# using python slicing to extract a sub-image
cropped_img = img[0:min_size, 0:min_size]
print(cropped_img.GetSize())
sitk.WriteImage(cropped_img, "square.mhd")
Note that the section being cropped is at the "end" (high index values) of the image.
i am trying to convert pdf to image need to some manipulation on image and again need to convert back manipulated file to pdf using python.
I have tried to convert pdf to image but i don't need to save file in local instead of this need to manipulate on the image file and again need to convert back to pdf file.
# import module
from pdf2image import convert_from_path
# Store Pdf with convert_from_path function
images = convert_from_path('example.pdf')
for i in range(len(images)):
# Save pages as images in the pdf
images[i].save('page'+ str(i) +'.jpg', 'JPEG')
// here it saving locally but i need to apply some background change like operation again i need to convert back it to pdf.
I am taking a Pattern recognition subject in this semester. I have a project to do face detection system from 3000++ images. I am using python for this project.
What I have done so far is convert the image into numpy array and store inside a list using code below:
# convert to numpy array, then grayscale, then resize, then vectorize, finally store in
# a list
for file in sorted(img_path):
img = cv2.imread(file)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.resize(img_gray, dsize=(150, 150), interpolation=cv2.INTER_CUBIC)
img_gray = img_gray.reshape(-1)
imagesData.append(img_gray)
# save to .h5 file, not yet do for label dataset
hf = h5py.File(save_path, 'a')
dset = hf.create_dataset('dataset',data=imagesData)
hf.close()
There is a small question here, is reshape(-1) mean vectorize? I try imagesData.shape, it print out (22500,), originally (150,150)
print(imagesData[0].shape)
The images are from a google drive folder(consisit of .png image). I am using sorted in looping because I want to arrange and store the numpy array in list from first to last images (1223 - 5222). Why I do this because I was given a text file containing some features that arranged from (1223-5222) and I going to store both dataset (imagesData) and label datasets (features) inside a .h5 file. The features text file as below:
text file
Am I right? because after store both dataset and label datasets into .h5 file, I will load them out and start some machine algorithm for my project, so I have to make sure each row of sample match correct label.
I have a data set of images in an image processing project. I want to input an image and scan through the data set to recognize the given image. What module/ library/ approach( eg: ML) should I use to identify my image in my python- opencv code?
To find exactly the same image, you don't need any kind of ML. The image is just an array of pixels, so you can check if the array of the input image equals that of an image in your dataset.
import glob
import cv2
import numpy as np
# Read in source image (the one you want to match to others in the dataset)
source = cv2.imread('test.jpg')
# Make a list of all the images in the dataset (I assume they are images in a directory)
filelist = glob.glob(r'C:\Users\...\Images\*.JPG')
# Loop through the images, read them in and check if an image is equal to your source
for file in filelist:
img = cv2.imread(file)
if np.array_equal(source, img):
print("%s is the same image as source" %(file))
break
I have a TIFF file with multiple frames. I found that if I open the TIFF file and call the seek(1) I can seek to the second frame. This is working, except when I go to save the image as a jpg, it only saves the first frame and not my current frame.
How can I save multiple frames to different JPG files?
from PIL import Image
im = Image.open('test.tiff')
im.save('test.jpeg')
I need to do something like...
from PIL import Image
im = Image.open('test.tiff')
im.seek(1)
im.save('test.jpeg')
and have it save the second frame and not the first.