Issue
I'm using Django to create a website where you can upload an image on that website and check if the image contains Moire pattern. Here is the project structure:
In file settings.py, I specified the following directory for media files:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
and in file views.py, I implemented the API that will receive the image and detect moire pattern like this:
from django.core.files.storage import default_storage
def index(request):
if request.method == 'POST':
f = request.FILES['sentFile']
response = {}
fname = 'pic.jpg'
fname2 = default_storage.save(fname, f)
file_url = default_storage.url(fname2)
image = Image.open(file_url)
pred_label = moire_detect(image)
response['label'] = pred_label
return render(request, 'frontpage.html', response)
else:
return render(request, 'frontpage.html')
However, when I try to open the image using Image module of Pillow, I got this error "No such file or directory: '/media/pic_O1TOyCK.jpg'".
I don't really understand what is happening here, because the path is correct. Any help would be really appreciated.
Solution
Image.open
doesn't work with url
but with normal path
.
But file_url
has /media/pic_O1TOyCK.jpg
which is relative url
and it can be used on HTML (to get http://localhost/media/pic_O1TOyCK.jpg
) but it can't be used as normal path to access directly local file.
You should rather use fname2
with Image.open()
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.