I'm currently working on converting breastcancer scans into black and white photos. This code needs to scan every file in the directory, process it, and save it with a unique name. My code looks like this:
wd = os.getcwd()
lencounter = 0
for file in os.listdir(wd):
lencounter += 1
for x in range(lencounter):
for file in os.listdir(wd):
if file.endswith("class0.png"):
image_file = Image.open(file)
image_file= image_file.convert('L')
image_file= image_file.convert('1')
print(image_file, x)
image_file.save("result1.png")
This code only allows me to save the last transformed picture, as "result1". Somehow the .save function doesn't let me include any iterationumber, just like you would expect when using the write() function.
I need something like "originalname_blackandwhite1.png" for every picture. I hope someone could help me out!
Thanks
What about image_file.save("{}_blackandwhite1.png".format(x))?
It can be more specific if you elaborate on how do you want your originalname to be
Related
I am trying to read/open some multi-extension .fits files. But I have a problem opening them. Here is the part of the cod I am using to open .fits files located in the same folder:
imgs = sorted(glob.glob('location_of_the_files/*.fits'))
for location in imgs:
hdul = fits.open(imgs)
original = hdul[1].data
model = hdul[2].data
residual = hdul[3].data
When running this I am getting this:
OSError: File-like object does not have a 'write' method, required for mode 'ostream'.
I try to check on the internet, but I do not understand what is going on.
Any help on how to solve this?
Maybe it is important to mention when trying to open single .fits file with this code everything works without any problem:
hdul = fits.open("location_of_the_files/image_data.fits")
original = hdul[1].data
model = hdul[2].data
residual = hdul[3].data
If needed let me know and I can upload .fits files (in that case, please just tell me how to do this here).
Thanks.
I suppose that you were trying to do something like this:
for location in imgs:
with fits.open(location) as hdul:
original = hdul[1].data
model = hdul[2].data
residual = hdul[3].data
...
Note the location instead of imgs as an argument of open method.
I would like to finish my script, I tried a lot to solve but being a beginner failed.
I have a function imageio which takes image from website and after that, i would like resize all images in 63x88 and put all my images in one pdf.
full_path = os.path.join(filePath1, name + ".png")
if os.path.exists(full_path):
number = 1
while True:
full_path = os.path.join(filePath1, name + str(number) + ".png")
if not os.path.exists(full_path):
break
number += 1
imageio.imwrite(full_path, im_padded.astype(np.uint8))
os.chmod(full_path, mode=0o777)
thanks for answer
We (ImageIO) currently don't have a PDF reader/writer. There is a long-standing features request for it, which hasn't been implemented yet because there is currently nobody willing to contribute it.
Regarding the loading of images, we have an example for this in the docs:
import imageio as iio
from pathlib import Path
images = list()
for file in Path("path/to/folder").iterdir():
im = iio.imread(file)
images.append(im)
The caveat is that this particular example assumes that you want to read all images in a folder, and that there is only images in said folder. If either of these cases doesn't apply to you, you can easily customize the snippet.
Regarding the resizing of images, you have several options, and I recommend scikit-image's resize function.
To then get all the images into a PDF, you could have a look at matplotlib, which can generate a figure which you can save as a PDF file. The exact steps to do so will depend on the desired layout of your resulting pdf.
Directory I would need to save to:
DataBase-Faces\name
Directory where the code is saving:
DataBase-Faces
My code:
cv2.imwrite("DataBase-Faces\\"+name+str(frames)+".png",Faceimage)
My program creates a folder with the typed name, I have to record these images in that folder, and the name of each image will be called Nome Typed + (frame number) .png
It looks like you're forgetting to put a directory separator between name and str(frames).
Try
image_path = '\\'.join(['DataBase-Faces', name, str(frames) + '.png'])
cv2.imwrite(image_path, Faceimage)
As a side note, you should not be concatenating paths by hand. Consider looking into the os.path module, which can save you from this sort of headache in the future.
General the Code to save the image is something like that
cv2.imwrite('file.png',img)
For the 'file.png' u want to be
EXAMPLE
name = 'my_photo_album_name'
frames = 500
frame_name = f'{name}\{frames}'
cv2.imwrite(f'DataBase-Faces\{frame_name}.png', img)
So I have this bit of code, which clips out a shapefile of a tree out of a Lidar Pointcloud. When doing this for a single shapefile it works well.
What I want to do: I have 180 individual tree shapefiles and want to clip every file out of the same pointcloud and save it as a individual .las file.
So in the end I should have 180 .las files. E.g. Input_shp: Tree11.shp -> Output_las: Tree11.las
I am sure that there is a way to do all of this at once. I just dont know how to select all shapefiles and save the output to 180 individual .las files.
Im really new to Python and any help would be appreciated.
I already tried to get this with placeholders (.format()) but couldnt really get anywhere.
from WBT.whitebox_tools import WhiteboxTools
wbt = WhiteboxTools()
wbt.work_dir = "/home/david/Documents/Masterarbeit/Pycrown/Individual Trees/"
wbt.clip_lidar_to_polygon(i="Pointcloud_to_clip.las", polygons="tree_11.shp", output="Tree11.las")
I don't have the plugin you are using, but you may be looking for this code snippet:
from WBT.whitebox_tools import WhiteboxTools
wbt = WhiteboxTools()
workDir = "/home/david/Documents/Masterarbeit/Pycrown/Individual Trees/"
wbt.work_dir = workDir
# If you want to select all the files in your work dir you can use the following.
# though you may need to make it absolute, depending on where you run this:
filesInFolder = os.listDir(workDir)
numberOfShapeFiles = len([_ for _ in filesInFolder if _.endswith('.shp')])
# assume shape files start at 0 and end at n-1
# loop over all your shape files.
for fileNumber in range(numberOfShapeFiles):
wbt.clip_lidar_to_polygon(
i="Pointcloud_to_clip.las",
polygons=f"tree_{fileNumber}.shp",
output=f"Tree{fileNumber}.las"
)
This makes use of python format string templates.
Along with the os.listdir function.
I am using the Image.save method from PIL and I cannot find where the file is being placed. I have done a system search yet still no luck.
My code looks like this:
print imageObj.save(fileName, "JPEG")
and gives the proper None response to say that it is working. Any idea where they go and how I can find them?
Thanks!
You should pick a location to save the image when setting the filename variable.
filename = "/Users/clifgray/Desktop/filename.jpeg"
imgObj.save(filename)