Python creating a function that pulls a picture from a url then resizing to thumbnail
Python creating a function that pulls a picture from a url then resizing to thumbnail
So I have been having problems trying to write the function to change the size of an image if to big and saving it as a thumbnail. I have how to retrieve the image just lost after that. I know about pillow but cant use for the class any help would be appreciated.
Update: So far I have gotten the code to resize the image and make it a thumbnail. The next part that I am on is having it save if resized to thumbnail2, but if it stays the same save as thumbnail1. Here is my code so far without the next step.
import urllib
url ="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstra ion_1.png"
src = "C:UserslaramiePicturesPNG_transparency_demonstration_1.png"
connect = urllib.urlretrieve(url, src)
def scalePicture(src):
newWidth = getWidth(src)/2
newHeight = getHeight(src)/2
canvas = makeEmptyPicture(newWidth, newHeight)
for x in range(newWidth):
for y in range(newHeight):
setColor(getPixel(canvas, x,y), getColor(getPixel(src, x*2, y*2)))
return canvas
def thumbNail():
srcPic = makePicture(src)
destWidth = getWidth(srcPic) / 2
destHeight = getHeight(srcPic) / 2
destPic = makeEmptyPicture(destWidth, destHeight)
destPic = scalePicture(srcPic)
show(srcPic)
show(destPic)
thumbNail()
sorry im just confused on my next step I have to make a function that checks it size and changes the size if its to big.
– Onedrkzero
1 hour ago
With what library do you want to work on resizing the image? standard python libraries do not offer such functionality since each image format has a data structure (compression, coding, etc.)
– eyllanesc
1 hour ago
The only image support that comes with Python is low-level stuff like
imghdr
to guess what image format a file is in and colorsys
to convert individual pixels. Unless you want to transmit data in a really simple uncompressed format instead of PNG, or read up on the PNG file format and write your own library to decode it, you are going to have to use either Pillow or some other image library like Wand.– abarnert
49 mins ago
imghdr
colorsys
There used to be libraries for all kinds of ancient formats like
rgbimg
, but those are all gone, and not likely to have helped you anyway. And AFAIK, nobody's interested in adding support for newer modules to the standard library; instead, they want to add links to external libraries like Pillow to the standard library docs.– abarnert
47 mins ago
rgbimg
1 Answer
1
There are a bunch of strange things going on in your code:
destPic = makeEmptyPicture(destWidth, destHeight)
destPic = scalePicture(srcPic)
the first line here is not required, because the destPic
is overwritten immediately.
destPic
for x in range(newWidth):
for y in range(newHeight):
setColor(getPixel(canvas, x,y), getColor(getPixel(src, x*2, y*2)))
Ths is a very inefficient way to scale an image, that gives inferior results, unless the scale factor is an integer, and even then there are faster and better approaches.
I would recommend you to import PIL
(Python Image Library) and use it to work with images. Things like loadng, saving, scaling or flipping images are easily done. However, you may need to install this library if it did not come with your python installation.
import PIL
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what is your question?
– eyllanesc
1 hour ago