Skip to content

Add: Image Compression #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ nodeenv==1.3.3
oauthlib==3.0.1
pre-commit==1.16.1
pyasn1==0.4.5
Pillow==7.0.0
pyasn1-modules==0.2.5
PyJWT==1.7.1
python3-openid==3.1.0
Expand Down
1 change: 1 addition & 0 deletions uploader/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ class FileSerializer(serializers.Serializer):
class GooglePhotosUploadInputSerializer(serializers.Serializer):
fileList = FileSerializer(many=True)
token = fields.CharField()
compress = fields.BooleanField()
12 changes: 10 additions & 2 deletions uploader/templates/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ <h5 class="card-title text-center">
var oauthToken;
var fileStagingTable;


function TDate()
{
var dc = document.getElementById(`wpDateCreated`).value;
Expand Down Expand Up @@ -282,6 +282,7 @@ <h5 class="card-title text-center">
fileStagingTable = new Tabulator("#picked-files-table", {
data: pickedFiles,
layout: "fitColumns",

responsiveLayout:"collapse",
columns: [
{
Expand All @@ -292,6 +293,8 @@ <h5 class="card-title text-center">
cell.getValue()
)}'>`;
},


width: 80,
responsive:0,
},
Expand Down Expand Up @@ -329,14 +332,17 @@ <h5 class="card-title text-center">
responsive:1,
},
{

formatter: "buttonCross",
width: 40,
align: "center",
headerSort: false,
cellClick: function(e, cell) {
cell.getRow().delete();

},
responsive:0,

}
]
});
Expand All @@ -351,7 +357,8 @@ <h5 class="card-title text-center">
.removeClass("fa-caret-right")
.addClass("fa-spinner fa-spin");
var fileData = {
fileList: fileStagingTable.getData(),
fileList: fileStagingTable.getData(),
compress: $("#imageCompress").is(":checked"),
token: oauthToken
};

Expand All @@ -369,6 +376,7 @@ <h5 class="card-title text-center">
fileData["fileList"][x]["license"] = sel.value;
}


function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== "") {
Expand Down
10 changes: 10 additions & 0 deletions uploader/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from PIL import Image


def resize_image(img):

basewidth = 512
wpercent = basewidth / float(img.size[0])
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
return img
18 changes: 16 additions & 2 deletions uploader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from uploader.serializers import GooglePhotosUploadInputSerializer
from uploader.wiki_uploader import WikiUploader

from uploader.utils import resize_image
from PIL import Image
from uploader.models import FileUpload


Expand Down Expand Up @@ -55,6 +57,7 @@ def post(self, request, format=None, *args, **kwargs):
drive_service = self.get_google_drive_service(
access_token=validated_data.get("token", None)
)
compress = validated_data.get("compress", False)
social_auth = self.request.user.social_auth.get(
provider="mediawiki"
).extra_data["access_token"]
Expand All @@ -80,10 +83,21 @@ def post(self, request, format=None, *args, **kwargs):
while done is False:
download_status, done = downloader.next_chunk()

uploaded, image_info = wiki_uploader.upload_file(
image = Image.open(fh)

if compress:
image = resize_image(image)

fh_resized = io.BytesIO()
image.save(
fh_resized,
Image.registered_extensions()["." + file["name"].split(".")[-1]],
)

uploaded, image_info = wiki_uploader.upload_file(
file_stream=fh_resized,
description=file["description"],
file_name=file["name"],
file_stream=fh,
date_created=file["date_created"],
description=file["description"],
license=file["license"],
Expand Down