473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.argv[1])
outURL=sys.argv[2]

zip=zipfile.ZipFile(inURL,mode="r")
picture=zip.read("Thumbnails/thumbnail.png")
thumbnail=open(outURL,"w")
thumbnail.write(picture)
thumbnail.write("/n")
zip.close()
thumbnail.close()

Jun 18 '06 #1
9 3567
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.argv[1])
outURL=sys.argv[2]

zip=zipfile.ZipFile(inURL,mode="r")
picture=zip.read("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***********@acm.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.ZipFile(inURL,mode="r")
picture=zip.read("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.thumbnail((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.argv[1])
outURL=sys.argv[2]

zip=zipfile.ZipFile(inURL,mode="r")
picture=zip.read("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.ZipFile(inURL,mode="r")
picture=zip.read("00.jpg")
image = Image.open(StringIO(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.ZipFile(inURL,mode="r")
picture=zip.read("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.StringIO(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.ZipFile("text.zip",mode="r")
picture=zip.read(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.ZipFile("text.zip",mode="r")
picture=zip.read(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(jpeglist[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.argv[1])
outURL=sys.argv[2]

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

zip.close() #close the file, since we no longer have need of it
image = Image.open(StringIO.StringIO(picture)) # create image object
from file-like object
image.thumbnail((128,128),Image.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
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...
2
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
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...
3
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++){...
12
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...
2
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...
25
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...
4
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...
3
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.