Image resized with Python PIL are darker - python

I'm fairly new to PIL and having issue with some image processing. I'm just trying to resize an image to a different resolution using PIL:
resized_hd = image.resize((hd_width, hd_height), Image.ANTIALIAS)
However, the resized picture does not look as good/bright as the original one.
Original (5184*3456) -> http://d31d9cjolqcwln.cloudfront.net/San+Francisco/sutro+baths.jpg
Resized (2048*1366) -> http://d31d9cjolqcwln.cloudfront.net/San+Francisco/HD-sutro+baths.jpg
Any guess ?

I think it's because one of the two images (the original one) has an embedded color profile, which the other one (the resized one) doesn't have. I discovered this by trying to open the original image in gimp. The color profile will cause advanced viewers to make corrections, in theory to fix differences like the ones between paper and screen. When I view the two images in a simple viewer that doesn't know about color profiles, they are really the same brightness. It looks like the PIL library ignores the color profile and doesn't carry it onto the resized image.
I can't help you more precisely than that, though, as I don't know much about color profiles. There might be tools to copy the profile over.

Kuddo to Armin for his suggestion.
Pil allow you to attach an ICC profile to a resized image using the following code:
icc_profile = im1.info.get('icc_profile')
im6 = im1.resize((hd_width, hd_height), Image.ANTIALIAS)
## this one will preserve the colours
im6.save("colorok-"+image,icc_profile=icc_profile)
## this one don't
im6.save("nop-"+image)

Related

Is there a way to increase the size of the background image in turtle?

I am trying to make a game in python 3 which requires a background image. I already have the image. The problem here is my image looks small compared to the big screen. I don't want to change the size of the screen because then I would have to redo all the coordinates of the other objects on the screen. Is there any way I can increase the size of the background image?
Ps. I'm using Python 3 and on VS code.
Thanks in advance!
This is the picture of what the small picture looks like.enter image description here
In order to increase the size of the image, you would need too increase the resolution.
Assuming that you are using windows, open the png with Microsoft Photos, and click on the three horizontal dots at the top right.
A dropdown menu will open, press the third option from the top labeled "Resize"
After this, press on "Define custom dimensions" and you may manipulate the dimensions of the image as you like.
Then, simply save the resized image, and use it in your project.
you can also do this using CV2 library
import cv2
image=cv2.imread("image.png")
scale_percent=1.5
width=int(image.shape[1]*scale_percent)
height=int(image.shape[0]*scale_percent)
dimension=(width,height)
resized = cv2.resize(image,dimension, interpolation = cv2.INTER_AREA)
print(resized.shape)
cv2.imwrite("output.png",resized)
OR you can use PIL library as well
from PIL import Image
image = Image.open("image.png")
image.save("output.png", dpi=(image.size[0]*1.5,image.size[1]*1.5))

Python Image Scaling

I'm trying to scale a screenshot using this code :
im = Image.open(img_path)
im = im.resize((newWidth,newHeight),Image.ANTIALIAS)
but this results in a very low quality image especially texts are impossible to read
Original
Click for original Image
Scaled
Click for scaled Image
I have tried other algorithms in PIL but none of them gives the result I wanted.
I actually tried to resize my images inside Office PowerPoint and texts are clear and readable.
PowerPoint scaled
Click for Office scaled Image
Are there any other ways which I can scale the Images ?
it had worked for me.
import imutils
im = imutils.resize(im, width=Image.ANTIALIAS)
if you want for details, you can examine https://www.programcreek.com/python/example/93640/imutils.resize

Image resize to a specific height and width in django python

I have a django based website in which I have created profiles of people working in the organisation. Since this is a redesign effort, I used the already existing profile pictures. The size of current profile image style is 170x190px. Since the images already exist and are of different sizes, I want to crop them to the size specified above. But how do I decide from which side I have to crop?
Currently, I have applied style of 170by190 to all the images while displaying in profiles, but most of them look distorted as the aspect ratios do not match.
I have tried PIL thumbnail function but it does not fit the need.
Please suggest a solution.
Well, you have to resize pictures, but images ratio create huge impact on final result. As images have some ratio, and you cannot simply resize them to 170px190px without prior adjusting of their ratio, so you have to update( not crop them!) images before resizing them to get best possible output, it can be done in next ways:
Crop them manually to desired ratio (17:19). (take a while if you have plenty of images)
Create script which add padding to that images if image ratio is close to required, all images which ratio is far away from desired mark as 'human cropping required' and work with their ratio later by own (semi-manual, so still may be really time consuming)
Spend some time and write face recognation function, then process images with that function and find faces, then crop them from origin image, but before: add padding to achieve desired radio (17:19) at top and bottom of face. (recommended)
Some links which may be use full for you:
Face Recognition With Python, in Under 25 Lines of Code
facereclib module, they probably are able to help you.
Image Manipulation, The Hitchhiker’s Guide
Good luck !
Use sorl-thumbnail, you don't need to crop every image manually.

Python PIL thumbnail with Image.ANTIALIAS. How to preserve quality

I'm trying to resize images while keeping aspect ratio. I use PIL's thumbnail method for this. I use Image.ANTIALIAS filter.
You can check my code here:
image = Image.open(design.design.path)
format = image.format
image = ImageOps.mirror(image)
new_size = (241, 241)
image.thumbnail(new_size, Image.ANTIALIAS)
image.save(response, format)
This code works perfectly however quality is lost after thumbnail. I can see it by zooming in on the saved image. I can see pixels at the corners of the image while I don't on the original image. This is seen even better when I print out resized image.
You can check out sample images here: http://imgur.com/a/ifZoU
Please tell me if you need anything else
Image.save has options for you:
img.save(fpath, 'image/png', quality=100, optimize=True)
If you are making thumbnails of a pixel-based image, of course you will loose quality. Re-sizing this kind of image (as opposed to vector images) simply throw information away - there is no way to recover it.
IF you need to view the image in full size, you have to preserve the original image, and keep the scaled-down version to be shown only where it is needed - use the original for everything else.
https://en.wikipedia.org/wiki/Raster_graphics

PIL - empty PSD image

I'm trying to load this PSD image with Python Imaging Library.
http://www.2shared.com/photo/JjSY7dN-/BG1.html
I'm not very familiar with layered images. Can someone check to see what's the issue? The image appears to be completely transparent. Opening it in my image editor I noticed that the only layer in the image was hidden, I could unhide it to see the colors.
When I load the image with PIL, I get the same issue, but it seems that PIL consolidates the layers into one and I can't do the same as in my image editor. Or maybe there's something I don't know.
PIL provides only very rudimentary hooks into the PSD format. It doesn't support writing or much in the way of manipulation. Your image layer is not shown, so it results in a transparent image. You'll need something more advanced to modify the PSD.
Here's all there is about the PSD format in PIL: http://www.pythonware.com/library/pil/handbook/format-psd.htm
Soon to be available here: http://effbot.org/imagingbook/format-psd.htm

Categories