0

I am creating a django Project where I had Stored a Picture(Image) in database using ImageField as...

original_pic = models.ImageField()

Also, I want to Store an Image which will Contain the Same Picture(Image) as original_pic with Watermark in another ImageField as..

display_pic = models.ImageField(null=True, blank=True)

In short, I just want to Apply Algorithm on original_pic and save the result in watermark_pic using django models

Algorithm(Logic) for Applying Watermark to image is as follows...

def watermark_image_with_text(filename):
   text = 'PicMesh'
   color = 'blue'
   fontfamily = 'arial.ttf'
   image = Image.open(filename).convert('RGBA')
   imageWatermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
   draw = ImageDraw.Draw(imageWatermark)
   width, height = image.size
   font = ImageFont.truetype(fontfamily, int(height / 20))
   textWidth, textHeight = draw.textsize(text, font)
   x = width / 5
   y = height / 6
   draw.text((x, y), text, color, font)
   my_image = Image.alpha_composite(image, imageWatermark)
   my_image.convert('RGB').save('D:\Github\PicMesh\media\water_'+
   filename.name + '.png')
   return 'D:\Github\PicMesh\media\water_'+filename.name + '.png'

My Models.py contains following Photo model that overwrites save method to store value in display_pic.

class Photo(models.Model):
format_of_tags = (
    ('PNG', 'PNG'),
    ('JPG', 'JPG'),
    ('JPEG', 'JPEG'),
    ('Exif', 'Exif'),
    ('TIF', 'TIF'),
    ('GIF', 'GIF'),
    ('WEBP', 'WEBP'),
    ('SVG', 'SVG'),
)
title = models.CharField(max_length=150)
format = models.CharField(max_length=20, choices=format_of_tags, blank=False)
tags = models.CharField(max_length=250)
original_pic = models.ImageField()
display_pic = models.ImageField(null=True, blank=True)
description = models.CharField(max_length=1000)
photographer = models.ForeignKey('Photographer', on_delete=models.CASCADE)
category = models.ForeignKey('Categories', on_delete=models.CASCADE, default=0)

# Overwrites save method and set value of display_pic by default
def save(self, *args, **kwargs):
    if not self.pk:
        rotate_img_name = watermark_image_with_text(self.original_pic)
    self.display_pic = rotate_img_name
    super().save(*args, **kwargs)

the Problem with the code is that it handles all formats of image perfectly for e.g- png,jpg,jpeg,etc but it is unable to handle image of format tif.

I got OSError when save() method is invoked error trackback is as follows...

`Internal Server Error: /admin/home/photo/add/
Traceback (most recent call last):
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\contrib\admin\options.py", line 607, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\utils\decorators.py", line 140, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\contrib\admin\sites.py", line 223, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\contrib\admin\options.py", line 1647, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\utils\decorators.py", line 140, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\contrib\admin\options.py", line 1536, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\contrib\admin\options.py", line 1575, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.1b1-py3.6.egg\django\contrib\admin\options.py", line 1094, in save_model
    obj.save()
  File "D:\Github\PicMesh\home\models.py", line 53, in save
    rotate_img_name = watermark_image_with_text(self.original_pic)
  File "D:\Github\PicMesh\home\models.py", line 16, in watermark_image_with_text
    image = Image.open(filename).convert('RGBA')
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\Image.py", line 892, in convert
    self.load()
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\TiffImagePlugin.py", line 1061, in load
    return self._load_libtiff()
  File "C:\Users\abx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\TiffImagePlugin.py", line 1153, in _load_libtiff
    raise IOError(err)
OSError: -2
[29/Aug/2018 10:15:37] "POST /admin/home/photo/add/ HTTP/1.1" 500 132413`
6
  • 1
    What you got while uploading a TIF image?
    – JPG
    Aug 29, 2018 at 4:25
  • I got OSError when save method is invoked..... Aug 29, 2018 at 4:28
  • Add the error traceback
    – JPG
    Aug 29, 2018 at 4:32
  • @JPG I added it.... Aug 29, 2018 at 4:38
  • 1
    Where? I couldn't see anything related to those
    – JPG
    Aug 29, 2018 at 4:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.