I am writing a Django app which will fetch all images of particular URL and save them in the database.
But I am not getting on how to use ImageField in Django.
Settings.py
MEDIA_ROOT = os.path.join(PWD, "../downloads/") # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash. # Examples: "http://example.com/media/", "htp://media.example.com/" MEDIA_URL = '/downloads/'
models.py
class images_data(models.Model): image_id =models.IntegerField() source_id = models.IntegerField() image=models.ImageField(upload_to='images',null=True, blank=True) text_ind=models.NullBooleanField() prob=models.FloatField()
download_img.py
def spider(site): PWD = os.path.dirname(os.path.realpath(__file__ )) #site="http://en.wikipedia.org/wiki/Pune" hdr= {'User-Agent': 'Mozilla/5.0'} outfolder=os.path.join(PWD, "../downloads") #outfolder="/home/mayank/Desktop/dreamport/downloads" print "MAYANK:"+outfolder req = urllib2.Request(site,headers=hdr) page = urllib2.urlopen(req) soup =bs(page) tag_image=soup.findAll("img") count=1; for image in tag_image: print "Image: %(src)s" % image filename = image["src"].split("/")[-1] outpath = os.path.join(outfolder, filename) urlretrieve('http:'+image["src"], outpath) im = img(image_id=count,source_id=1,image=outpath,text_ind=None,prob=0) im.save() count=count+1
I am calling download_imgs.py inside one view like
if form.is_valid(): url = form.cleaned_data['url'] spider(url)
In django we can deal with the images with the help of model field which is ImageField . In this article, we have created the app image_app in a sample project named image_upload. The very first step is to add below code in the settings.py file. MEDIA_ROOT is for server path to store files in the computer.
In Django, a default database is automatically created for you. All you have to do is add the tables called models. The upload_to tells Django to store the photo in a directory called pics under the media directory. The list_display list tells Django admin to display its contents in the admin dashboard.
Most of the web applications deal with the file or images, Django provides the two model fields that allow the user to upload the images and files. These fields are FileField and ImageField; basically ImageField is a specialized version of FileField that uses Pillow to confirm that file is an image.
To save changes to an object that's already in the database, use save() . This performs an UPDATE SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() .
Django Documentation is always good place to start
class ModelWithImage(models.Model): image = models.ImageField( upload_to='images', )
UPDATED
So this script works.
.
import requests import tempfile from django.core import files # List of images to download image_urls = [ 'http://i.thegrindstone.com/wp-content/uploads/2013/01/how-to-get-awesome-back.jpg', ] for image_url in image_urls: # Stream the image from the url response = requests.get(image_url, stream=True) # Was the request OK? if response.status_code != requests.codes.ok: # Nope, error handling, skip file etc etc etc continue # Get the filename from the url, used for saving later file_name = image_url.split('/')[-1] # Create a temporary file lf = tempfile.NamedTemporaryFile() # Read the streamed image in sections for block in response.iter_content(1024 * 8): # If no more file then stop if not block: break # Write image block to temporary file lf.write(block) # Create the model you want to save the image to image = Image() # Save the temporary image to the model# # This saves the model so be sure that it is valid image.image.save(file_name, files.File(lf))
Some reference links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With