0

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:\Users\laramie\Pictures\PNG_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()
Graham
  • 3,153
  • 3
  • 16
  • 31
Onedrkzero
  • 11
  • 1
  • 3
    what is your question? – eyllanesc Jul 29 '18 at 05:11
  • 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 Jul 29 '18 at 05:13
  • 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 Jul 29 '18 at 05:16
  • The only image support that comes with Python is low-level stuff like [`imghdr`](https://docs.python.org/3/library/imghdr.html) to guess what image format a file is in and [`colorsys`](https://docs.python.org/3/library/colorsys.html) 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 Jul 29 '18 at 05:27
  • There _used_ to be libraries for all kinds of ancient formats like [`rgbimg`](https://docs.python.org/2.3/lib/module-rgbimg.html), 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 Jul 29 '18 at 05:29
  • sorry for my lack of information so I have been still working on it and it looks like this. As of right now it pulls both images up the normal one and the resized. My next step is I have to make image save as thumbnail2 if it had to be adjusted and tumbnail1 if not. – Onedrkzero Jul 29 '18 at 05:34
  • I think "[Python / Pillow: How to scale an image](https://stackoverflow.com/q/24745857)" might be helpful. – Graham Jul 29 '18 at 06:06
  • I need to check the size of the original - something like if getWidth () and getHeight() are less that a constant number - let's say resize = 200 - so if the width or height is greater than 200 - call the scalePicture ( ) function - if not - you don't resize it. Might need to create a thumbNail2 ( ) function to help with this process. – Onedrkzero Jul 29 '18 at 20:04

1 Answers1

0

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.

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.

lenik
  • 23,228
  • 4
  • 34
  • 43
  • I appreciate your your help. The problem is I am doing this for a class and I think our teacher does not want u to use PIL. So I have been trouble shooting as i go. There is so much information when using PIL but almost none when trying to do everything out like I we are. – Onedrkzero Jul 29 '18 at 06:19