djangosnippets.org: Latest snippets tagged with 'imagefield'https://djangosnippets.org/tags/imagefield/2023-05-24T14:14:07.813738-05:00Image compression before saving the new model / work with JPG, PNG
2023-05-24T14:14:07.813738-05:00Schleidenshttps://djangosnippets.org/snippets/10949/<p>Don't forget to replace "self.image" by your image field name from your model ex ( self.cover )
replace Product by your model name</p>
<p>works pretty well :)</p>
Freely redistributableResize or Modify an image before saving
2016-12-29T10:30:09.721190-06:00asurahttps://djangosnippets.org/snippets/10597/<p>Small snippet that will resize all images before they uploaded to the server. </p>
Freely redistributableDynamic Thumbnail for ImageField
2016-01-21T05:28:41.565679-06:00agusmakmunhttps://djangosnippets.org/snippets/10549/<p>Dynamic Thumbnail for <code>ImageField</code> with <code>Pillow</code>.</p>
Freely redistributableDownload an image using requests, resize it using PIL, and save it to a model ImageField.
2015-03-24T19:31:51.304922-05:00stewxhttps://djangosnippets.org/snippets/10473/<p>You can use this as a model method.</p>
Freely redistributableImage compression and conversion to JPG format before uploading the file
2015-01-26T09:57:55.487301-06:00monikkinomhttps://djangosnippets.org/snippets/10460/<p>I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully. </p>
<p>You need to have PILLOW installed for this to work.</p>
Freely redistributableImage compression and conversion to JPG format before uploading the file
2015-01-26T09:46:48.316864-06:00monikkinomhttps://djangosnippets.org/snippets/10459/<p>I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully. </p>
Freely redistributableSave an image to ImageField from URL
2012-10-27T22:46:09.646734-05:00ekinertachttps://djangosnippets.org/snippets/2838/<h4>this is the right and working way</h4>
Freely redistributableFile deletion of FileField and ImageField in the Admin panel
2012-07-04T12:32:55.339980-05:00davmuzhttps://djangosnippets.org/snippets/2782/<p>This code runs well on Django 1.4 also with the Admin panel.
It's important to get the storage and the path before delete the model or the latter will persist void also if deleted.</p>
Freely redistributableRemoving old ImageFields and FileFields when updating through admin
2012-05-04T06:03:10.000027-05:00alejandro.alonsohttps://djangosnippets.org/snippets/2742/<p>Example:</p>
<p>admin.site.register(YourCoolModel, CustomModelAdmin)</p>
Freely redistributableget and image object
2012-03-01T15:56:47.455700-06:00grillermohttps://djangosnippets.org/snippets/2699/<p>I use this snippet to save images to my imagefields on django models.
This uses the very awesome requests library, so install it first</p>
<p>pip install requests</p>
<p>You probably want to have this be done on a celery queu, if the image is big enough.</p>
Freely redistributableClearableFileInput with image preview
2011-10-26T13:13:08.403006-05:00pafahttps://djangosnippets.org/snippets/2581/<p>This widget allows you to display preview images with adjustable width and length of the link:</p>
<p><a href="http://img526.imageshack.us/img526/6588/screenshotat20111026215.png">example</a></p>
<p>AdvancedFileInput(preview=True, image_width=200)</p>
<p>For other files, you can adjust the length of the link without preview:</p>
<p><a href="http://img845.imageshack.us/img845/6588/screenshotat20111026215.png">example</a></p>
<p>AdvancedFileInput(preview=False, url_length=30)</p>
<p>by default, parameters are:</p>
<p>preview = True</p>
<p>url_length = 30</p>
<p>image_width = 200</p>
Freely redistributableLoad File From URL Widget
2011-08-21T18:37:33.866375-05:00bryanhelmighttps://djangosnippets.org/snippets/2520/<p>Rather simple usage, modelforms/in the admin:</p>
<pre><code>class CustomAdminForm(forms.ModelForm):
class Meta:
model = Something
widgets = {
'image': URLFileInput(default_exts=[".png", ".gif", ".jpg"]),
}
class SomethingAdmin(admin.ModelAdmin):
form = CustomAdminForm
admin.site.register(Something, SomethingAdmin)
</code></pre>
<p>Basically, this will pull the image from the URL instead of only pulling it from your harddrive for upload.</p>
<p>Also accepts optional …</p>
Freely redistributableImageField with specified maximum image size
2010-03-21T11:20:25.703036-05:00virhilohttps://djangosnippets.org/snippets/1966/<p>You can specyfy max width and height which image can have, and it never exceed that size.</p>
Freely redistributableUpdated Filter to resize a ImageField on demand (ver.2)
2010-01-25T11:35:28.660240-06:00rawjamhttps://djangosnippets.org/snippets/1887/<p>An update to snippet 1718 (http://www.djangosnippets.org/snippets/1718/). This update lets you pass an image URL string as well as a model ImageField instance. This means that you can then create thumbnails on demand for images outside of model</p>
Freely redistributableImageField for Google App Engine
2009-11-17T12:46:21.682897-06:00davepeckhttps://djangosnippets.org/snippets/1805/<p>This is a replacement for Django's built-in ImageField. It uses the Google AppEngine image APIs in order to validate. </p>
<p>Notes:</p>
<ol>
<li>Validation of the field counts against your App Engine transformations quota.</li>
<li>This code assumes you're only using the in-memory file upload handler. None of the other stock handlers work well …</li></ol>
Freely redistributable