Want to read multiple image and save in a different directory after crop images - directory

I want to read multiple files from folder and run this crop script and want to save into different directory folder
import os
import cv2
from PIL import Image
# Crops the image and saves it as "new_filename"
def crop_image(img, crop_area, new_filename):
cropped_image = img.crop(crop_area)
cropped_image.save(new_filename)
# # The x, y coordinates of the areas to be cropped. (x1, y1, x2, y2)
crop_areas = [(40,85, 140, 185 )]
image_name = './left0.jpg'
Here I want to read the multiple images and save into the different folder
img = Image.open(image_name)
# Loops through the "crop_areas" list and crops the image based on the coordinates in the list
for i, crop_area in enumerate(crop_areas):
filename = os.path.splitext(image_name)[0]
ext = os.path.splitext(image_name)[1]
new_filename = filename + '_cropped' + str(i) + ext
crop_image(img, crop_area, new_filename)

Related

Error when looping: attempt to select more than one element in vectorIndex

I am new in coding with R and I work with a large dataset.
I am trying to write a code that do the following things:
Get all pathes to all files in my folder
Extract the names of the files (as I want to name my plots after the input file)
Read in all files in my folder (these are all .csv files)
Plot a diagram for each .csv file by plotting groundwater level against the year
--> these plots should then get the title of the input file and also be stored under the same name.
For example when my file is called 211210.csv, then the title should be 211210 and stored as 211210.png
This is the code I have until know. As I said, I am new to R, and I tried to solve may problems I had in the code but I still run into new errors. Is there someone who can explain me where the problem is and how to solve it.
library(fs)
library(ggplot2)
library(tidyverse)
#Opening path to my data
filepath <- fs::dir_ls("D:/Desktop/Masterarbeit/Daten/Test/")
# Get name of files
name <- basename(filepath)
#Read every single files
file_content <- list()
for (i in seq_along(filepath)){
path <- filepath
file_content[[i]] <- read.csv(
file = filepath[[i]], header = TRUE
)
}
file_content <- set_names(file_content, filepath)
#Plot the diagram with gwl against year for each file, title = name of each file and store it in a seperat folder with the name of the input file
for (i in file_content){
mypath <- file.path("D:/Desktop/Masterarbeit/Daten/Results/", paste("Messstelle_", name[[i]], ".png", sep = ""))
png(file=mypath)
mytitle = paste("Messstelle", name[[i]])
plot(i$year, i$gwl,
pch = 19, #--> solid circle
cex = 1.5, #--> make 150% size
main = name[[i]],
xlab = "Year",
ylab = "Ground water level",
)
dev.off()
}
First I would prefer doing everything in one loop for efficiency. Second, I would avoid using unnecessary packages, e.g. fs (Base R has a good list.files function to list all files in a folder) Third, I would iterate through the names of the files and not through a numeric vector, e.g.:
filepath <- "D:/Desktop/Masterarbeit/Daten/Test/"
files <- list.files(filepath, pattern=".csv")
#Iterate through every single file
for (file in files){
name2store <- strsplit(file, "[.]")[[1]][1]
path2read <- file.path(filepath, file)
data <- read.csv(file =path2read, header = TRUE)
mypath <- file.path("D:/Desktop/Masterarbeit/Daten/Results/", paste("Messstelle_", name2store, ".png", sep = ""))
png(file=mypath)
mytitle = paste("Messstelle", name2store)
plot(data$year, data$gwl,
pch = 19, #--> solid circle
cex = 1.5, #--> make 150% size
main = name2store,
xlab = "Year",
ylab = "Ground water level",
)
dev.off()
}

How to get pathview plot displayed directly (rather than saving as a file) in R?

Here is an example and the output is a png file hsa04110.gse16873.png. My question is how to get the plot displayed directly rather than save it as a file.
library(pathview)
data(gse16873.d)
data(demo.paths)
data(paths.hsa)
pathview(gene.data = gse16873.d[, 1], pathway.id = demo.paths$sel.paths[1],
species = "hsa", out.suffix = "gse16873", kegg.native = T)
#> 'select()' returned 1:1 mapping between keys and columns
#> Info: Working in directory D:/github/RNASeq-KEGG
#> Info: Writing image file hsa04110.gse16873.png
It looks like pathview only renders PNGs, so the trick is to define a function that will show the created PNG in the plot window and by default delete the file itself afterwards as if you were viewing it in memory. As an option, you can keep a copy of the PNG.
require(pathview)
require(grid)
require(png)
see_pathview <- function(..., save_image = FALSE)
{
msg <- capture.output(pathview::pathview(...), type = "message")
msg <- grep("image file", msg, value = T)
filename <- sapply(strsplit(msg, " "), function(x) x[length(x)])
img <- png::readPNG(filename)
grid::grid.raster(img)
if(!save_image) invisible(file.remove(filename))
}
Now when you do:
data(gse16873.d)
data(demo.paths)
data(paths.hsa)
see_pathview(gene.data = gse16873.d[, 1],
pathway.id = demo.paths$sel.paths[1],
species = "hsa",
out.suffix = "gse16873",
kegg.native = T)
You don't get any messages informing you about a written file, there is no new png in your working directory, and this picture pops up in the viewer pane:

Move pictures into folders based on Creation Date

