473,854 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple script to make .png thumbnails from .zip archive...

Hi.
I'm looking for a small script that will take a .zip archive and pull
the first .jpg from the archive and convert it to a .png.

The reason for this is I want to have tuhmbnails for these archives in
nautilus under gnome. I would like something similar to the following
code, which will pull a thumbnail from an openoffice.org (oasis)
document. What I want is a little more involved, I guess, since I
don't know the name of the file (for the zip.read command), and I need
to convert the file from .jpg to .png once I get it. Any help would be
appreciated. Including a pointer to a web page of a manual with
examples. :-)

#!/usr/bin/python

import zipfile
import sys
import gnomevfs

inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
outURL=sys.argv[2]

zip=zipfile.Zip File(inURL,mode ="r")
picture=zip.rea d("Thumbnails/thumbnail.png")
thumbnail=open( outURL,"w")
thumbnail.write (picture)
thumbnail.write ("/n")
zip.close()
thumbnail.close ()

Jun 18 '06 #1
9 3585
Hi,

I don't know zipfile by heart, but python official documentation is
always good ( docs.python.org ). You need a loop in the file list like
this:

for file in zip:
process(file)

Unfortunatelly, there are too many ways to create a thumbnail from an
image. I'll cite one, using the "python image" external module, that
I've found to be very easy:

import Image
def process(file):
try:
image = Image.open(file )
image.thumbnail ((128,128), Image.ANTIALIAS )
image.save (file + '.thumb.png')
except:
print 'Skipping file', file

Links:
http://docs.python.org/lib/lib.html - Python Library Reference
http://www.pythonware.com/library/pi...book/image.htm - The Image
Module

K P S wrote:
Hi.
I'm looking for a small script that will take a .zip archive and pull
the first .jpg from the archive and convert it to a .png.

The reason for this is I want to have tuhmbnails for these archives in
nautilus under gnome. I would like something similar to the following
code, which will pull a thumbnail from an openoffice.org (oasis)
document. What I want is a little more involved, I guess, since I
don't know the name of the file (for the zip.read command), and I need
to convert the file from .jpg to .png once I get it. Any help would be
appreciated. Including a pointer to a web page of a manual with
examples. :-)

#!/usr/bin/python

import zipfile
import sys
import gnomevfs

inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
outURL=sys.argv[2]

zip=zipfile.Zip File(inURL,mode ="r")
picture=zip.rea d("Thumbnails/thumbnail.png")
thumbnail=open( outURL,"w")
thumbnail.write (picture)
thumbnail.write ("/n")
zip.close()
thumbnail.close ()


Jun 18 '06 #2
hdante wrote:
.... there are too many ways to create a thumbnail from an
image. I'll cite one, using the "python image" external module, that
I've found to be very easy:

import Image
def process(file):
try:
image = Image.open(file )
image.thumbnail ((128,128), Image.ANTIALIAS )
image.save (file + '.thumb.png')
except:
print 'Skipping file', file

Links:
http://docs.python.org/lib/lib.html - Python Library Reference
http://www.pythonware.com/library/pi...book/image.htm - The Image
Module


That, by the way, is the "PIL" library that you'll see a lot about --
The Python Imaging Library that the effbot is justly proud of. You
won't do better than that.

--Scott David Daniels
sc***********@a cm.org
Jun 18 '06 #3
Thanks a lot.
I'm really new to python, and haven't coded in over a decade, so
please be patient. :-)

I'm able to read a .jpg from a .zip archive, but can't seem to
manipulate it. If I do this:

zip=zipfile.Zip File(inURL,mode ="r")
picture=zip.rea d("00.jpg")

I get the image, but it is of "type" ZipFile. How can I change it to
type Image? Or am I thinking about this in the wrong way? I would
like to follow this with something like:

picture.thumbna il((128, 128), Image.ANTIALIAS )

But obviously I can't do this directly. What am I missing?

hdante wrote:
Hi,

I don't know zipfile by heart, but python official documentation is
always good ( docs.python.org ). You need a loop in the file list like
this:

