0

I use python with BeautifulSoup to download images, but all these images have to rotate 90 degree for view, for save time, I want to rotate it before save to disk, is there any easier way? By the way, I can download images without rotation.

mikezang
  • 2,291
  • 7
  • 32
  • 56

1 Answers1

1

This is the way to download an image -

# Import Pillow:
from PIL import Image
import urllib2
from io import StringIO

url = "http://matthiaseisen.com/pp/static/p0201_2.jpg"

# Load the original image:
img = Image.open(urllib2.urlopen(url))

# Counterclockwise 90 degree
img3 = img.rotate(90)
img3.save("img3.jpg")

# Clockwise
img4 = img.rotate(-90)
img4.save("img4.jpg")

# Diable Cropping
img5 = img.rotate(90, expand=True)
img5.save("img5.jpg")

So basically you can extract the src attributes from the img tags and try running the above piece of code. You may have to do some handling where there are Relative URLs used. [concatenating the domain and all]

Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
  • Your code is simple! I am using code as below, can you tell me which way is faster to download? Of course, it can be rotated by my method! imgData = urllib2.urlopen(urlServer).read() output = open(os.path.join(downloadLocationPath, urlLocal), 'wb') output.write(imgData) output.close() – mikezang Feb 20 '17 at 11:02
  • I found a problem, the part of image is lost after roated, cause image is not a square, what can I do? – mikezang Feb 21 '17 at 01:28
  • Have you tried to [rotate the images in their folder](http://stackoverflow.com/questions/42074311/loop-through-directory-of-images-and-rotate-them-all-x-degrees-and-save-to-direc), after they have been downloaded ? – Ettore Rizza Feb 22 '17 at 14:23
  • Thanks again! By the way, the image is created by camera, if light is off the image is black so that such images don't need to be download, how can I check image byte size before download? – mikezang Feb 22 '17 at 21:48