I want to catalog a bunch of pictures from a source folder and move them based on their creation date. So, if a file was created on 2017-10-31, it would move the picture to a parent folder of 2017 and into the "2017-10-31" sub-folder. If the folder does not exist, then it should create it.
i used the below to create a data listing of the files creation dates and year.
files_tmp <- dir("C:/Users/.../pictures/",
full.names = TRUE)
filedate <-
as.data.frame(file.info(files_tmp)$ctime)
names(filedate)[1] <- "date"
pics <- filedate %>%
mutate (createDate = format(date, "%Y-%m-%d"),
year = format(date, "%Y"))
You just need to set your data correctly. Moving the files to appropriate folders is then trivial. See comments in the code.
# create some fake data
cat("2017-01-01", file = "2017-01-01.txt")
cat("2018-05-11", file = "2018-05-11.txt")
cat("2018-09-09", file = "2018-09-09.txt")
# fetch all files - here I'm using to find exact dates and your method may vary
xy <- list.files(pattern = "\\d{4}-\\d{2}-\\d{2}\\.txt")
# prepare variables that will create (sub)folders
# having things in Date form makes extraction of year easy (and safe)
xy <- data.frame(file = xy, date = as.Date(gsub("\\.txt", "", xy)), stringsAsFactors = FALSE)
xy$path <- sprintf("./%s/%s", format(xy$date, "%Y"), xy$date)
# check that folder(s) exist
subfolders <- as.character(unique(xy$path))
# create those that do not exist
cs <- xy[!sapply(subfolders, FUN = dir.exists), ]
# if there's no data, it means all appropriate folders are already created
if (nrow(cs) == 0) {
message("All folders already exist.")
} else {
sapply(cs$path, dir.create, recursive = TRUE)
}
# prepare paths where to move files to
xy$dest_to <- sprintf("%s/%s", xy$path, xy$file)
# execute the final move
mapply(FUN = function(x, y) {
message(sprintf("Moving file %s to %s", x, y))
file.rename(from = x, to = y)
}, x = xy$file, y = xy$dest_to)

Read and Convert .pdf files to .txt files in R faster?

I am trying to .pdf files (most of which are image based) to .txt files in bulk. The below program successfully converts both text and image based pdfs to text files.
My problem is that there is a set of ~15 pdf files that take a really long time to convert. They aren't particularly large (maximum pages between 10 to 600) but my program takes about 45 mins to convert them.
Why is it taking so long to convert them and how can I speed it up? I am using CRAN RGui(64-bit) and the R version 3.5.0
The .pdf files are in the following hirarchy
My Directory->Sub-folder 1->abc.pdf
My Directory->Sub-folder 2->def.pdf
etc..
The code is as below:
programdir<-"C:\\My directory"
# Delete all txt files in the path
file.remove(list.files(path=programdir, pattern = ".txt", recursive = T, full.names = T))
# Get list of sub folders in the main directory
mydir<-list.dirs(path=programdir,full.names = TRUE, recursive = TRUE)
# Loop through sub-folders, starting from 2 as 1 is the parent directory
for(i in 2:length(mydir)) {
# make a vector of PDF file names
myfiles <- list.files(path=mydir[i],pattern = ".pdf",
full.names = TRUE,recursive = TRUE)
# Loop through every file in the sub-directory
for(j in 1:length(myfiles)) {
# Render pdf to png image
img_file <- pdftools::pdf_convert(myfiles[j], format = 'tiff', dpi = 400)
# Extract text from png image
pdftotext <- ocr(img_file)
# Ensure text files are named as per sub-directory name_pdf name.txt format
fname = paste(mydir[i],basename(file_path_sans_ext(myfiles[j])),sep="_")
# Save files to directory path
sink(file=paste(fname , ".txt", sep=''))
writeLines(unlist(lapply(pdftotext , paste, collapse=" ")))
sink()
j <- j + 1 # Next file in sub-directory
}
i <- i + 1 # Next sub-directory record
}
file.remove(list.files(pattern = ".tiff", recursive = TRUE, full.names = TRUE))

Loop for grayscale png files in R

I read png file 28x28px and then I select grayscale:
img = readPNG("../Folder/image.png")
pic <- img[,,1]+img[,,2]+img[,,3]
pic <- pic/max(pic)'
result = pic*255
result
And now i try make loop for more png files in some folder:
img = lapply(sprintf("image%i.png", 1:10), png::readPNG) #select 10 png files
pic <- img[,,1]+img[,,2]+img[,,3]
pic <- pic/max(pic)'
result = pic*255
result
But it does not work :(
Error in img[, , 1] : incorrect number of dimensions
lapply creates a list of images, so in your case img[[1]] is the first image and img[[10]] is the 10th image. Try looping through each image in list. So if you wanted to do the do the addition operation on image 1, you'd need to do:
pic <- img[[1]][,,1]+img[[1]][,,2]+img[[1]][,,3]
To do this over all items in the list.
img.list <- lapply(sprintf("../Folder/image%i.png", 1:10), png::readPNG) #select 10 png files
results <- list()
for (img in img.list) {
pic <- img[,,1]+img[,,2]+img[,,3]
pic <- pic/max(pic)
results[[length(results)+1]] <- pic*255
}
Now result is a list of results for all images.

Resources