python-imaging-library

How to change the dpi or density when saving images using PIL

落花浮王杯 提交于 2020-07-06 04:43:15
问题 I'm trying to compress a jpg file using PIL (to be more specific, Pillow) I know images can be compressed by doing this: from PIL import Image im = Image.open(img_path) im = im.resize(new_size, Image.ANTIALIAS) im.save(output_path, optimize=True, quality=50) But I want to go further by tweaking more params, like this: im.save(output_path, optimize=True, quality=50, jfif_unit=1, dpi=(72,72), jfif_density=(72,72)) Unfortunately, it does not change dpi or density at all. How am I suppose to

How to send image to Flask server from curl request

折月煮酒 提交于 2020-07-05 08:30:18
问题 I want to send an image by curl to flask server, i am trying this curl command curl -X POST -F file=image.jpg "http://127.0.0.1:5000/" but it did not work by the way on the server side i handle the image by this code image = Image.open(request.files['file']) i am trying to read the image using PIL Is there anyway to do this? Thanks in advance 回答1: This worked for me: curl -F "file=@image.jpg" http://localhost:5000/ The '@' is important, otherwise you end up with an http error 400 (server

How to send image to Flask server from curl request

喜夏-厌秋 提交于 2020-07-05 08:29:10
问题 I want to send an image by curl to flask server, i am trying this curl command curl -X POST -F file=image.jpg "http://127.0.0.1:5000/" but it did not work by the way on the server side i handle the image by this code image = Image.open(request.files['file']) i am trying to read the image using PIL Is there anyway to do this? Thanks in advance 回答1: This worked for me: curl -F "file=@image.jpg" http://localhost:5000/ The '@' is important, otherwise you end up with an http error 400 (server

Convert non-transparent image to transparent gif image PIL

大兔子大兔子 提交于 2020-06-27 18:38:09
问题 Is it possible to convert a non-transparent png file into a transparent gif file with PIL? I need it for my turtle-graphics game. I can only seem to transparentize a png file, not a gif file. Thanks! 回答1: It's not obvious, to me at least, how you are supposed to do that! This may be an unnecessary work-around for a problem that doesn't exist because I don't know something about how PIL works internally. Anyway, I messed around with it long enough using this input image: #!/usr/bin/env python3

PIL - Images not rotating

☆樱花仙子☆ 提交于 2020-06-27 17:42:15
问题 I'm curious as to why my image is not rotating, it ends up in the same position every time. img = Image.open(r'C:\Users\Brett\Downloads\testing.jpg') exif_data = { TAGS[k]: v for k, v in img._getexif().items() if k in TAGS } print(exif_data['Orientation']) That outputs a '6' No matter how many degrees I tell the image to rotate it ends up in the same position. if exif_data['Orientation'] == 6: img.rotate(90) or if exif_data['Orientation'] == 6: img.rotate(270) or if exif_data['Orientation'] =

Print md5 hash of an image opened with Python's PIL

旧时模样 提交于 2020-06-27 07:41:24
问题 How can I open an image in PIL, then print the md5 hash of the image without saving it to a file and reading the file? 回答1: You could save the image to a io.BytesIO() , and take the md5 hash of its value: import hashlib import Image import io img = Image.open(FILENAME) m = hashlib.md5() with io.BytesIO() as memf: img.save(memf, 'PNG') data = memf.getvalue() m.update(data) print(m.hexdigest()) This will compute the same md5 hash as if you saved the Image to a file, then read the file into a

How can I insert EXIF/other metadata into a JPEG stored in a memory buffer?

六眼飞鱼酱① 提交于 2020-06-25 05:13:49
问题 I have created a JPEG using Python OpenCV, EXIF data being lost in the process and apparently not being able to be re-added when calling imwrite (reference: Can't keep image exif data when editing it with opencv in python). Two questions: In general, how can I write the original EXIF data/new custom metadata into a JPEG that exists in memory rather than a file? Would pillow/PIL be able to maintain the EXIF data and allow supplementary metadata to be added? As of 2013 (reference: how maintain

Why is PIL's Image.fromarray() distorting my image color? [duplicate]

无人久伴 提交于 2020-06-22 02:29:06
问题 This question already has answers here : OpenCV giving wrong color to colored images on loading (2 answers) Closed 11 days ago . I am generating thumbnails for mp4 videos using the following code: import cv2 as cv from PIL import Image vidcap = cv.VideoCapture(videoPath) vidcap.set(cv.CAP_PROP_POS_MSEC, millisecond) #Turn video frame into numpy ndarray success, image = vidcap.read() cv.imwrite('fromImage.jpg', image) #line to be replaced The thumbnail generated from a high budget,

Why is PIL's Image.fromarray() distorting my image color? [duplicate]

给你一囗甜甜゛ 提交于 2020-06-22 02:29:05
问题 This question already has answers here : OpenCV giving wrong color to colored images on loading (2 answers) Closed 11 days ago . I am generating thumbnails for mp4 videos using the following code: import cv2 as cv from PIL import Image vidcap = cv.VideoCapture(videoPath) vidcap.set(cv.CAP_PROP_POS_MSEC, millisecond) #Turn video frame into numpy ndarray success, image = vidcap.read() cv.imwrite('fromImage.jpg', image) #line to be replaced The thumbnail generated from a high budget,

open .raw image data using python

心已入冬 提交于 2020-06-10 09:59:20
问题 I have been searching google for the method to display a raw image data using python libraries but couldn't find any proper solution. The data is taken from a camera module and it has the '.raw' extension. Also when I tried to open it in the terminal via 'more filename.raw', the console said that this is a binary file. Vendor told me that the camera outputs 16-bits raw greyscale data. But I wonder how I can display this data via PIL, Pillow or just Numpy. I have tested the PIL's Image module.