I want to find original path of the uploaded image but 'image_path' gives me the location of the project. Is it possible to save the path where the uploaded image is located?
def image_data():
data = {
'full_image': request.files['image'],
'image_name': request.files['image'].filename,
'image_path': os.path.realpath(request.files['image'].filename)
}
return data
As in the path of the user who uploaded the file? The file is uploaded, not the path of the user, also you wouldn't have much value in this information.
The path of the image is where it is currently stored for Flask, in the project, where the uploaded image file is probably located as well.
Related
return redirect(--),os.path.abspath(filename)
Return statement is not giving both of them as output
Why ?
I am trying to upload a file using the flask and boto3 module to one of my Amazon S3 buckets. My code does not just upload the file, but it also uploads the folder where that file is stored. Can somebody please help me with that. If I am already providing the path of the file in the code which you can see below. How does the upload thing work in the Html button?
#app.route("/upload", methods=['POST'])
def upload():
if request.method == "POST":
f = request.files['file']
f.save(os.path.join(UPLOAD_FOLDER, f.filename))
upload_file(f"readme/{f.filename}", BUCKET)
return redirect("/storage")
Folders do not actually exist in Amazon S3. If you upload a file to a folder, the folder will magically 'appear'. Later, if you delete all files in the (pretend) folder, then the folder will disappear.
If you use the "Create Folder" button in the S3 management console, it actually creates a zero-length object with the same name of the folder. This 'forces' the folder to appear because it contains an object (but that object isn't displayed).
So, when you say "it also uploads the folder where that file is stored", you are probably just seeing the folder name 'appear'. It probably only uploaded one file.
I would like to upload a set of files with each file to a different destination folder in mega.nz using python. I am trying to do so with the library mega.py 1.0.8 (https://pypi.org/project/mega.py/) developed by https://odwyer.software. Thank you guys for this great tool!.
Looking at the examples provided at the library site, I was able to upload a file to root directory in MEGA and also to a folder in the root (path “folderA”) with the following commands:
from mega import Mega
mega = Mega()
m = mega.login(“userX", "Mypasswd" )
#Upload to root in mega
file = m.upload('/home/userX/test/meganz/A.pdf')
m.get_upload_link(file)
#add to a specific folder in root
folder_destination = m.find('folderA')
m.upload('/home/userX/test/meganz/A.pdf', folder_destination[0])
But when I try to specify a different path with sub-folders, for example “folderA/folderB/randomname”. In mega, the path looks like:
Screenshot of path in mega platform and I get an error:error message in the console
"TypeError: 'NoneType' object is not subscriptable"
The code when I get the error is (I just changed the path):
folder_destination2 = m.find('folderA/folderB/randomname')
m.upload('/home/userX/test/meganz/A.pdf', folder_destination2[0])
I change the path to other subfolders but I get the same error. What could be the problem? I don’t see what I am missing. Thank you very much for your help.
Have you tried:
folder_destination = m.find('folderA')
folder_destination2 = folder_destination.find('folderB')
Enter the last subfolder, the place where the file will be uploaded
folder = m.find('randomname')
and upload
m.upload('file_to_upload.pdf', folder[0])
I have tried and this worked.
Use the find_path_descriptor:
folder_Id = m.find_path_descriptor('folderA/folderB/randomname')
m.upload('file_to_upload.pdf', folder_Id)
Hi I have a project that the images upload and save in a sub folder in the media and never set an image size so now the images save 4mb and ended up totalling to 40GB in size.
I know how to write a script if the images were in a single folder but could someone guide me to do this to check all images in a folder and resize it ? Even if its in a sub folder and another sub folder.
Using Django and python
Function for image upload
def artwork_theme_name(instance, filename):
path, name = get_hashed_upload_to(instance.id, filename)
return 'theme/{}/{}'.format(path, name)
Upload model
class ArtworkForeground(models.Model):
title = models.CharField(_("title"), max_length=128)
description = models.TextField(_("description"))
foreground = models.ImageField(_("foreground image"), upload_to=artwork_theme_name)
Here I'm overwrite the existing image with reduced size image. The artwork.foreground is a file-pointer and we passed the fp to the PIL class. The save() method of Image class takes filename/path-conatining-filename as first argument. At that point the .path attribute became handy
from PIL import Image
def do_resize(image):
pil_img = Image.open(image)
resize_limit = 100, 150
pil_img.thumbnail(resize_limit, Image.ANTIALIAS)
pil_img.save(image.path)
for artwork in ArtworkForeground.objects.filter(foreground__isnull=False):
do_resize(artwork.foreground)
UPDATE-1
Where should I put this snippet?
If you set some kind of restriction or validation from Now Onwards, just run this script in your Django Shell ( This is only for a one time use)
You could use os.walk() function to go through all files and subfolders.
for (path, dirs, files) in os.walk(path):
for file in files:
filename = os.path.join(path, file)
if filename.endswith('.jpg'):
resize_image(filename)
I have a database model that in the Django model all I need to do is upload an image file to a new database record. Is there anyway I can automate this as I have a lot of images to upload? All images will come from a folder on my computer and each picture will be added to a new database record. All help is appreciated. Thanks
Just run a simple script to save files stored in a particular folder
from django.core.files import File
class MyModel(models.Model):
picture=models.ImageField()
MyModel.picture.save('abc.png', File(open('/tmp/pic.png', 'r')))
To do this for all files in a directory -
BASE_PATH = '/home/somefolder'
files = os.listdir(BASE_PATH)
for file in files:
MyModel.picture.save(file, File(open(os.path.join(BASE_PATH, file), 'r')))