I have a Python Script in Google Cloud Functions.
One of the parts of the script gets a list of URL's (in String format), which I'm currently storing as it is.
The problem I'm having is that in the local version of that script (the one I run on my computer) I download the files of those URL's into a local folder as I need to store those files in Firebase Storage but with a lower resolution and size.
Here is the code I'm using to download the images:
Here I run the full thing getting the urls, and later changing those URL's into jpg's.
#Images Section
imagesRaw = []
imagesSection = soup.find('div', {'class': 'src__GalleryContainer-sc-bdjcm0-7'})
imagesInfo = imagesSection.find_all('img', {'class': 'gallery-image__StyledImg-sc-jtk816-0'})
image1 = imagesInfo[0].get('src')
for image in imagesInfo:
img = image.get('data-flickity-lazyload-src')
imagesRaw.append(img)
imagesRaw.pop(0)
imagesRaw.insert(0, image1)
images = imagesRaw[:12]
imageFile = []
#Here we will store the images in local file
for image in images:
#First we change the ending from webp to jpg
newURL = image[:-4] + 'jpg'
print(newURL)
name = find_between(newURL, "_img", "/origin.jpg")
if name == "":
name = random_name()
print(name)
#Here the function to download the image
try:
dl_jpg(newURL, 'images/', name)
except:
break
#Here we Optimize the image to size 500 x 394 pixels
# And get the location for the new image
try:
path = optimizeImage(name)
except:
break
# We append the path to the Array of paths
imageFile.append(path)
This is the function I use to download the Image from the URL:
def dl_jpg(url, file_path, file_name):
full_path = file_path + file_name + '.jpg'
path = urllib.request.urlretrieve(url, full_path)
This is the function where I optimize te Image to my needs:
def optimizeImage(name) -> str:
foo = Image.open(os.path.join('images/', name + '.jpg'))
foo = foo.resize((525,394),Image.ANTIALIAS)
foo.save('images/' + name + '.jpg',optimize=True,quality=50)
print('Optimized Image: ' + name)
return 'images/' + name + '.jpg'
And Finally I get the images stored in Firebase Storage like this:
photos = []
for image in listing.photos:
fullpath = image #find_between(image, 'scrapping/', '.jpg') + '.jpg'
filename = fullpath[7:]
path = fullpath[0:6]
imagePath = path + '/' + filename
bucket = storage.bucket()
blob = bucket.blob('ListingImages/' + ref.id + '/' + filename)
blob.upload_from_filename(imagePath)
blob.make_public()
photos.append(blob.public_url)
The problem that I see is that I do have a folder called Images next to my Script in my local, however in Cloud Functions I don't know how to have something similar that can help me to just hold those files while it optimizes and uploads and then just delete the files.
Any ideas?
Related
I copy the images from the source folder to destination folder using shutil.copy() Then I rename the folder using os.rename. However, there is a problem. So if the destination folder is empty, then I will get the image that I copy from source folder and os.rename will rename the images. But once the new images from source folder comes in, it will rename again the image that already rename. For example, after I rename the image, it will be 1_01_411.jpeg. But once it runs again, it will be 1_01_411_412.jpeg as it is in while loop.
I will attach the image that shows the issue.
While True:
src = "/home/pi/Pictures/JB_CAM/"
dest = "/home/pi/windowshare/"
par ="*"
i=1
d = []
for file in glob.glob(os.path.join(src,par)):
f = str(file).split('\\')[-1]
for n in glob.glob(os.path.join(dest,par)):
d.append(str(n).split('\\')[-1])
if f not in d:
print("copied",f," to ",dest)
shutil.copy(file,dest)
else:
f1 = str(f).split(".")
f1 = f1[0]+"_"+str(i)+"."+f1[1]
while f1 in d:
f1 = str(f).split(".")
f1 = f1[0]+"_"+str(i)+"."+f1[1]
print("{} already exists in {}".format(f1,dest))
i =i + 1
shutil.copy(file,os.path.join(dest,f1))
print("renamed and copied ",f1 ,"to",dest)
i = 1
os.getcwd()
collection = "/home/pi/windowshare"
dest = "/home/pi/windowshare/"
for i, filename in enumerate(os.listdir(collection)):
os.rename(dest + filename, dest + filename.split(".")[0] +"_"+ db_sn+ ".jpeg)
I also use OpenCV to test.
src ="/home/pi/Pictures/JB_CAM/"
for img in os.listdir(src):
name,ext = os.path.splitext(img)
if ext ==".jpeg":
f1 = cv2.imread('/home/pi/Pictures/JB_CAM/'+img)
print(f1)
#status =cv2.imwrite("/home/pi/windowshare/"+img.split(".")[0]+"_"+"%d.jpeg" % db_sn,f1)
cv2.imwrite("/home/pi/windowshare/"+img.split(".")[0]+"_"+"%d.jpeg" % db_sn,f1)
However, the problem with this approach is it overwrites the db_sn value to the all the images. The goal is to copy or write(save) the files from source to destination folder and rename according to db_sn(serial number).
I have a folder that is structured like this
post
-----1
------10am
-----------images
-----2
-------10am
-----------images
-----3
------10am
-----------images
this folder is up to 31 with the same subfolder '10am' and inside that is another folder 'images'
in another folder, I have all the image and .txt files that that I need to copy based on folder name in python
So what I need to do now is copy
'2.jpg' inside 'post\2\10am\images' and
'2.txt' inside ''post\2\10am' and so on
here is my code so far:
import os,shutil
sampleFiles = r"\practice image and text"
destination = r"\posts"
time = '10am'
sample = os.listdir(sampleFiles)
# sample = ['10.jpg', '10.txt', '11.jpg', '11.txt', '13.png', '13.txt', '16.jpg', '16.txt', '17.jpg', '17.txt', '18.jpg', '18.txt', '2.jpg', '2.txt', '20.jpg', '20.txt', '23.jpg', '23.txt', '24.jpg', '24.txt', '25.jpg', '25.txt', '27.jpg', '27.txt', '3.jpg', '3.txt','4.jpg', '4.txt', '5.jpg', '5.txt', '6.jpg', '6.txt', '9.jpg', '9.txt']
for root, dirs, files in os.walk(destination):
for folderName in dirs:
#get root + foldername
rootWithFolder = os.path.join(root, folderName)
#get path to date
pathToDate = rootWithFolder.endswith(int(folderName)) # how to get the number?
# get path to image folders
if rootWithFolder.endswith('images'):
pathToImage = rootWithFolder
#copy .jpg files to pathToImage
shutil.copy(sampleFiles + '\\' + str(pathToDate) + '.jpg' , pathToImage) #not the most elegant way
#copy .txt files to pathToDate
shutil.copy(sampleFiles + '\\' + str(pathToDate) + '.txt' , pathToDate + '\\' + 'time') #not the most elegant way
in my code, I am stuck on how to get pathToDate so I can just copy it based on the name of the folder,
I tried using def like this:
def allfiles(list):
for i in range(len(list)):
return list[i] # returns only the first value of the list
# print(list[i]) #but this one returns all the value of the list
allfiles(sample)
but it only returns 1 instance of the list.
My question is, how can I get the folder that is named as a number and ignore the strings like 10am folder and images folder
or is there a better way to do this? Thank you
Here's a suggestion if you're still looking for a solution... I'd go the other way round:
from pathlib import Path
from shutil import copy
sample_folder = Path("practice image and text")
dest_folder = Path("posts")
for file in sample_folder.glob("*.*"):
number, suffix = file.name.split(".")
if suffix == "txt":
copy(file, dest_folder / number / "10am")
else:
copy(file, dest_folder / number / "10am" / "images")
I write a script for the Avidemux app to read a folder and get all video files in that folder then split them into a number of the part base on the file size.
the problem is that the input_ext variable which used in the get_folder_content function for checking the file format in the folder. it only gets one format and I can't set a list of formats to check it from the folder.
the input_ext variable only gets one format but I want to set a bunch of formats to it.
my code is this
adm = Avidemux()
gui = Gui()
# file set list of of input files
# input_ext = 'mp4'
input_ext = 'mkv'
# file extension for output files
output_ext = 'MKV'
def convert_file(input_file, output_folder):
file_size = get_file_size(input_file)
number_of_part = math.ceil(file_size/2037760000)#equal to 1990 MB 2037760000
if number_of_part >1:
for part in range(0,number_of_part):
file_name = " ".join(basename(input_file).split('.')[0:-1])
output_file = output_folder + '/' +file_name+'_part'+str(part+1)+'.mkv'
adm.loadVideo(input_file)
len = adm.markerB
adm.clearSegments()
adm.addSegment(0, 0, adm.markerB)
adm.markerA = (part/number_of_part)*len
adm.markerB = ((part+1)/number_of_part)*(len)
adm.videoCodec("Copy")
adm.audioClearTracks()
adm.setSourceTrackLanguage(0,"und")
if adm.audioTotalTracksCount() <= 0:
raise("Cannot add audio track 0, total tracks: " + str(adm.audioTotalTracksCount()))
adm.audioAddTrack(0)
adm.audioCodec(0, "copy")
adm.audioSetDrc(0, 0)
adm.audioSetShift(0, 0, 0)
adm.setContainer("MKV", "forceAspectRatio=False", "displayWidth=1280", "displayAspectRatio=2", "addColourInfo=False", "colMatrixCoeff=2", "colRange=0", "colTransfer=2", "colPrimaries=2")
adm.save(output_file)
def main():
input_folder = gui.dirSelect("Select the source folder")
# input_folder =
files = get_folder_content(input_folder, input_ext)
if files is None:
gui.displayError("Error", "Folder doesn't containt any ." + input_ext+ " file")
return 0
output_folder = gui.dirSelect("Select the output folder")
# output_folder =
for one_file in files:
convert_file(one_file, output_folder)
print("Done")
main()
I want to use input_ext like this :
input_ext = ['mkv','mp4']
Also, I can't find the lib document for tinypy to read function and find the solution
I'm trying to create a script that changes the contrast and sharpness of all the images in a folder, and save it to a new folder as filename_edited.jpg
I'm using pythong 3.7, importing os and PIL. I have a folder with some images - DJI_001.jpg, DJI_002.jpg and so forth. The folder with the images is called test.
The folder I want them to go to is called 'novo' in ..\test\novo
Initially I had this. It works, but it saves the images in the same folder:
import PIL
from PIL import Image, ImageEnhance
import os.path, sys
path = "C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test"
dirs = os.listdir(path)
def teste():
for item in dirs:
fullpath = os.path.join(path,item)
if os.path.isfile(fullpath):
img = Image.open(fullpath)
f, e = os.path.splitext(fullpath)
sharpness = ImageEnhance.Sharpness(img)
sharp = sharpness.enhance(10.0)
newimg = sharp
contrast = ImageEnhance.Contrast(newimg)
cont = contrast.enhance(2.3)
head, tail = os.path.split(fullpath)
cont.save(f + "_edited.jpg")
teste()
So after some research I tried to split fullpath in head and tail. The tail variable gets me the file name.
I did this so each time it looped, It could save to the filepath of my new folder + tail.
So I tried this:
def sha():
for item in dirs:
fullpath = os.path.join(path,item)
if os.path.isfile(fullpath):
img = Image.open(fullpath)
#f, e = os.path.splitext(fullpath) #don't need this here
sharpness = ImageEnhance.Sharpness(img)
sharp = sharpness.enhance(10.0)
newimg = sharp
contrast = ImageEnhance.Contrast(newimg)
cont = contrast.enhance(2.3)
head, tail = os.path.split(fullpath)
cont.save("C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test\\novo" + tail)
print("this is the filepath: " + head)
print("this is the filename: " + tail)
sha()
I thought this would work, but it saves the files on the same directory, as novoDJI_001.jpg, novoDJI_002.jpg and so forth.
I added a couple images if it helps:
Saving in the same folder
and
Trying to save on a new folder
So, on my second attempt (more like 20th but well), as you can see I inerted the filepath, but the new folder called novo, in \test\novo ended up in the file name.
Any help is greatly appreciated, I'm sure this is simple but I've spent the last 5 hours on this, but I can't find why it does this! Thank you!
There's no path separator in this line betweeen "novo" and tail:
cont.save("C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test\\novo" + tail)
Please, replace it with something like this:
cont.save("C:\\Users\\r o d r i g o\\Desktop\\001 - progamer\\98 - Image brightness\\test\\novo\\" + tail) // '\\' added after 'novo'
or you may use this as well:
new_name = os.path.join( head, "novo", tail )
cont.save( new_name )
You have to run it in the folder with a couple images and run shuffle_all_images() and it will create new folder and randomly generate all of the values for each pixel. I think it has to do with not converting to numpy images as opposed to PIL images?, but I can't figure it out.
import random
import os.path
import PIL
import numpy
def image_shuffle(original_image):
for row in len(original_image):
for col in len(original_image[1]):
r,g,b = original_image[row][col]
r = random.randint(1,255)
g = random.randint(1,255)
b = random.randint(1,255)
original_image[row][col] = [r, g, b]
return original_image
def get_images(directory=None):
""" Returns PIL.Image objects for all the images in directory.
If directory is not specified, uses current directory.
Returns a 2-tuple containing
a list with a PIL.Image object for each image file in root_directory, and
a list with a string filename for each image file in root_directory
"""
if directory == None:
directory = os.getcwd() # Use working directory if unspecified
image_list = [] # Initialize aggregaotrs
file_list = []
directory_list = os.listdir(directory) # Get list of files
for entry in directory_list:
absolute_filename = os.path.join(directory, entry)
try:
image = PIL.Image.open(absolute_filename)
file_list += [entry]
image_list += [image]
except IOError:
pass # do nothing with errors tying to open non-images
return image_list, file_list
def shuffle_all_images(directory=None):
""" Saves a modfied version of each image in directory.
Uses current directory if no directory is specified.
Places images in subdirectory 'modified', creating it if it does not exist.
New image files are of type PNG and have transparent rounded corners.
"""
if directory == None:
directory = os.getcwd() # Use working directory if unspecified
# Create a new directory 'modified'
new_directory = os.path.join(directory, 'modified')
try:
os.mkdir(new_directory)
except OSError:
pass # if the directory already exists, proceed
#load all the images
image_list, file_list = get_images(directory)
#go through the images and save modified versions
for n in range(len(image_list)):
# Parse the filename
filename, filetype = file_list[n].split('.')
# Round the corners with radius = 30% of short side
new_image = image_shuffle(image_list[n])
#save the altered image, suing PNG to retain transparency
new_image_filename = os.path.join(new_directory, filename + '.png')
new_image.save(new_image_filename)
The image_shuffle function is wrong.
It should be:
for row in range(original_image.size[0]):
for col in range(original_image.size[1]):
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
original_image.putpixel((row, col), (r,g,b))
You want to have color values starting from 0 unless you don't want to get all the possible colors.