for file in zip:
process(file)

Unfortunatelly, there are too many ways to create a thumbnail from an
image. I'll cite one, using the "python image" external module, that
I've found to be very easy:

import Image
def process(file):
try:
image = Image.open(file )
image.thumbnail ((128,128), Image.ANTIALIAS )
image.save (file + '.thumb.png')
except:
print 'Skipping file', file

Links:
http://docs.python.org/lib/lib.html - Python Library Reference
http://www.pythonware.com/library/pi...book/image.htm - The Image
Module

K P S wrote:
Hi.
I'm looking for a small script that will take a .zip archive and pull
the first .jpg from the archive and convert it to a .png.

The reason for this is I want to have tuhmbnails for these archives in
nautilus under gnome. I would like something similar to the following
code, which will pull a thumbnail from an openoffice.org (oasis)
document. What I want is a little more involved, I guess, since I
don't know the name of the file (for the zip.read command), and I need
to convert the file from .jpg to .png once I get it. Any help would be
appreciated. Including a pointer to a web page of a manual with
examples. :-)

#!/usr/bin/python

import zipfile
import sys
import gnomevfs

inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
outURL=sys.argv[2]

zip=zipfile.Zip File(inURL,mode ="r")
picture=zip.rea d("Thumbnails/thumbnail.png")
thumbnail=open( outURL,"w")
thumbnail.write (picture)
thumbnail.write ("/n")
zip.close()
thumbnail.close ()


Jun 19 '06 #4
Ant
Try adapting the other posters example with something like:

import Image, StringIO
zip=zipfile.Zip File(inURL,mode ="r")
picture=zip.rea d("00.jpg")
image = Image.open(Stri ngIO(picture))
image.thumbnail ((128,128), Image.ANTIALIAS )
image.save (file + '.thumb.png')

I haven't tested it, but something like this should work.

Jun 19 '06 #5
K P S wrote:
I'm able to read a .jpg from a .zip archive, but can't seem to
manipulate it. If I do this:

zip=zipfile.Zip File(inURL,mode ="r")
picture=zip.rea d("00.jpg")

I get the image, but it is of "type" ZipFile.


string, more likely (zip is a ZipFile object, picture is a string of
bytes). PIL expects a file name or a file object, so you need to use
the StringIO adapter:

picture = zip.read(name) # returns a string
file = StringIO.String IO(picture) # wrap string in file-like object
image = Image.open(file ) # create image object from file-like object

</F>

Jun 19 '06 #6
K P S wrote:
What I want is a little more involved, I guess, since I
don't know the name of the file (for the zip.read command)


the first example on this page shows you how to get the names of all
files in a zip file:

http://effbot.org/librarybook/zipfile.htm

</F>

Jun 19 '06 #7
Thanks everyone.

One last thing (I hope).

How can I get the name of just the first file in a zipfile? I see
routines to list all the files in a zip archive, but I don't see any to
list only the first, or only the second, etc. It doesn't look like
zipfile is storing info in a useful array that I can hash into. Or is
it?

Actually, I would like to get the name of the first file with a .jpg
extension, if that's not asking too much. :-)

zip=zipfile.Zip File("text.zip" ,mode="r")
picture=zip.rea d(zip.namelist( 0))

This doesn't seem to work. :-(

Jun 20 '06 #8
K P S wrote:
Thanks everyone. routines to list all the files in a zip archive, but I don't see any to
list only the first, or only the second, etc. It doesn't look like
If you can list all the files, then you can list only the first. :-)
Don't worry about python internal allocation procedures. It will try to
make things in an efficient way. Just make sure your code looks cool.
;-)

zip=zipfile.Zip File("text.zip" ,mode="r")
picture=zip.rea d(zip.namelist( 0))


That's because you're using the wrong syntax. -> zip.namelist()[0]

However, I bet that your first file in the zip is not a jpeg file. Do
this, instead:

zip = zipfile.ZipFile ('text.zip')
jpeglist = [x for x in zip.namelist() if '.jp' in x]
try:
picture = zip.read(jpegli st[0])
except IndexError:
print 'No jpeg found'

