[Image-SIG] Re: Image-SIG Digest, Vol 10, Issue 11

gerard coing gerard.coing at culture.gouv.fr
Mon Feb 23 04:44:14 EST 2004


Thank you Fredrik
I have to apologize because i forgot to tell you that I first do a 
resize. In fact my input images are 2000x3000 px and I have to output 
"full screen" (768x512 px) and thumbnails (192x192 px) in JPEG format 
for the web.
For example to produce thumbnails I use following script :

for infile in glob.glob("*.psd"):
      f, e = os.path.splitext(infile)
      outfile = f + "_V.jpg"
      im = Image.open(infile)
      lg, ht = im.size[0], im.size[1]
      if lg > ht and lg > 192:
          k = 192.0 / lg
      elif ht > 192:
          k = 192.0 / ht
      im=im.resize((int(lg*k+0.5),int(ht*k+0.5)), Image.ANTIALIAS)
      enhancer=ImageEnhance.Sharpness(im)
      im=enhancer.enhance(1.3)
      im.save(outfile)

Full screen images must not be larger than 150 Ko and thumbnails 8 Ko. 
What I intented to do was looping through quality value until I got the 
desired weight and then break, but I had not found the syntax to  :
1) get the file size of an image
2) set the quality when saving to JPEG
Assuming that "filesize = nk" and "save with quality=n" are the answers 
I 'm going to do tests

Cheers
gc

Le samedi, 21 fév 2004, à 18:03 Europe/Paris, 
image-sig-request at python.org a écrit :

>
>> New to Python & PIL
>> I have to do image batch converting and resizing from TIFF to JPEG 
>> with
>> a MAXIMUM WEIGHT (150 Ko) for the output.
>> Could someone tell me what is the syntax to get the weight of an 
>> output
>> image and if there is another way than looping through the output 
>> until
>> the desired weight is got.
>
> I assume you mean image size (in bytes).
>
> unlike many video formats, JPEG doesn't support "constant bitrate", so 
> the
> size of the output file will vary depending on the contents of the 
> image.
>
> a typical colour image is compressed about 20 times at the default 
> quality,
> so your 150k limit corresponds to a 1000x1000-pixel image.  If the 
> image is
> only slightly larger, you can try dropping the quality somewhat.  
> However,
> if the image is much larger, it's probably better to resize it than to 
> tweak
> the JPEG quality settings.  I'd try something like:
>
>     width, height = im.size
>     if width > 3000 or height > 3000:
>         # really large image; resize before compression
>         im.thumbnail((1000, 1000), Image.ANTIALIAS)
>     save with default quality (75)
>     if filesize > 150k:
>         if width > 2000 or height > 2000:
>                 # large image; dropping the quality isn't likely to 
> help
>                 im.thumbnail((1000, 1000), Image.ANTIALIAS)
>                 save with default quality (75)
>                 if filesize > 150k:
>                     save with quality=50
>         else:
>                 # try dropping the quality before resizing
>                 save with quality=50
>                 if filesize > 150k:
>                     im.thumbnail((1000, 1000), Image.BICUBIC)
>                     save with default quality (75)
>                     if filesize > 150k:
>                         save with quality=50
>                         # if the 150k limit is absolute, add more 
> tests here
>
> (tweak the numbers as necessary)
>
> (if most images are rectangular, it's probably a good idea to calculate
> the "thumbnail" size based on the aspect ratio for each image)
>
> </F>



More information about the Image-SIG mailing list