Chandan Pitta wrote:
> Ok I made the changes just apply the patch and it should make use of
> the fileops.py now.
> 
> However I am having doubts regarding the scaling using imlib2. Look at
> snapshot4.png and you will see that the images seem to be stretched
> out.
> I am using imlib2 v1.3 and giflib v4.1.4 with
> WWW_IMAGE_THUMBNAIL_SIZE = (200, 100)
> 
> If I use
> WWW_IMAGE_THUMBNAIL_SIZE = (200, 200)
> then I get what you see in snapshot5.png. Are you sure that the scale
> maintains aspect?

I agree, it doesn't look like it is. This is really a problem to be
solved in kaa.imlib2, or in the worst case in cache_www_image. Don't
worry, your code won't be lost just moved to where is should really go.

The size that is returned from cache_www_image is not used and so I've
done some changes to use the size that is returned and added a function
cache_www_image_size for this.

  scaled_image_path = util.cache_www_thumbnail_path(item)
  if not os.path.exists(scaled_image_path):
    size = util.cache_www_image(item)
  else:
    size = util.cache_www_image_size(item)
  image_link = self.convert_dir(filepath)

Let me know if this is incorrect; Jason should fix kaa.imlib2.

Cheers,
Duncan

> 
> Chandan
> 
> 
> 
> On 1/3/07, Duncan Webb <[EMAIL PROTECTED]> wrote:
>> Chandan Pitta wrote:
>> > Code reuse is always good. But once again the question arises, how
>> > does freevo handle changes to WWW_IMAGE_THUMBNAIL_SIZE if images are
>> > already cached. I am guessing we may need another function in
>> > fileops.py like
>>
>> freevo cache --rebuild should do it. It may not work until I've done the
>> code to delete files from the cache.
>>
>> >
>> > def is_image_cached(scaled_image_path):
>> >     new_size = config.WWW_IMAGE_THUMBNAIL_SIZE
>> >     new_size = self.get_fit_to_rectangle_size(size, new_size)
>> >     create_scaled_image = False
>> >     if os.path.exists(scaled_image_path):
>> >         # if the scaled image already exists then make sure it is
>> > scaled to the correct size
>> >         new_image = imlib2.open(scaled_image_path)
>> >         if new_image.width != new_size[0] or new_image.height !=
>> new_size[1]:
>> >             os.remove(scaled_image_path)
>> >             create_scaled_image = True
>> >     else:
>> >         create_scaled_image = True
>> >     return !create_scaled_image
>> >
>> > You will also want to add the timestamp check in the above function.
>> > And this is what we need to do both with freevo cache and webserver
>> >
>> > thumb_filename = util.cache_www_thumbnail_path(filename)
>> > if not util.is_image_cached(thumb_filename):
>> >     util.cache_www_image(filename)
>> >
>> > But if I were to do it, I would actually drag the checking for
>> > accuracy of the thumbnail into cache_www_image(filename). That way
>> > cache_www_thumbnail_path() is called only once and the main program
>> > will only have to do
>>
>> Doesn't matter it's upto kaa.imlib2 to do this, less work for the
>> webserver.
>>
>> > util.cache_www_image(filename)
>> >
>> > And the function will cache if there is no cached image or original
>> > image has changed or WWW_IMAGE_THUMBNAIL_SIZE has changed. It will do
>> > nothing otherwise. The caller will never have to know. Or we can add a
>> > return which tells the caller if a cached image is created or not. Let
>> > me know what you think
>>
>> Lets not try to change the design of cache, it would lead to too many
>> changes. Cache only works on time stamps and the version of the cache
>> generators.
>>
>> I don't expect people to change WWW_IMAGE_THUMBNAIL_SIZE much if at all.
>> so a cache rebuild is fine.
>>
>> Duncan
>>
>> >
>> > On 1/2/07, Duncan Webb <[EMAIL PROTECTED]> wrote:
>> >> Chandan Pitta wrote:
>> >>> Try this patch.
>> >> Patch worked fine now in svn rel-1 at r8917
>> >>
>> >> I have these functions in util.fileops.py which is now updated from
>> the
>> >> patch you have sent. They is called when "freevo cache" is run.
>> >>
>> >> def cache_www_thumbnail_path(filename):
>> >>     '''returns the path to the thumbnail image for a given filename
>> >>     '''
>> >>     file_ext_index = filename.rindex(".")
>> >>     file_ext = filename[file_ext_index:].lower()
>> >>     if file_ext == ".gif":
>> >>         file_ext += ".jpg"
>> >>     imagepath = filename[:file_ext_index].replace("/", "_") + file_ext
>> >>     thumb_path = os.path.join(www_image_cachedir(), imagepath)
>> >>     return thumb_path
>> >>
>> >> def cache_www_image(filename):
>> >>     '''creates a webserver thumbnail image and returns its size.
>> >>     '''
>> >>     thumb_path = www_cache_thumbnail_path(filename)
>> >>     image = imlib2.open(filename)
>> >>     thumb =
>> image.scale_preserve_aspect(config.WWW_IMAGE_THUMBNAIL_SIZE)
>> >>     thumb.save(thumb_path)
>> >>     return thumb.size
>> >>
>> >> What this means is that we're not quite finished yet :( sorry. Cache
>> >> needs to cache every image, size doesn't matter it's determined by the
>> >> timestamp between the original item and the cached item.
>> >>
>> >> The functions is called like this:
>> >>         sinfo = os.stat(filename)
>> >>         thumb = util.cache_www_thumbnail_path(filename)
>> >>         try:
>> >>             if os.stat(thumb)[stat.ST_MTIME] > sinfo[stat.ST_MTIME]:
>> >>                 files.remove(filename)
>> >>         except OSError:
>> >>             pass
>> >>
>> >
>> > I am assuming this is a code you came up with and did not actually
>> > commit to svn? Seems like you are checking to see if the thumbnail
>> > timestamp > original image TS then you delete the original file? The
>> > condition should be reversed and the thumnail image has to be deleted
>> > not the original right?
>> >
>> > Frankly I do not mind doing the code over as long as it is cleaner and
>> > more maintainable.
>> >
>> > Regards
>> > Chandan
>> >
>> >
>> >> All this means it that it makes this code simpler, you don't need to
>> >> worry about threshold sizes or aspect ratios, the helper functions in
>> >> util.fileops does this for you.
>> >>
>> >> So the library.rpy only needs to check if the
>> >> cache_www_thumbnail_path(path) exist and if not call
>> >> cache_www_image(thumb_path).
>> >>
>> >> This will be much cleaner and easier to maintain. When you get some
>> time
>> >> could you do the changes.
>> >>
>> >> Many thanks
>> >> Duncan
>> >>
>> >>
>> >>> On 1/2/07, Duncan Webb <[EMAIL PROTECTED]> wrote:
>> >>>> Chandan Pitta wrote:
>> >>>>> Sorry attached the wrong diff file. See this one
>> >>>> I can't apply this patch, says that it has been previously detected.
>> >>>>
>> >>>> Will you try an svn update, I applied Wout's patch at 8912 and your
>> >>>> one-liner after.
>> >>>>
>> >>>> Thanks
>> >>>> Duncan
>> >>
>> >>
>> >>
>> -------------------------------------------------------------------------
>> >> Take Surveys. Earn Cash. Influence the Future of IT
>> >> Join SourceForge.net's Techsay panel and you'll get the chance to
>> share your
>> >> opinions on IT & business topics through brief surveys - and earn cash
>> >>
>> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
>> >> _______________________________________________
>> >> Freevo-devel mailing list
>> >> [email protected]
>> >> https://lists.sourceforge.net/lists/listinfo/freevo-devel
>> >>
>> >
>> >
>> -------------------------------------------------------------------------
>> > Take Surveys. Earn Cash. Influence the Future of IT
>> > Join SourceForge.net's Techsay panel and you'll get the chance to
>> share your
>> > opinions on IT & business topics through brief surveys - and earn cash
>> >
>> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
>> > _______________________________________________
>> > Freevo-devel mailing list
>> > [email protected]
>> > https://lists.sourceforge.net/lists/listinfo/freevo-devel
>> >
>>
>>
>>
>> -------------------------------------------------------------------------
>> Take Surveys. Earn Cash. Influence the Future of IT
>> Join SourceForge.net's Techsay panel and you'll get the chance to
>> share your
>> opinions on IT & business topics through brief surveys - and earn cash
>> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
>> _______________________________________________
>> Freevo-devel mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/freevo-devel
>>
> 
> ------------------------------------------------------------------------
> 
> Index: src/www/htdocs/library.rpy
> ===================================================================
> --- src/www/htdocs/library.rpy  (revision 8922)
> +++ src/www/htdocs/library.rpy  (working copy)
> @@ -478,11 +478,13 @@
>                      ### show image
>                      if action_mediatype == "images":
>                          size = (info['width'], info['height'])
> -                        (scaled_image, new_size) = 
> self.get_scaled_image_and_size(item, size)
> +                        scaled_image_path = 
> util.fileops.cache_www_thumbnail_path(item)
> +                        if not os.path.exists(scaled_image_path):
> +                            util.fileops.cache_www_image(item)
>                          image_link = self.convert_dir(filepath)
> +                        scaled_image_link = 
> self.convert_dir(scaled_image_path)
>                          fv.tableCell('<div class="image"><a 
> href="javascript:openfoto(\''+image_link+'\','+str(size[0])+','+str(size[1])+')">'\
> -                            +'<img src="'+scaled_image+'" 
> height="'+str(new_size[1])+'px" width="'+str(new_size[0])+'px" />'\
> -                            +'<br />'+Unicode(title)+'</a></div>', 
> 'class="'+status+'" colspan="1"')
> +                            +'<img src="'+scaled_image_link+'" /><br 
> />'+Unicode(title)+'</a></div>', 'class="'+status+'" colspan="1"')
>                      ### show movie
>                      elif action_mediatype == "movies":
>                          if os.path.exists(jpg_file):
> @@ -635,61 +637,6 @@
>              new_height = 200
>          return (int(new_width), int(new_height + 0.5))
> 
> -    def get_fit_to_rectangle_size(self, size, new_size):
> -        print 'get_fit_to_rectangle_size(self, size=%s, new_size=%s)' % 
> (str(size), str(new_size))
> -        try:
> -            scaled_width = new_size[0]
> -            scaled_height = new_size[1]
> -            ### if actual image aspect ratio > scaled image aspect ratio 
> then scale height
> -            if float(size[0]) / size[1] > float(scaled_width) / 
> scaled_height:
> -                scaled_height = scaled_width * size[1] / size[0]
> -            ### else scale width
> -            else:
> -                scaled_width = scaled_height * size[0] / size[1]
> -        except ZeroDivisionError:
> -            pass
> -        return (scaled_width, scaled_height)
> -
> -    def get_scaled_image_and_size(self, filepath, size):
> -        '''
> -        Returns the location of a scaled image and size of the scaled image
> -        as a 2-tuple. Eg. ("/var/cache/freevo/test.jpg", (200, 150)).
> -        The image will be scaled only if it larger than the threshold size
> -        in config.WWW_IMAGE_THRESHOLD_SIZE. The scaled size will be limited
> -        to the boundaries of config.WWW_IMAGE_THUMBNAIL_SIZE
> -        '''
> -        print 'get_scaled_image_and_size(self, filepath=%r, size=%s)' % 
> (filepath, str(size))
> -        threshold_size = config.WWW_IMAGE_THRESHOLD_SIZE
> -        new_size = config.WWW_IMAGE_THUMBNAIL_SIZE
> -        new_size = self.get_fit_to_rectangle_size(size, new_size)
> -        file_ext_index = filepath.rindex(".")
> -        file_ext = filepath[file_ext_index:].lower()
> -        if file_ext.lower() == ".gif":
> -            file_ext += ".jpg"
> -        scaled_image_path = self.cache_dir + 
> filepath[:file_ext_index].replace("/", "_") + file_ext
> -
> -        # if the size of image falls below threshold size then use original 
> image
> -        if size[0] < threshold_size[0] and size[1] < threshold_size[1]:
> -            scaled_image_path = filepath
> -        else:
> -            create_scaled_image = False
> -            if os.path.exists(scaled_image_path):
> -                # if the scaled image already exists then make sure it is 
> scaled to the correct size
> -                new_image = imlib2.open(scaled_image_path)
> -                if new_image.width != new_size[0] or new_image.height != 
> new_size[1]:
> -                    os.remove(scaled_image_path)
> -                    create_scaled_image = True
> -            else:
> -                create_scaled_image = True
> -
> -            if create_scaled_image:
> -                image = imlib2.open(filepath)
> -                new_image = image.scale(new_size)
> -                new_image.save(scaled_image_path)
> -
> -        scaled_image_path = self.convert_dir(scaled_image_path)
> -        return (scaled_image_path, new_size)
> -
>      def get_fxd_title(self, fxd_file):
>          print 'get_fxd_title(self, fxd_file=%r)', (fxd_file)
>          fxd_info = ""
> 
> 
> 
> ------------------------------------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys - and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Freevo-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freevo-devel



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-devel

Reply via email to