Jun 20 '06 #9
Thanks a lot! This is what I ended up with.
(I would like to get rar archive support, but browsing the web it looks
like rar support isn't in any python library (yet)) :-( Anyway, I was
able to use the below code unchanged to create thumbnails in nautilus
based on the first .jpg file in a .zip archive.

Is there any rar support module in python?

Thanks again.
#!/usr/bin/python

import zipfile
import sys
import gnomevfs
import Image
import StringIO

inURL=gnomevfs. get_local_path_ from_uri(sys.ar gv[1])
outURL=sys.argv[2]

zip=zipfile.Zip File(inURL,mode ="r")
jpeglist=[x for x in zip.namelist() if '.jp' in x]
try:
picture=zip.rea d(jpeglist[0])
except IndexError:
print 'No jpeg found'

zip.close() #close the file, since we no longer have need of it
image = Image.open(Stri ngIO.StringIO(p icture)) # create image object
from file-like object
image.thumbnail ((128,128),Imag e.ANTIALIAS) #create the thumbnail
image.save (outURL, "PNG") #output the file in the proper format

Jun 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3260
by: Preston Crawford | last post by:
I'm looking to quickly get a photo album online. Very simple, thumbnails, a few pages, maybe a description, but hopefully a small script that's easy to edit and work into my existing site. I know about hot scripts, etc. but I was wondering if any one could recommend one? Secondly, I also want to setup a journal. It's not really a "blog" although I guess blog software may work. But it isn't going to be a message board or anything like...
2
3710
by: Johan | last post by:
Hi, Where to find a php script to upload jpg files and make thumbnails of the jpg files ? Johan
4
2237
by: Summasummarum | last post by:
Hi ng, I need some input/suggestions for a very small layout. The situation: Some groupings of thumbnails. For every picture (thumbnail) there is a "big" picture. Thats it basically :) On the front the scenario is this: A user clicks "Autumn". The user is presented with the "Autumn" thumbnails. If he click a thumbnail, the corresponding big image is displayed. Well, u
3
1765
by: dan | last post by:
does anyone know why this script doesnt work propertly the images on the right side can not be displayed here is the script i use function initSettings () { for ( var i = 1; i <= 50; i++){ removeMovieClip("thumbnails.thumbBox" + i) } thumbSpacing = 72 thumbsInRow = 5
12
5759
by: pac | last post by:
I'm preparing to distribute a Windows XP Python program and some ancillary files, and I wanted to put everything in a .ZIP archive. It proved to be inordinately difficult and I thought I would post my solution here. Is there a better one? Suppose you have a set of files in a directory c:\a\b and some additional files in c:\a\b\subdir. Using a Python script, you would
2
3571
by: ranger7419 | last post by:
I'm trying to figure out why this script will work in IE 6 but not Firefox, and so I need someone here with a far better grasp on javascript to explain this. Basically, I have a page with several thumbnails. Above these thumbnails I placed a large picture with text next to it to describe what the picture is about. So, when you click a thumb, the large pic AND text change dynamically to reflect the thumbnail -- see...
25
4162
by: Gilles Ganault | last post by:
Hello I've been googling for a couple of hours, but still haven't found what I need: - really simple PHP-based image gallery software. Ideally, just a single file that I just drop into a directory filled with JPG files - uses eg. GD to generate thumbnails automagically, and saves them into a sub-directory for the next time (I don't want thumbnails to be regenerated every time someone views the pictures) - as an option, supports...
4
1425
by: Paul Furman | last post by:
I've set up a web gallery with 6 thumbnails per page, etc with php which is a big complicated mess because I'm not a brilliant programmer... it is the outgrowth of a couple classes I took at a community college & not my main career... anyways someone asked me to set up something like that for their web site and I'm wondering if there is a simpler php package to accomplish this task. I prepare thumbnails in a subfolder named thumbs and put...
3
5202
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
9899
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11023
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10672
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9509
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5936
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4147
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.